Nested FOR loops and variables in Batch - windows

I have the following code;
setlocal EnableDelayedExpansion
:splitEncode
::Get the number of Chapters
set "cmd=FINDSTR /R /N "^.*" %~n1.txt | FIND /C ":""
for /F %%a in ('!cmd!') do set numChapters=%%a
::Cycle through this once for every chapter, getting the line and the line after it
for /L %%a in (1,1,%numChapters%) do (
set "skip="
if %%a geq 2 (
set /a skip=%%a-1
set "skip=skip=!skip!"
)
for /F "!skip! tokens=1,2" %%i in ("%~n1.txt") do (
set startTime=%%i
set chapterName=%%j
)
set "skip=skip=%%a"
for /F !skip! %%i in ("%~n1.txt") do (
set endTime=%%i
)
echo %startTime% %endTime% %chapterName%
)
First I find out how many lines are in a text file, and set that to the variable numChapters.
I then use this to create a for loop that iterates for each chapter.
Inside the for loop, there are two further loops. The first reads a line, and the second reads the following line.
The intent of this is to read lines 1+2, 2+3, 3+4, and use those values as part of a command run the same number of times as the number of lines.
This means that from a list such as this;
00:00:00 The Meeting Room/The Meeting
00:03:36 Long Distance Runaround
00:07:47 Wonderous Stories
I can end up with a command that includes the start time, end time, and chapter title.
The issue I am facing is that no matter what I do, I cannot get the nested for loops to use the skip variables. I've tried %%a, %skip%, !skip!, and none of them work. The value isn't correctly substituted in any situation.
Does anyone have any way to get this variable used, or a better method of reading a specific line of a text file than a for loop?

The option string of for /F (like the root path of for /R) requires immediate (%-)expansion, because for (besides if and rem) is recognised by the command interpreter even before delayed expansion and also expansion of for meta-variables occur.
A possible solution is to put each for /F loop with the dynamic skip options into a sub-routine, to use call to call it and to apply %-expansion therein (see all the additional rem remarks for explanations):
#echo off
setlocal EnableDelayedExpansion
:splitEncode
rem Get the number of chapters
rem // To determine the number of lines in a file you do not need `findstr`:
for /F %%a in ('^< "%~n1.txt" find /C /V ""') do set "numChapters=%%a"
rem Cycle through this once for every chapter, getting the line and the line after it
for /L %%a in (1,1,%numChapters%) do (
set /A "skip=%%a-1"
call :getTwoValues startTime chapterName "%~n1.txt" "!skip!"
call :getTwoValues endTime dummy "%~n1.txt" "%%a"
rem /* For the last line, there is of course no next line containing the end time;
rem therefore, let us mark that case specifically: */
if not defined endTime set "endTime=??:??:??"
rem /* If there is no chapter specified, do not output anything; this might also
rem be quite useful in case the last line just contains a time stamp but no
rem chapter name just to provide the end time of the last one: */
if defined chapterName echo !startTime! !endTime! !chapterName!
)
goto :EOF
:getTwoValues <1st var. name> <2nd var. name> <file path/name> <lines to skip>
rem // Ensure not to return the former output, and set up `skip` option string:
set "%~1=" & set "%~2=" & set /A "skip=0, skip+=%~4" 2> nul
if %skip% gtr 0 (set "skip=skip=%skip%") else set "skip="
rem /* Added `usebackq` in order not to interprete the quoted file path/name as
rem a literal string; also changed the `tokens` option to return the first
rem token and then the whole remainder of the line: */
rem /* Remember that `for /F` regards empty lines for its `skip` option, but it
rem does not iterate through such; hence the first line it iterates over is
rem actually the first non-empty line after the number of skipped lines: */
for /F "usebackq %skip% tokens=1,*" %%i in ("%~3") do (
set "%~1=%%i"
set "%~2=%%j"
rem // Since we do not want to iterate to the last line, leave the loop here:
goto :EOF
)
rem /* This is just needed in case `skip` points beyond the end of the file, or
rem there are no more non-empty lines behind the skipped ones: */
goto :EOF
Based on your sample data, the output should be this:
00:00:00 00:03:36 The Meeting Room/The Meeting
00:03:36 00:07:47 Long Distance Runaround
00:07:47 ??:??:?? Wonderous Stories
However, the entire approach could be heavily simplified, when you avoid the file multiple times and do not read each line twice, by simply reading the file line by line once, but return the chapter information from the previous iteration, together with the end time from the current line:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_FILE=%~n1.txt" & rem // (path/name of file to process)
set "_FRMT=??:??:??" & rem // (dummy end time output for last chapter)
rem /* Initialise variables; loop through lines of files, augmented by
rem an additional line at the end, to alyways output last chapter: */
set "STA=" & for /F "tokens=1,*" %%K in ('
type "%_FILE%" ^& echo(%_FRMT%
') do (
rem // Output the chapter from the previous loop iteration:
set "END=%%K" & if defined STA if defined NAME (
setlocal EnableDelayedExpansion
echo(!STA! !END! !NAME!
endlocal
)
rem // Store chapter information for the next loop iteration:
set "STA=%%K" & set "NAME=%%L"
)
endlocal
exit /B

Related

How to iterate through items in an .ini file with a batch file?

I am currently trying to loop through every item from a .ini file and to work with the values later on. But I can't figure out how. My config.ini file looks like this:
[items]
item_1=XXXXX
item_2=XXXXX
item_3=XXXXX
item_4=XXXXX
[SomeSection]
......
I found a way to iterate and echo every item from the config.ini file, like so:
#echo off
for /F %%i in (config.ini) do (
echo %%i
)
My problem is that I want to work with specific values. So I have to check the categorie and the keys from the config.ini file. I tried using this, but I ran into errors:
#echo off
for /F %%i in (config.ini) do (
SET item = %%i
if %item%==[items] (
rem do something here with the key and values now
)
)
As I already mentioned, I am not able to save the values to another variable, which leads to my problem that I can't work with them.
A quite simple approach was to determine the line number of the target section header in advance, then skip such as many lines when reading the configuration file, stopping as soon as there occurs another string enclosed within brackets:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_CONFIG=%~dp0config.ini" & rem // (path to configuration file)
set "_SECT=items" & rem // (section name without brackets)
rem // Clean up variables whose names begin with `$`:
for /F "delims==" %%V in ('2^> nul set "$"') do set "%%V="
rem // Gather number of line containing the given section (ignoring case):
for /F "delims=:" %%N in ('findstr /N /I /X /C:"[%_SECT%]" "%_CONFIG%"') do set "SKIP=%%N"
rem // Read configuration file, skipping everything up to the section header:
for /F "usebackq skip=%SKIP% delims=" %%I in ("%_CONFIG%") do (
rem // Leave loop as soon as another section header is reached:
for /F "tokens=1* delims=[]" %%K in ("%%I") do if "[%%K]%%L"=="%%I" goto :NEXT
rem // Do something with the key/value pair, like echoing it:
echo(%%I
rem // Assign a variable named of `$` + key and assign the value:
set "$%%I"
)
:NEXT
rem // Return assigned variables:
set "$"
endlocal
exit /B
This script would assign the following variables, based on your sample configuration file:
$item_1=XXXXX
$item_2=XXXXX
$item_3=XXXXX
$item_4=XXXXX

CMD - Pivot / Transpose lines of file [e.g. convert 1500 lines (x1 col) --> 500 lines (x3 col)]

how please can I 'pivot' or transpose a file (i.e. turn a single-column list, into a table of data)...
Currently...
VideoA.name
VideoA.size
VideoA.bitrate
VideoB.name
VideoB.size
VideoB.bitrate
...
Desired...
VideoA.name, VideoA.size, VideoA.bitrate
VideoB.name, VideoB.size, VideoB.bitrate
Name
Size
Bitrate
VideoA.name
VideoA.size
VideoA.bitrate
VideoB.name
VideoB.size
VideoB.bitrate
Extra Info / Context
I'm aware people often ask 'why are you doing this?' so (if interested), here is the wider context / problem I am trying to solve...
I have a list of files in Files.txt
I have a jscript batch file getProps.bat that extract file properties and prints them, 1 per line
I have written a batch file to loop through Files.txt, get the properties of each and write the output to Details.csv
However if I have 500 files x 3 properties, this currently gives me 1500 lines, and I want 500 lines x 3 columns
GetProps_AllFiles.bat
---------------------
#ECHO OFF
SETLOCAL
FOR /F "tokens=*" %%A in (Files.txt) do (
getprops %%A 0,1,320 /noheaders >> Details.csv
)
Thanks in advance!
Use the "standard way" (for /f) to read a file line by line, extended by a counter. Add the line to a string (line), followed by a comma (or whatever you want to use as separator), and increase the counter. Except it's the third line. Then print the string plus the current line, reset the counter and string, and repeat.
#echo off
setlocal enabledelayedexpansion
set "line="
set count=0
(for /f "delims=" %%a in (test.txt) do (
set /a count+=1
if !count! equ 3 (
echo !line!%%a
set "line="
set count=0
) else (
set line=!line!%%a,
)
))>test.csv
The below is a slight adjustment to the code kindly provided by Stephan that allows a filename and number of lines to be passed into the script...
ColumiseFile.cmd
----------------
#ECHO OFF
SETLOCAL enabledelayedexpansion
REM :: USAGE -----------------------------------------------------------------
REM ColumiseFile [1]File.txt [2]NumOfLines
REM > Concatenates every n Lines into 1, exporting result to File.csv
SET "File=%1"
SET /A Lines=%2
REM :: MAIN -------------------------------------------------------------------
REM Thanks to Stephan [https://stackoverflow.com/a/67664755/15919675]
REM Loops through input file, compacting every n lines into 1
set "line="
set count=0
(for /f "delims=" %%a in (%File%) do (
set /a count+=1
if !count! equ %Lines% (
echo !line!%%a
set "line="
set count=0
) else (
set line=!line!%%a,
)
REM :: OUTPUT -----------------------------------------------------------------
REM Create .csv in same location as source .txt file
))>%~dpn1.csv

How Can I Replace Any Line by Its Line Number?

EDIT: After great help from #aschipfl, the code is %110 as functional as I wanted it to be! I did some extra research and made it easy to use with prompts for that extra %10 :P
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Create a prompt to set the variables
set /p _FILETYPE="What file type: "
set /p _LINENUM="Which line: "
set /p _NEWLINE="Make line say: "
rem // Start the loop, and set the files
for %%f in (*%_FILETYPE%) do (
set "_FILE=%%f"
echo "_FILE=%%f"
rem // To execute seperate code before the end of the loop, starting at ":subroutine".
call :subroutine "%%f"
)
:subroutine
rem // Write to a temporary file:
> "%_FILE%.new" (
rem /* Loop through each line of the original file,
rem preceded by the line number and a colon `:`:*/
for /F "delims=" %%A in ('findstr /N "^" "%_FILE%"') do (
rem // Store the current line with prefix to a variable:
set "LN=%%A"
rem /* Store the line number into another variable;
rem everything up to the first non-numeric char. is regarded,
rem which is the aforementioned colon `:` in this situation: */
set /A "NUM=LN"
rem // Toggle delayed expansion to avoid trouble with `!`:
setlocal EnableDelayedExpansion
rem /* Compare current line number with predefined one and replace text
rem in case of equality, or return original text otherwise: */
if !NUM! equ %_LINENUM% (
echo(!_NEWLINE!
) else (
rem // Remove line number prefix:
echo(!LN:*:=!
)
endlocal
)
)
rem // Move the edited file onto the original one:
move /Y "%_FILE%.new" "%_FILE%"
endlocal
exit /B
ORIGINAL QUESTION:
Doesn't matter whats in any of the lines already. I just want to be able to pick any line from a .txt and replace it with whatever I choose.
So for example: Maybe I have a bunch of .txt's, and I want to replace line 5 in all of them with "vanilla". And later choose to replace line 10 of all .txt's with "Green". And so on...
I've seen lots of people asking the same main question. But I keep finding situational answers.
"How do I replace specific lines?" "you search for whats already in the line, and replace it with your new text" -I cant have that. I need it to be dynamic, because whats in each "line 5" is different, or there's lots of other lines with the same text.
I had tried the only one answer I could find, but all it ended up doing is replace literally all lines with "!ln:*:=!", instead of echoing.
#echo off
setlocal disableDelayedExpansion
set "file=yourFile.txt"
set "newLine5=NewLine5Here"
>"%file%.new" (
for /f "delims=" %%A in ('findstr /n "^" "%file%"') do for /f "delims=:" %%N in ("%%A") do (
set "ln=%%A"
setlocal enabableDelayedExpansion
if "!ln:~0,6!" equ "5:FMOD" (echo(!newLine5!) else echo(!ln:*:=!
endlocal
)
)
move /y "%file%.new" "%file%" >nul
The following (commented) code should work for you:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_FILE=yourFile.txt"
set "_NEWLINE=NewLine5Here"
set /A "_LINENUM=5" & rem // (target line number)
rem // Write to a temporary file:
> "%_FILE%.new" (
rem /* Loop through each line of the original file,
rem preceded by the line number and a colon `:`:*/
for /F "delims=" %%A in ('findstr /N "^" "%_FILE%"') do (
rem // Store the current line with prefix to a variable:
set "LN=%%A"
rem /* Store the line number into another variable;
rem everything up to the first non-numeric char. is regarded,
rem which is the aforementioned colon `:` in this situation: */
set /A "NUM=LN"
rem // Toggle delayed expansion to avoid trouble with `!`:
setlocal EnableDelayedExpansion
rem /* Compare current line number with predefined one and replace text
rem in case of equality, or return original text otherwise: */
if !NUM! equ %_LINENUM% (
echo(!_NEWLINE!
) else (
rem // Remove line number prefix:
echo(!LN:*:=!
)
endlocal
)
)
rem // Move the edited file onto the original one:
move /Y "%_FILE%.new" "%_FILE%"
endlocal
exit /B
Besides the typo in EnableDelayedExpansion in your code, you do not even need a second for /F loop to get the line number, and you do not need to extract a certain number of characters from the prefixed line text.
Note that this approach fails for line numbers higher than 231 - 1 = 2 147 483 647.
...is replace literally all lines with "!ln:*:=!", instead of echoing.
But that's correct, because the FINDSTR /N prefixes each line with a line number before.
The !ln:*:=! only removes the line number again.
And the findstr trick is used to avoid skipping of empty lines or lines beginning with ; (the EOL character).
The !line:*:=! replaces everthing up to the first double colon (and incuding it) with nothing.
This is better than using FOR "delims=:" because delims=: would also strip double colons at the front of a line.
The toggling of delayed expansion is necessary to avoid accidential stripping of ! and ^ in the line set "ln=%%A"
To fix your code:
setlocal DisableDelayedExpansion
for /f "delims=" %%A in ('findstr /n "^" "%file%"') do (
set "ln=%%A"
setlocal EnableDelayedExpansion
if "!ln:~0,6!" equ "5:FMOD" (
set "out=!newLine5!"
) else (
set "out=!ln:*:=!"
)
echo(!out!
endlocal
)

How do I merge text files in a particular order using cmd

I am using the following command copy *.txt newfile.txt to merge my text files into the main file but the order gets messed up. I have text files whose name are in the order
1january.txt
2february.txt
3february.txt
4march.txt
5may.txt
6june.txt
7july.txt
8august.txt
9september.txt
10october.txt
11november.txt
12december.txt
But using the cmd command it first appends 10october,11november,12december & then appends from 1january.
Is there any command in cmd that can do this or any other code will also do.
A possible way is this (given that the preceding numbers are positive and do not have leading zeros, and the file names do not contain ! or ^):
cmd /V /Q /C copy nul "newfile.txt" ^& set /A "LIM=0" ^& (for %J in ("*.txt") do set "NUM=%~nJ" ^& set /A "NUM+=0" ^& set "$[!NUM!]=%~J" ^& if !LIM! lss !NUM! set /A "LIM=NUM") ^& (for /L %I in (1,1,!LIM!) do if defined $[%I] copy /B "newfile.txt" + "!$[%I]!" "newfile.txt")
If you place this code in a batch-file it might look like this (note that I additionally inserted several rem marks for explanation of the code here):
#echo off
rem // Enable delayed expansion to be able to write AND read variables in a single block:
setlocal EnableDelayedExpansion
rem // Create empty target file:
copy nul "newfile.txt"
rem // Reset buffer for greatest preceding integer number:
set /A "LIM=0"
rem // walk through all matching files:
for %%J in ("*.txt") do (
rem // Store base file name to variable, then covert it to integer:
set "NUM=%%~nJ" & set /A "NUM+=0"
rem // Write full file name to array-like variable with gathered integer as index:
set "$[!NUM!]=%%~J"
rem // Update buffer for greatest preceding integer number:
if !LIM! lss !NUM! set /A "LIM=NUM"
)
rem // Count up from one to greatest preceding integer number:
for /L %%I in (1,1,!LIM!) do (
rem // Check whether pseudo-array element is defined and append to target file then:
if defined $[%%I] copy /B "newfile.txt" + "!$[%%I]!" "newfile.txt"
)
rem // End environment localisation:
endlocal

Loop recursively into subfolders and look for a substring into files

I would like to create a script that loops reccursively through subfolders of D:\MyFolder\ for example, to find multiple files named MyFile.txt
then look into each file for the keyword FROM and retrieve the string between the FROM and the next semicolon ;.
Sample of MyFile.txt:
LOAD
Thing1,
Thing2,
Thing3,
FROM
Somewhere ;
The desired result is: Somewhere.
(The position of the semicolon ; can be in another line).
I did some tries but I did not succeed in writing a correct script:
#echo off
SET PATH="D:\MyFolder\"
FOR /R %PATH% %%f IN (MyFile.txt) DO (
FOR /F "delims=FROM eol=;" %%A in (%%f) do (
set str=%%A
ECHO %str%
)
)
If it can't be done in batch, please let me know in which language I can do it easily. I would like to have an executable script in the end.
There are some issues in your code:
The delims option of for /F defines characters but not words to be used as delimiter for parsing text files. To find a word, use findstr instead (you could use its /N option to derive the position/line number of the search string).
The eol option of for /F defines a character to ignore a line in case it occurs at the beginning (or it is preceded by delimiters only).
for /R does actually not search for files in case there are no wild-cards (?, *) in the set (that is the part in between parentheses). The dir /S command does, so you can work around this by wrapping a for /F loop around dir /S.
The PATH variable is used by the system to find executables, like findstr, so you must not overwrite it; use a different variable name instead.
Here is the way I would probably do it (supposing any text following the keyword FROM needs to be returned also):
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_ROOT=D:\MyFolder" & rem // (root directory of the tree to find files)
set "_FILE=MyFile.txt" & rem // (name of the files to find in the tree)
set "_WORD=FROM" & rem // (keyword to be searched within the files)
set "_CHAR=;" & rem // (character to be searched within the files)
rem // Walk through the directory tree and find matching files:
for /F "delims=" %%F in ('dir /B /S "%_ROOT%\%_FILE%"') do (
rem // Retrieve the line number of each occurrence of the keyword:
for /F "delims=:" %%N in ('findstr /N /I /R "\<%_WORD%\>" "%%~F"') do (
rem // Process each occurrence of the keyword in a sub-routine:
call :PROCESS "%%~F" %%N
)
)
endlocal
exit /B
:PROCESS
rem // Ensure the line number to be numeric and build `skip` option string:
set /A "SKIP=%~2-1"
if %SKIP% GTR 0 (set "SKIP=skip^=%SKIP%") else set "SKIP="
rem // Read file starting from line containing the found keyword:
set "FRST=#"
for /F usebackq^ %SKIP%^ delims^=^ eol^= %%L in ("%~1") do (
set "LINE=%%L"
setlocal EnableDelayedExpansion
rem // Split off everything up to the keyword from the first iterated line:
if defined FRST set "LINE=!LINE:*%_WORD%=!"
rem /* Split read line at the first occurrence of the split character;
rem the line string is augmented by preceding and appending a space,
rem so it is possible to detect whether a split char. is there: */
for /F "tokens=1,* delims=%_CHAR% eol=%_CHAR%" %%S in (" !LINE! ") do (
endlocal
set "TEXT=%%S"
set "RMND=%%T"
set "ITEM=%~1"
setlocal EnableDelayedExpansion
rem // Check whether a split character is included in the line string:
if not defined RMND (
rem // No split char. found, so get string without surrounding spaces:
set "TEXT=!TEXT:~1,-1!"
) else (
rem // Split char. found, so get string without leading space:
set "TEXT=!TEXT:~1!"
)
rem // Trimm leading white-spaces:
for /F "tokens=*" %%E in ("!TEXT!") do (
endlocal
set "TEXT=%%E"
setlocal EnableDelayedExpansion
)
rem // Return string in case it is not empty:
if defined TEXT echo(!ITEM!;!TEXT!
rem // Leave sub-routine in case split char. has been found:
if defined RMND exit /B
)
endlocal
set "FRST="
)
exit /B

Resources