Two for batch cycles for receiving data from two different text files

I am trying to create a small script that will create a folder for each username from a text file (Users.txt) and inside this folder to create another subfolder with their ID (ID.txt)

I created the following script:

@echo off setlocal EnableDelayedExpansion set Users=Users.txt set ID=id.txt for /f %%i IN (%Users%) DO (( set username=%%i ) for /f %%j IN (%ID%) DO ( set ID=%%j mkdir c:\Test\!username!\!ID! ) ) 

but the result is that for each user folder I got all ID subfolders.

Thank you for your help!

+4
source share
3 answers

Assuming both files have the same number of lines:

 @echo off setlocal EnableDelayedExpansion set "userFile=Users.txt" set "idFile=id.txt" <"%idFile%" ( for "usebackq" /f %%i IN ("%userFile%") do ( set /p "id=" mkdir "c:\Test\%%i\!id!" ) ) 
+1
source

Based on your comments, you can save the sort counter, and in your nested loop look for the correct identifier based on this counter. I'm not even sure that this is possible.

Something like this is possible (unverified):

 set COUNTER = 0 for /f %%i in (%users%) do ( set /A COUNTER = %COUNTER%+1 goto :callInnerLoop %%i %COUNTER% ) goto :EOF :callInnerLoop SET LOCALCOUNTER=0 for /f %%i in (%id%) DO ( set /A COUNT=%COUNT%+1 if (!COUNT!=%2) ( REM FOUND %ID% goto :EOF ) ) 

This is really dirty code, but the script package hardly seems to be the right tool for the job. It would be much easier if you can combine the files (for example, a CSV file with "USERNAME; ID" on each line). Otherwise, I would recommend switching to a more powerful scripting language such as powershell.

0
source

One pretty ugly way; read the usernames, then select the appropriate line from the id file

 @echo off setlocal disabledelayedexpansion set Users=Users.txt set ID=id.txt set linenum=0 for /f %%i IN (%Users%) DO ( call :getId %%linenum%% %%i set /a linenum=linenum+1 ) goto :eof :getId if "%1"=="0" (set options=) else set options=skip=%1 for /f "%options%" %%j in (%id%) do ( echo %2\%%j goto:eof ) 
0
source

Source: https://habr.com/ru/post/1446950/


All Articles