computing
  • 1

Solved BATCH Text Content To Multiple Variables

  • 1

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

Share

1 Answer

  1. 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
    )
    

    • 0