Batch file variable value not available until outside of loop - variables

I am trying to write a batch file that will search through a directory for *.pdf file extensions then convert them to *.tif file extensions with ImageMagic. I am able to do this if there is one PDF file in the directory, but if there are more than one I can't figure out how to convert them with the correct name. The problem is that within the loop, the fName variable doesn't appear to be assigned, but outside the loop, it has a value...
Here is the code that works for a single PDF file and works for multiple, but the name containts ".pdf":
echo off
set dSource=C:\Users\Nick\Documents\Research\Journal Article\Figures
set fType=*.pdf
for /f "delims=" %%f in ('dir /a-d /b "%dSource%\%fType%"') do (
rem remove extension from file name, set value to variable:
set fName=%%~nf
rem call ImageMagic to convert to TIFF
rem convert -compose copy -density 300 -alpha off "%%f" "%%f.tif"
rem above line (when uncommented) lets multiple TIFF images to be produced, but they are *.pdf.tif
rem convert -compose copy -density 300 -alpha off "%fName%.pdf" "%fName%.tif"
rem above line (when uncommented) does not work because fName has no value...
rem variable value does not appear to be assigned within loop:
echo.file name within loop: %fName%
)
echo.file name after loop: %fName%
rem outside loop, variable value is now available...
rem convert -compose copy -density 300 -alpha off "%fName%.pdf" "%fName%.tif"
rem above line of code works, but only for the last file name with *.pdf discovered in directory
pause

Enable delayed expansion if you need to set a variable and use it in the same loop, and then refer to the variable in the loop using ! instead of %:
#echo off
setlocal ENABLEDELAYEDEXPANSION
set dSource=C:\Users\Nick\Documents\Research\Journal Article\Figures
set fType=*.pdf
for /f "delims=" %%f in ('dir /a-d /b "%dSource%\%fType%"') do (
set fName=%%~nf
echo fName in the loop: !fName!
)
echo fName out of the loop: %fName%
Google "batch delayed expansion" for details.

In your case a solution is to replace this:
convert -compose copy -density 300 -alpha off "%%f" "%%f.tif"
with this:
convert -compose copy -density 300 -alpha off "%%f" "%%~dpnf.tif"
rem or "%%~nf.tif" to save them all in the current folder.
The drawback with delayed expansion is that a ! character becomes more difficult to process in a filename or path or string.

Related

CMD Set only reading first letter in text file

Using the following command Set /p out=<out.txt I do not get the text in the file, what is returned is just a symbol and the first letter in the file.
I've tried using Set out=more out.txt but that ends up just placing the command "more out.txt" into the script instead of what's in the text file.
https://i.stack.imgur.com/yU9A1.jpg
It looks that the out.txt is saved using UTF-16 encoding with BOM. Unlike type command, the < redirection operator does not understand this. The following cmd command should do the trick:
set out=&for /f "delims=" %G in ('type out.txt') do #if not defined out set out=%G
To use the FOR command in a batch script (.bat or .cmd extension), specify %%G instead
of %G as follows:
#ECHO OFF
set "out="
for /f "delims=" %%G in ('type out.txt') do if not defined out set out=%%G

Rename pdf files and create & move files basis filename

I have 5000 .pdf files which I have to rename and to move into newly created folders, whose names are based on the original file names.
For example: original name is crf--aaa--208912--2089120010.
The folder name should be 208912 and the file name 2089120010.
Please help with the process to do so.
You might want to thy the following code snippet:
rem // Change to directory containing the files:
pushd "D:\Data" && (
rem // Retrieve matching files and iterate through them:
for /F "delims=" %%F in ('dir /B /A:-D "*--*--*--*.pdf"') do (
rem // Store name of currently iterated file:
set "NAME=%%F"
setlocal EnableDelayedExpansion
rem /* Split name into tokens separated by `--`; since `for /F` uses characters
rem as delimiters but not strings, `--` is replaced by `|` intermittently: */
for /F "tokens=3* delims=|" %%I in ("!NAME:--=|!") do (
endlocal
rem // Create sub-directory:
mkdir "%%I"
rem // Rename and move file:
move "%%F" "%%I\%%J"
)
endlocal
)
rem // Restore previous working directory:
popd
)

CMD: extract part of a filename and use it as variable

I ripped my CDs with EAC to CUE+WAV files, added cover art and all my files are in the same folder with filenames pattern "Album artist - Album title", for ex.:
Clannad - Legend.wav/cue/jpg
David Bowie - Best Of Bowie [Disc 1].wav/cue/jpg
David Bowie - Best Of Bowie [Disc 2].wav/cue/jpg
I'm new to this so I wrote a simple CMD batch to convert my music to FLAC format, but it requires manual copying and pasting of the actual wav/cue/jpg filenames and input of album artist, disc no. and total disc no. for their corresponding tags. It cannot be stored in cuesheet file for some reason, but in my case I have them in filenames as you can see above).
ECHO WAV/CUE/JPG FILENAME
SET /P "input="
ECHO ALBUMARTIST
SET /P "albumartist="
ECHO DISCNUMBER
SET /P "discnumber="
ECHO TOTALDISCS
SET /P "totaldiscs="
flac.exe -0 --picture="D:\Music\%input%.jpg" --tag-from-file="CUESHEET=D:\Music\%input%.cue" -T "ALBUMARTIST=%albumartist%" -T "DISCNUMBER=%discnumber%" -T "TOTALDISCS=%totaldiscs%" "D:\Music\%input%.wav"
My question is about automation of converting all my ripped albums. How can I extract album artist/disc no./total disc no. info from filenames and loop that for every .wav file?
#ECHO OFF
SETLOCAL
SET "destdir=U:\destdir"
PUSHD "%destdir%"
:: Find all .jpgs where there is a .wav and .cue with the same name
FOR /f "delims=" %%a IN ('dir /b /a-d *.jpg') DO IF EXIST "%%~na.wav" IF EXIST "%%~na.cue" (
FOR %%b IN (input albumartist discnumber totaldiscs) DO SET "%%b="
SET "input=%%~na"
FOR /f "delims=-" %%b IN ("%%a") DO SET "albumartist=%%b"
FOR /f "tokens=2delims=[]" %%b IN ("%%a") DO SET "disc=%%b"
IF DEFINED disc (
FOR /f "tokens=1delims=[]" %%d IN ("%%a") DO FOR /f %%c IN ('dir /b "%%d[*.wav"') DO SET /a totaldiscs+=1
)
CALL :gflac
)
POPD
GOTO :EOF
:gflac
:: remove trailing spaces from INPUT
IF "%albumartist:~-1%"==" " SET "albumartist=%albumartist:~0,-1%"&GOTO gflac
:: presume default for disc and totaldiscs
IF DEFINED disc (FOR /f "tokens=2" %%d IN ("%disc%") DO SET /a disc=%%d) ELSE (SET /a disc=1)
IF NOT DEFINED totaldiscs SET /a totaldiscs=1
ECHO( flac.exe -0 --picture="D:\Music\%input%.jpg" --tag-from-file="CUESHEET=D:\Music\%input%.cue" -T "ALBUMARTIST=%albumartist%" -T "DISCNUMBER=%discnumber%" -T "TOTALDISCS=%totaldiscs%" "D:\Music\%input%.wav"
GOTO :eof
You would need to change the setting of destdir to suit your circumstances.
The above will merely echo the required flac line. I left it as you posted but set the scant test data you posted up as I interpret it (ie. there is a set of 3 files) on my U: drive.
Sadly, you've given us insufficient information. I've assumed that you need all three files to be present, and the default for totaldiscs is 1.
First, look for all .jpgs, and if there is a corresponding .wav and .cue then process for flac generation as follows:
Set input to the name part of the .jpg found
set albumartist to the first part of the filename, up to the -
get the [disc n] string if it's present
count the number of .wavs that start with the filename up to the [
generate the flac line.
Within the generation of the flac line, we strip off the trailing spaces from input, convert disc n to n or set disc to 1 (although this information may not be needed), and set the totaldiscs to 1 if it's not been calculated.
You don't say what flac produces as output, but I'd suggest that you further gate that filetype so that the procedure doesn't run if the %input%.finalproductwhateverthatis file is present.
[edited per dbenham's comments]
This is very similar to Magoo's answer, with some bug fixes, and everything is done in one master loop without a CALL. As with Magoo's answer, modify your destination folder at the beginning to suit your needs.
#echo off
:: Delayed expansion must be disabled to protect ! when expanding FOR variables.
:: It is normally disabled by default, but I'm making it explicit, just to be sure.
setlocal disableDelayedExpansion
:: Define where source files are coming from
set "source=D:\Music"
:: Define where output should be stored
set "destination=D:\Music"
pushd "%destination%"
:: Iterate each .wav file (A) and only proceed if .jpg and .cue also exists
for /f "delims=" %%A in ('dir /b /a-d "%source%\*.wav"') do if exist "%source%\%%~nA.jpg" if exist "%source%\%%~nA.cue" (
%= Get base name with path, but without extension =%
set "file=%source%\%%~nA"
%= Extract "artist - album " (B) and "Disc #" (C) from base name (~nA) =%
for /f "delims=[] tokens=1,2" %%B in ("%%~nA") do (
%= Extract "artst " (D) from "artist - abum ". (~nxD) trims trailing space =%
for /f "delims=-" %%D in ("%%B") do set "artist=%%~nxD"
%= Extract the number (E) from "Disc #", use 1 as default if not there =%
set "disc=1"
for /f "tokens=2" %%E in ("%%C") do set "disc=%%E"
%= Count the number of discs (F), will be 0 if no [Disc #] =%
%= The [ is appended to name to prevent something like "Greatest Hits 2" from matching "Greatist Hits" =%
for /f %%F in ('dir /b /a-d "%source%\%%B[*.wav" 2^>nul ^|find /c /v ""') do set "count=%%F"
%= temporarily enable delayed expansion to access variables set within loop =%
setlocal enableDelayedExpansion
%= Set count to 1 if no [Disc #] =%
if !count! equ 0 set /a count=1
flac.exe -0 --picture="!file!.jpg" --tag-from-file="CUESHEET=!file!.cue" -T "ALBUMARTIST=!artist!" -T "DISCNUMBER=!disc!" -T "TOTALDISCS=!count!" "!file!.wav"
%= pop the setlocal stack to get back to state at beginning of loop =%
endlocal
)
)
popd
You may want to add a check to only proceed if the FLAC file does not already exist so you can run the script multiple times without reprocessing files. The outer loop would look something like this, but I can't be sure since I don't know the format of the output file name:
for /f "delims=" %%A in ('dir /b /a-d "%source%\*.wav"') do if exist "%source%\%%~nA.jpg" if exist "%source%\%%~nA.cue" if not exist "%destination%\%%~nA.flac" (
The logic is much simpler using my JREPL.BAT utility if you understand regular expressions:
#echo off
:: Delayed expansion must be disabled to protect ! when expanding FOR variables.
:: It is normally disabled by default, but I'm making it explicit, just to be sure.
setlocal disableDelayedExpansion
:: Define where source files are coming from
set "source=D:\music"
:: Define where output should be stored
set "destination=D:\music"
pushd "%destination%"
:: Iterate all *.wav and use JREPL to format result as "fullFileName|artist - album|artist|Disc#"
:: %%A %%B %%C %%D
:: Only proceed if .jpg and .cue also exist
for /f "tokens=1-4 delims=|" %%A in (
'dir /b /a-d "%source%\*.wav"^|jrepl "^((.+?) - .+?)(?:\[Disc (\d+)])?\.wav$" "$&|$1|$2|$3" /i'
) do if exist "%%~nA.jpg" if exist "%%~nA.cue" (
%= disc and count are both 1 if %%D is empty =%
if "%%D" equ "" (
flac.exe -0 --picture="%source%\%%~nA.jpg" --tag-from-file="CUESHEET=%source%\%%~nA.cue" -T "ALBUMARTIST=%%C" -T "DISCNUMBER=1" -T "TOTALDISCS=1" "%source%\%%A"
%= else count the number of .wav files =%
) else for /f %%E in ('dir /b /a-d "%%B[*.wav"^|find /c /v ""') do (
flac.exe -0 --picture="%source%\%%~nA.jpg" --tag-from-file="CUESHEET=%source%\%%~nA.cue" -T "ALBUMARTIST=%%C" -T "DISCNUMBER=%%D" -T "TOTALDISCS=%%E" "%source%\%%A"
)
)
popd
Again, you can add an IF to the outer loop to proceed only if the .flac file does not already exist in the destination.

Findstr, how to read part of string/line

I have file with a list of names and extensions, formatted such as below each on it's own line:
JoeBloggs=102
JohnSmith=109
What I want to do is use findstr but read the number after the equals sign. So I am using the following command:
#echo off
for /F "delims=" %%a in ('findstr /p %username% extensions.txt') do set ext=%%a
If a user logs on as JoeBloggs it will capture JoeBloggs=102, what I want it to do is only capture 102. So essentially only the numbers after the equals sign.
#echo off
for /f "tokens=1,2 delims==" %%a in (names.txt) do (
if "%%a"=="%username%" set ext=%%b
)
echo %ext%
pause >nul
This will read each line of your text file and split it when it comes across an = sign.
I have specified to use tokens 1 and 2, 1 being before the split, and 2 being after so we can compare the first and if it's what you want, use the second.

concatenate multiple variable's names in batch/CMD

searched this site and others - no joy.
:: first for loop
for /L %%x in (1,1,2) do (
generic-executable.output > grab1-%%x.txt
:: second, nested for loop
for /f "delims=" %%i in (grab1-%%x.txt) do (set grab1=%%i)
echo variable string is %grab1%%x%
generic-executable.output > grab2-%%x.txt
for /f "delims=" %%i in (grab2-%%x.txt) do (set grab2=%%i)
echo variable string is %grab2%%x%
)
Trying to run a nested for loop that will
1) write data to the file
2) take data from the file and save it to another variable.
The names of the end variables should be a concatenation of each of the for loops (i.e. grab1-1, 2-1, 1-2, 2-2).
Saving the data to the variables is no problem, formatting the variables to recall the data IS.
I'm most likely missing something in the formatting of the concatenated variable. I've tried single ', double ", ^, !, one %, two %, backslash, ACK!! ... the closest I've gotten is
echo %grab1-%%x
gave:
%grab1-1
I'd appreciate any tips you can provide.
Thanks,
Dave
You have run into a classic stumbling block for batch newbies: You cannot set a variable within a loop (within parentheses) and then access the value using %var% within the same loop. The Entire loop (parenthesized block of code) is parsed in one pass, and %var% is expanded at parse time. So you see the value of var as it was prior to the loop executing.
The solution is to enable delayed expansion using setlocal enableDelayedExpansion near the top of your script, and then expand the variable using delayed expansion as !var!. Delayed expansion means the value is expanded at run time - exactly what you want.
I believe this is what you were trying to achieve
setlocal enableDelayedExpansion
for /L %%x in (1,1,2) do (
genericOutput1.exe > grab1-%%x.txt
for /f "delims=" %%i in (grab1-%%x.txt) do set "grab1-%%x=%%i"
echo grab1-%%x variable string is !grab1-%%x!
genericOutput2.exe > grab2-%%x.txt
for /f "delims=" %%i in (grab2-%%x.txt) do set "grab2-%%x=%%i"
echo grab2-%%x variable string is !grab2-%%x!
)
::List all of the grab variable defined
set grab
You don't need to save the output of your executables to a file. (Unless of course that is your requirement). You can use FOR /F to process the output directly. The FOR command has many variants that look nearly identical, yet behave very differently. This variant uses single quotes to cause FOR /F to process a command.
setlocal enableDelayedExpansion
for /L %%x in (1,1,2) do (
for /f "delims=" %%i in ('genericOutput1.exe') do set "grab1-%%x=%%i"
echo grab1-%%x variable string is !grab1-%%x!
for /f "delims=" %%i in ('genericOutput1.exe') do set "grab2-%%x=%%i"
echo grab2-%%x variable string is !grab2-%%x!
)
::List all of the grab variable defined
set grab
Note that FOR /F will iterate each line (whether it be from a text file or from command output). Your algorithm will only save and print the content of the last line - each successive line will overwrite the value from the prior line.