Batch script Read multi line Argument in Variable - batch-file

I wanted to take list of files to delete from user as a argument. One line per argument.
How can store the list of files separated by new line in a variable.
I am using below command.
Set DeletionFiles=${p:DeleteFiles}"
for %%i in (%DeletionFiles%) do (
echo %%i
)
Then i wanted to iterated them on a loop.
${p:DeleteFiles} will get replaced by it's value from external app, which will contain list of files separated by new line.I can not change it.

#ECHO OFF
SETLOCAL
SET "deletionfiles="
:dloop
SET "deleteme="
SET /p "deleteme=Which file to delete ? "
IF DEFINED deleteme SET "deleteme=%deleteme:"=%"
IF DEFINED deleteme SET "deletionfiles=%deletionfiles%,"%deleteme%""&goto dloop
ECHO delete %deletionfiles:~1%
GOTO :EOF
There is no need to use a newline. Your for command (or a del command) will operate perfectly happily on a comma-(or space-)separated list.
Note that there are certain characters that batch uses for special purposes and batch string-processing may not process them in the expected manner. These characters include % ^ and &.

${p:DeleteFiles} will get replaced by it's value from external app,
which will contain list of files separated by new line.I can not
change it.
After the replacement the batch file looks like:
Set DeletionFiles=file1.jpg
file2.jpg
file3.jpg
"
This isn't a valid batch file anymore.
Furthermore it's a bad idea to modify the batch file itself, as this works only once.
You could place the ${p:DeleteFiles} into another file, like input.txt.
Your batch would look like
echo ${p:DeleteFiles} > input.txt
<external program for replacing the DeleteFiles> input.txt
for /F "tokens=*" %%A in (input.txt) do (
echo File: %%A
)

If I understand you correctly, your external program will generate a list of files. You then want to store this multi-line list to a variable. What do you want to do with the variable once you have it? I assume you want to delete the files, but your question isn't clear on that point, so I'll try to over-answer to cover it.
for /f "delims=" %%a in ('{command that generates your list}') do (
echo Doing stuff to %%a...
echo %%a>>listOfFilesToDelete.txt
set var=%%a
if "%var:~0,7%"="DoNotDelete" copy "%%a" \someArchiveFolder\
del "%%a"
)
This will read each line in your generated list as variable %%a. It will then do whatever command(s) you specify. This way, you can run a command on each of the files in the list. In the above code it's
Printing each line to the console embedded in some text
Outputting it to a file
Checking the first 7 characters of the line against a specified string and then copying it to a folder if it matches
And then deleting it
If you still need to reference each line from your generated list, you can even setup an array-like structure. (See Create list or arrays in Windows Batch)
setlocal EnableDelayedExpansion
:: Capture lines in an 'array'
set /a i=0
for /f "delims=" %%a in ('dir /b') do (
set /a i+=1
set var!i!=%%a
)
:: Loop through the 'array'
for /L %%a in (1,1,%i%) do (
echo Do more stuff with !var%%a!
)
Just like above, this will read each line in your generated list as variable %%a. It will then set a variable var!i! equal to the value of the current line. You can then reference each line as var1, var2, and so on, or, as the second section shows, you can loop through them all using for /L. You'll need to get a grasp on working with delayed expansion variables, though.

Related

How to sort the argument list in a a Windows 10 batch file

I need to call a program with a list of file names, but I need to find and extract the first file on the list taken as sorted order and pass the rest to the program.
Specifically, I want to pass a list of files selected with the QTTabbar application launcher and execute exiftool so that the first file on the list is used for the "-TagsFromFile" option, and then process all of the rest of the files so that they get the "-AllDates" option applied. So my first attempt was:
exiftool -TagsFromFile %1 -AllDates %*
This would put the first file on the list, but since exiftool would be setting it to the same value as it already has, that would be acceptable.
However, I discovered that QTTabbar did not pass the arguments to the batch file in the lexigraphically sorted order by name as I was expecting. So I figured I needed to sort the list.
I found the way to sort the arguments in How to sort the arguments dropped to a batch file? but in that solution there is a loop and a program is invoked once for each argument, rather than building a new argument list.
for /f "delims=" %%a in ('cmd /c ^"for %%i in ^(%*^) do #echo %%~i^"^|sort') do (
echo use "%%a"
)
Instead of "echo use "%%a", I need to build a new argument list that I can pass to exiftool. Ideally I could build a list and then replace the original argument list with the new one, like the "set" command in Bash. Failing that, I could build a new list and use that, but I don't know how to build a list and I don't know how to reference the first element if I had one.
How do I do this?
EDIT:
The files are selected in the File Explorer GUI. The order they are presented to the batch file is determined by Windows. Here is the output of "echo %*" from the batch file:
"C:\Users\user1\Desktop\setAB.test\00000920.jpg" "C:\Users\user1\Desktop\setAB.test\00000913.jpg" "C:\Users\user1\Desktop\setAB.test\00000914.jpg" "C:\Users\user1\Desktop\setAB.test\00000915.jpg" "C:\Users\user1\Desktop\setAB.test\00000916.jpg" "C:\Users\user1\Desktop\setAB.test\00000917.jpg" "C:\Users\user1\Desktop\setAB.test\00000918.jpg" "C:\Users\user1\Desktop\setAB.test\00000919.jpg"
As you can see, the last file appears first. I don't know why. Sometimes they are in reverse order.
So, the batch file gets invoked as:
ex.bat "C:\Users\user1\Desktop\setAB.test\00000920.jpg" "C:\Users\user1\Desktop\setAB.test\00000913.jpg" "C:\Users\user1\Desktop\setAB.test\00000914.jpg" "C:\Users\user1\Desktop\setAB.test\00000915.jpg" "C:\Users\user1\Desktop\setAB.test\00000916.jpg" "C:\Users\user1\Desktop\setAB.test\00000917.jpg" "C:\Users\user1\Desktop\setAB.test\00000918.jpg" "C:\Users\user1\Desktop\setAB.test\00000919.jpg"
And I want exiftool to run as:
exiftool -TagsFromFile "C:\Users\user1\Desktop\setAB.test\00000913.jpg" -AllDates "C:\Users\user1\Desktop\setAB.test\00000913.jpg" "C:\Users\user1\Desktop\setAB.test\00000914.jpg" "C:\Users\user1\Desktop\setAB.test\00000915.jpg" "C:\Users\user1\Desktop\setAB.test\00000916.jpg" "C:\Users\user1\Desktop\setAB.test\00000917.jpg" "C:\Users\user1\Desktop\setAB.test\00000918.jpg" "C:\Users\user1\Desktop\setAB.test\00000919.jpg" "C:\Users\user1\Desktop\setAB.test\00000920.jpg"
So your code was really close. All you needed to add were a few basic concepts to get the first argument into a variable which can be done by checking if a variable is defined or not. Then use the SET command to rebuild all the arguments back into another variable. This requires delayed expansion as described in this question.
#ECHO OFF
setlocal enabledelayedexpansion
set "first="
set "all="
for /f "delims=" %%a in ('cmd /c ^"for %%i in ^(%*^) do #echo %%~i^"^|sort') do (
if not defined first set "first=%%~a"
set "all=!all!"%%~a" "
)
exiftool -TagsFromFile "%first%" -AllDates %all%
endlocal
Very similar to the method already provided by Squashman, but suitable for filepaths containing ! characters.
#For %%G In (First Rest) Do #Set "%%G="
#For /F "Delims=" %%H In (
'"(For %%G In (%*) Do #Echo "%%~G") | %SystemRoot%\System32\sort.exe"'
) Do #If Not Defined First (Set "First=%%H") Else (SetLocal EnabledelayedExpansion
For /F "Delims=" %%I In ("!Rest!%%H") Do #EndLocal & Set "Rest=%%I")
exiftool.exe -TagsFromFile %First% -AllDates %Rest%

Batch file redirection to text file not working inside for loop

REM timestamped name of file
set PREFIX=LINE_
set SAVESTAMP=%PREFIX%%DATE:/=-%_%TIME::=-%
set SAVESTAMP=%SAVESTAMP: =%
set SAVESTAMP=%SAVESTAMP:,=.%.txt
echo %SAVESTAMP%
REM In H.txt, for each line grab the text after delimiter ':' and send it to file
for /f "tokens=2 delims=:" %%a in ('type D.txt^|find ":"') do (
set line=%%a
REM#1
echo %line% >> %SAVESTAMP%
)
REM#2
REM echo %line% >> %SAVESTAMP% ---This output one line
When I use REM#1 and comment the second echo outside the for loop, there is not file created.
But when I use REM#2 and don't use REM#1 echo a file with one line is created.
Most times the file will have multiples lines written to it.
So I want this working inside the for loop.
D.txt
line1:sample1
line2:sample2
line3:sample3
line4:sample4
Output should be :
Line_Date_TimeStamp.txt
sample1
sample2
sample3
sample4
Two problems:
First, there must be a separator (like a space) between rem and the text, otherwise an attempt to find and execute an executable named rem#1.(bat,exe etc.) is made.
Second, you need to search SO for delayed expansion as you are attempting to show the value of a variable (line) that is being modified within the for block.

Splitting CSV files on specific text

I have got a system that generates CSV files containing time based data.
Some files have data from two different dates. I want to break up these files into two files, one containing the data from the first day, the other containing the data from the next day. The original file looks like this:
09.01.2015 00:00:00,0385 - Veerhaven,VP01 in bedrijf hoog toerental,K,Process message.
09.01.2015 00:00:00,0385 - Veerhaven,VP01 in bedrijf laag toerental,G,Process message.
08.01.2015 23:59:55,1475 - Schaatsbaan,PO01 in bedrijf,G,Process message.
08.01.2015 23:59:52,0311 - Abraham van Stolkweg,PO01 in bedrijf,G,Process message.
The first 10 Characters are the date of the event. I want to break up the file in two output files seperating the data from the two days. I have to do this using batch processing because it has to be done every day over a lot of files.
I hope someone can help me on my way. Thanks in advance.
#echo off
setlocal enableextensions disabledelayedexpansion
set "file=c:\somewhere\data.txt"
for %%f in ("%file%") do for /f "usebackq" %%a in ("%%~ff") do (
if not defined %%a (
findstr /b /c:"%%a" "%%~ff" > "%%~dpnf.%%a%%~xf"
set "%%a=1"
)
)
The first for command is used only to retrieve a reference to the file and being able to separate path, filename and extension (that will be used later to generate the output files).
Second for loop reads the input file and for each line retrieves the first token/field in the line using spaces as delimiters (the default behaviour in for /f command). This value is used to filter the input file and declare environment variables:
If the variable is not defined, it is the first time the value is seen, matching records are extracted from the input file to a new output file and the variable is defined.
If the variable is defined, this value has been seen and the corresponding output file generated, the extraction is skipped and the process continues reading the next line.
edited to adapt to comments
#echo off
setlocal enableextensions disabledelayedexpansion
set "files=c:\somewhere\*.txt"
set "outputFolder=c:\where\to\put\files"
for %%f in ("%files%") do (
setlocal
for /f "usebackq" %%a in ("%%~ff") do if not defined %%a (
findstr /b /c:"%%a" "%%~ff" > "%outputFolder%\%%~nf.%%a%%~xf"
set "%%a=1"
)
endlocal
)
The wildcard management in the input needs no changes: for %%f iterates over the indicated set, being it only a file or a set of files.
The output folder is stored in a environment variable. The redirection is changed to use the variable insted of the path of the input file.
As the variables used to determine if the indicated token has been processed needs to be deleted for each file processed, the loop that processes the file contents is wrapped in a pair of setlocal/endlocal that cleans the flag variables after each file has been processed
read HELP FOR to learn how to use the FOR command to loop over the lines of a file and parse its contents. Then, try
for /f "tokens=1,*" %%a in (timedata.txt) do (
echo %%a ... %%b
)
you see that you may use %%a to split the files by date, so you could figure out something like
for /f "tokens=1,*" %%a in (timedata.txt) do (
echo %%b >>timedata.%%a.txt
)
or more generically
set fn=%~dpn1
set fx=%~x1
for /f "tokens=1,*" %%a in (%~1) do (
echo %%b >>%fn%.%%a%fx%
)

Batch - Recurse directories from a variable and expand results in another variable

I'm creating a simple production environment for work and in doing so need to set specific environment variables for specific projects in batch file.
Here's what i want to achieve:
1) Define a single environment variable which would define a list of directories
2) Recurse down each directory and add all leaf folders to a final environment variable.
[EDIT] After looking back at what i originally posted i was able to remove some redundancy. But the "The input line is too long." error occurs when %LINE% gets too long. Using the short path expansion does help but it can still error out. I'll look at how to break the echo to a temp file next as suggested.
Here's what i currently have:
#echo off
set RECURSE_THESE_DIRS=C:\Users\eric\Autodesk
set TMP_FILE=%CD%TMP_FILE.%RANDOM%.txt
setLocal EnableDelayedExpansion
for %%i in (%RECURSE_THESE_DIRS%) do (
if exist %%~si\NUL (
for /f "tokens=*" %%G in ('dir /b /s /a:d %%i') do set LIST=!LIST!;%%G
)
)
set LIST=%LIST:~1%
rem !!! AT THE ECHO LINE BELOW IF %LIST% IS TOO LONG, THIS SCRIPT FAILS
rem !!! WITH The input line is too long. ERROR :(
echo %LIST%>%TMP_FILE%
endlocal
for /F "delims=" %%G in (%TMP_FILE%) do set FINAL_VAR=%%G
del /F %TMP_FILE%
So by setting RECURSE_THESE_DIRS to directories i wish to parse, i end up with a %FINAL_VAR% which i can use to specify paths for proprietary software i use. Or i could use this script to append to %PATH%, etc...
This works for me but i would love suggestions to improve/streamline my script?
The root of your problem is that batch is limited to fit the variable name, contents and = into 8192 bytes, hence your directory-list simply isn't going to fit into one variable.
Personally, I'd just spit out a dir/s/b/a-d list to a tempfile and process that file with a for/f "delims=" - after all, you'd be likely to need to process your ;-separated envvar similarly in whatever process you are proposing to execute.
For instance, here's a test producing the same error - not using filenames at all
#ECHO OFF
SETLOCAL
SET "var=hello!1234"
SET var=%var%%var%%var%%var%%var%
SET var=%var%%var%%var%%var%%var%%var%%var%%var%
SET var=%var%%var%%var%%var%%var%
SET var=%var%%var%%var%%var%
SET count=8000
:loop
SET /a count +=1
ECHO %count%
SET var=%var%x
ECHO %var%
GOTO loop
GOTO :EOF
This should fail where count=8184.
Suggestions:
Use for /d /r to handle the recursion
Maybe i'm wrong, but in your script, you traverse the directory hierarchy, adding each directory to temp file which is then readed to concatenate its lines into a variable which is then writed to temp file, to be read again in another variable. If concatenation of directories fit into a variable, why not concatenate them without any temporary file?
If concatenation is in the limit of line length, as suggested by npocmaka, and if soported by end application, user short paths. Also, instead of adding the new values in the same instruction (not line) that contains the for loop, use a call to a subrutine with the new value passed to it, and then append the value. It's slower, but command lines executed will be shorter.

Modifying For loop variables cmd

Thing I want to do is to filter out specific video file extensions from a text file containing various video file names e.g filename.txt contents are
Red.mp4
Yellow.mp4
Blue.mp4
Orange.wmv
Purple.wmv
Now I will parse this file for .mp4 only & utilise 'for parameter' to make several .txt files each containing particular code for a particular file. In other words will create number of files same as number of .mp4 video in filename.txt My code
::==
#echo off
setLocal EnableDelayedExpansion
for /f "tokens=* delims= " %%a in ('type filename.txt ^|findstr ".mp4"') do (
set /a n+=1
echo >myfile!n!.txt
set v!n!=x264 --crf 23 --level 3.1 --tune film --o "%%a" "%%a.mkv"
)
set v
pause
::==
I have two problems:
How do I modify %%a to have its .mp4 string removed because when I apply "%%a.mkv" a file will be named *.mp4.mkv and I don't want that and .mp4 can be typed manually like --o "%%a.mp4" "%%a.mkv"
Provided that required txt files are already created how do I pass variables v1,v2,v3... to its respective text file I tried with
echo %v!n!% > >myfile!n!.txt
within loop but it didn't work, so plz advice
I don't see any need to create an array of variables with one loop, and then add the content to files in a 2nd loop. Simply do everything in one loop.
Also, there is no need to create an empty file, and your attempt to do so is incorrect - it adds the line ECHO is off. to each file. To echo a blank line you should use >filename ECHO(. To create an empty file use copy nul filename.
This is unlikely to be a concern, but it is possible for a filename to contain !. Expansion of a FOR variable containing ! will be corrupted if delayed expansion is enabled. The issue can be solved by toggling delayed expansion on and off within the loop, but there is another solution: use another FINDSTR command to prepend each line with the line number, and then parse the line number and file name with the FOR loop. This eliminates the need to increment a counter and eliminates the need for delayed expansion.
The ~n FOR variable modifier is used to get the name without the extension.
#echo off
for /f "tokens=1* delims=:" %%A in (
'findstr /lie ".mp4" filename.txt^|findstr /n "^"'
) do (
>"myfile%%A.txt" echo x264 --crf 23 --level 3.1 --tune film --o "%%B" "%%~nB.mkv"
)

Resources