PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Hébergement serveur > ms.win.server.scripting > Reading from file - batch
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Reading from file - batch

Réponse
 
LinkBack Outils de la discussion
Vieux 09/11/2007, 08h37   #1
abs
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Reading from file - batch

Hi everyone.

This is how my file containing list of users looks like (id username):

01 UserA
02 UserB
03 UserC
....

I'm trying to write a batch file which could read the id of user whose name
is passed as a parameter and save it to variable. I can find the line
containing user's name:

find "%1" UserB userlist.txt

but how to save just user's id to variable ?

Best regards,
ABS



  Réponse avec citation
Vieux 09/11/2007, 14h16   #2
Jeffery Hicks [MVP]
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Reading from file - batch

Try something like this:

FOR /F "tokens=1" %x in ('find /i "userb" users.txt') do @set zuser=%x

If you use this in a batch file change %x to %%x.

--
Jeffery Hicks
Microsoft PowerShell MVP
http://www.scriptinganswers.com
http://www.powershellcommunity.org

Now Available: WSH and VBScript Core: TFM
Coming Soon: Windows PowerShell: TFM 2nd Ed.
"abs" <nospam@wp.pl> wrote in message news:fh166n$phk$1@inews.gazeta.pl...
> Hi everyone.
>
> This is how my file containing list of users looks like (id username):
>
> 01 UserA
> 02 UserB
> 03 UserC
> ...
>
> I'm trying to write a batch file which could read the id of user whose
> name
> is passed as a parameter and save it to variable. I can find the line
> containing user's name:
>
> find "%1" UserB userlist.txt
>
> but how to save just user's id to variable ?
>
> Best regards,
> ABS
>
>
>


  Réponse avec citation
Vieux 11/11/2007, 23h49   #3
Al Dunbar
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Reading from file - batch

If by "ID" you mean the number in the first column, be careful about
treating it as if it were a number; the leading zero will flag it as an
octal number, in which case 08 and 09 will result in errors.


/Al

"Jeffery Hicks [MVP]" <jhicks@sapien.com> wrote in message
news:8F5155A0-9AB1-43E4-922C-5E11A8A30675@microsoft.com...
> Try something like this:
>
> FOR /F "tokens=1" %x in ('find /i "userb" users.txt') do @set zuser=%x
>
> If you use this in a batch file change %x to %%x.
>
> --
> Jeffery Hicks
> Microsoft PowerShell MVP
> http://www.scriptinganswers.com
> http://www.powershellcommunity.org
>
> Now Available: WSH and VBScript Core: TFM
> Coming Soon: Windows PowerShell: TFM 2nd Ed.
> "abs" <nospam@wp.pl> wrote in message news:fh166n$phk$1@inews.gazeta.pl...
>> Hi everyone.
>>
>> This is how my file containing list of users looks like (id username):
>>
>> 01 UserA
>> 02 UserB
>> 03 UserC
>> ...
>>
>> I'm trying to write a batch file which could read the id of user whose
>> name
>> is passed as a parameter and save it to variable. I can find the line
>> containing user's name:
>>
>> find "%1" UserB userlist.txt
>>
>> but how to save just user's id to variable ?
>>
>> Best regards,
>> ABS
>>
>>
>>

>



  Réponse avec citation
Vieux 12/11/2007, 19h10   #4
\RemS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Reading from file - batch

>> "save just user's id to variable "

In the batch below;
%1 set to static variable 'User' = "User name" (from command-line
parameter)
%1 reset to static variable 'User!' = "User_name" (no spaces)
Dynamic Variable '%User!%' = userID
Dynamic variable '%UserID%' = user name

::------------------------------------------------------

@echo off

(Set User=%1)

(Set User=%User:"=%) ||::> If exist, Erase quotes from parameter

FOR /F "tokens=1 delims= " %%* in ('find /i "%User%" "c:\UID.txt"') Do (
set UserID=%%*)

SetLocal

(Call :RemoveSpaces %User: =_%)&(GoTo :EOF)
:RemoveSpaces from original parameter
(Set User!=%1)
Goto :EndRemoveSpaces
:EndRemoveSpaces <---

(Call Set %%User!%%=%UserID%) & (Call Set %%UserID%%=%User%)


:ShowResults
echo.command-line parameter = "%User%" & echo.
echo.Match: %UserID% %User% & echo:

echo.&echo.Variables:&echo:_______________________ __________________
echo.
(call echo %1=%%%1%%) ||::> show dynamic variable value
echo.
(Set %User!%) ||::> show dynamic Usr environment variable
echo.
(Set %UserID%) ||::> show dynamic UserID environment variable
echo.
(echo %User!%=%UserID%) ||::> show static variable and value
echo.
echo.&echo.&echo.&pause

EndLocal

:EOF

::-----------------------------------------------

\Rems


"Jeffery Hicks [MVP]" wrote:

> Try something like this:
>
> FOR /F "tokens=1" %x in ('find /i "userb" users.txt') do @set zuser=%x
>
> If you use this in a batch file change %x to %%x.
>
> --
> Jeffery Hicks
> Microsoft PowerShell MVP
> http://www.scriptinganswers.com
> http://www.powershellcommunity.org
>
> Now Available: WSH and VBScript Core: TFM
> Coming Soon: Windows PowerShell: TFM 2nd Ed.
> "abs" <nospam@wp.pl> wrote in message news:fh166n$phk$1@inews.gazeta.pl...
> > Hi everyone.
> >
> > This is how my file containing list of users looks like (id username):
> >
> > 01 UserA
> > 02 UserB
> > 03 UserC
> > ...
> >
> > I'm trying to write a batch file which could read the id of user whose
> > name
> > is passed as a parameter and save it to variable. I can find the line
> > containing user's name:
> >
> > find "%1" UserB userlist.txt
> >
> > but how to save just user's id to variable ?
> >
> > Best regards,
> > ABS
> >
> >
> >

>

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 23h56.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,18525 seconds with 12 queries