Solved BATCH text content to multiple variables

February 8, 2016 at 04:05:21
Specs: Windows 7
Do you know a way to save the contents of a file into multiple variables in a Batch file?

I have a file called userlist.txt containing information like:

user1:password1
user2:password2
etc.

and i want to save the content of that file in two variables, so i can use these variables on other commands (like automatic creation of users on a server or creation of a bash file for a linux server).

something like this (%%u=user and %%p=password for each line):

echo #!/bin/bash > userStep.sh
echo groupeadd somegroup >> userStep.sh
for /f "delims=" %%u %%p in (%userFile%) do (
mkdir z:\users\%%u
net user %%u %%p /add
cacls z:\users\%%u /E /G %%u:r
cacls z:\users\%%u /E /G %%u:w
echo usereadd -d /srv/users/%%u -g somegroup -N -R -s /sbin/nologin %%a >> userStep.sh
echo echo "%%u:%%p" ^| chpasswd >> userStep.sh
)

any help will be appreciated

message edited by Atrealis


See More: BATCH text content to multiple variables

Report •

✔ Best Answer
February 10, 2016 at 02:27:32
You can use whatever letter you like for your two variables, but they must be strictly sequential, i.e. %%i/%%j or %%a/%%b or %%u/%%v and so on.

@echo off
set userFile=userList.txt
echo.#!/bin/bash > userStep.sh
echo.groupeadd somegroup >> userStep.sh
for /F "tokens=1,2 delims=:" %%i in (%userFile%) do (
  md Z:\users\%%i
  net user %%i:%%j /add
  cacls Z:\users\%%i /E /G %%i:r
  cacls Z:\users\%%i /E /G %%i:w
  echo.usereadd -d /srv/users/%%i -g somegroup -N -R -s /sbin/nologin %%i >> userStep.sh
  echo.echo "%%i:%%j" ^| chpasswd >> userStep.sh
)



#1
February 8, 2016 at 08:48:29
::====== script starts here ===============
::
:: atrealis.bat 2016-02-08 23:44:51.78
@echo off & setLocal enableDELAYedeXpansioN

(
set/p v1=
set/p v2=
) < myfile
::====== script ends here =================

=====================

M2 Get custom script or take private lessons


Report •

#2
February 9, 2016 at 01:14:39
Can you explain where in your script login:password lines from file are grabbed and processed as 2 separate vars in a loop so I can process every lines automatically in the in-loop commands?

Report •

#3
February 9, 2016 at 07:34:15
Maybe I can solve your issues, but, please, explain where your %%a variable comes from in rhe sample (and incorrect) script you posted.

message edited by IVO


Report •

Related Solutions

#4
February 9, 2016 at 23:07:29
Oh yeah sorry, %%a is %%u, I just forgot to rename this one when pasting.
Script is correct only if I set static password (delim without %%p and every other %%p replaced with static password var)

message edited by Atrealis


Report •

#5
February 10, 2016 at 02:27:32
✔ Best Answer
You can use whatever letter you like for your two variables, but they must be strictly sequential, i.e. %%i/%%j or %%a/%%b or %%u/%%v and so on.

@echo off
set userFile=userList.txt
echo.#!/bin/bash > userStep.sh
echo.groupeadd somegroup >> userStep.sh
for /F "tokens=1,2 delims=:" %%i in (%userFile%) do (
  md Z:\users\%%i
  net user %%i:%%j /add
  cacls Z:\users\%%i /E /G %%i:r
  cacls Z:\users\%%i /E /G %%i:w
  echo.usereadd -d /srv/users/%%i -g somegroup -N -R -s /sbin/nologin %%i >> userStep.sh
  echo.echo "%%i:%%j" ^| chpasswd >> userStep.sh
)


Report •

#6
February 11, 2016 at 01:09:04
Thanks! Working like a charm and I learned how to extract multiple vars on for loop =)

Report •

Ask Question