|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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 > > > |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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 >> >> >> > |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
>> "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 > > > > > > > |
|
![]() |
| Outils de la discussion | |
|
|