Home All Groups Group Topic Archive Search About

Reading from file - batch

Author
9 Nov 2007 8:37 AM
abs
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

Author
9 Nov 2007 2:16 PM
Jeffery Hicks [MVP]
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.
Show quoteHide quote
"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
>
>
>
Author
11 Nov 2007 11:49 PM
Al Dunbar
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

Show quoteHide quote
"Jeffery Hicks [MVP]" <jhi***@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
>>
>>
>>
>
Author
12 Nov 2007 7:10 PM
RemS
>> "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


Show quoteHide quote
"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
> >
> >
> >
>