how to prevent variable resolving (or to escape percent sign) in for loop in windows batch file? [duplicate] - variables

This question already has answers here:
Ignore percent sign in batch file
(6 answers)
Closed 5 years ago.
For example I have such loop that calls dir on a folder whose name contains percent signs so interpreter tries to resolve characters between these as a variable. Such folders are for example common in virtualizing solutions (for example ThinApp), that is data which would be stored in local user AppData is instead written to for example: X:\My Virtualized App\%AppData%.
And of course I know that it is possible to dir through it by doubling the %'s but it is not possible to convince interpreter to not resolve such variable in a for loop, for example:
FOR /F "tokens=*" %%F IN ('dir /b /s X:\myapp\%AppData% ') DO #(
echo %%F
)
Here no matter what I tried , doubling, quadrupling percents, or adding carets made no difference. The path passed to dir command has resolved appdata and thus is invalid having two drive specifications.

Short course in escaping.
#ECHO OFF &SETLOCAL
FOR /F "delims=" %%F IN ('echo X:\myapp\%AppData%') DO (
echo %%F
)
FOR /F "delims=" %%F IN ('echo X:\myapp\^^%%AppData^^%%') DO (
echo %%F
)
FOR /F "delims=" %%F IN ('echo "X:\myapp\^^%%AppData^^%%"') DO (
echo %%F
)
FOR /F "delims=" %%F IN ('echo ^^"X:\myapp\^%%AppData^%%^"') DO (
echo %%F
)
Output:
X:\myapp\C:\Users\User\AppData\Roaming
X:\myapp\%AppData%
"X:\myapp\^^%AppData^^%"
"X:\myapp\%AppData%"

try this:
pushd X:\myapp\%%AppData%%
FOR /F "tokens=*" %%F IN ('dir /b /s') DO #(
echo %%F
)
popd

setlocal
set "AppData="
FOR /F "tokens=*" %%F IN ('dir /b /s "X:\myapp\%%AppData%%"') DO #(
echo %%F
)
endlocal
or
FOR /F "tokens=*" %%F IN ('dir /b /s "X:\myapp\" ^| find /i "\%%appdata%%\"') DO #(
echo %%F
)

Related

How to fix renaming methods ? "The systems cannot find the file specified"

I've tried to rename file from someFile_v4 to someFile_v5 in all folders. Unfortunately I've get error The system cannot find the file specified.
#echo off
for /R %%f in (*_v4.xml) do call :ProcessFile
goto :Finished
:ProcessFile
rename "*_v4.xml" "*_v5.xml"
goto :eof
:Finished
The problem here is that you are using a for loop, which actually does nothing with the variable in this.
Try the following:
(
I also suggest you to consider changing:
for /r to for /f in using dir /s /b /a-d in the loop.
)
#echo off & setlocal EnableDelayedExpansion
for /f "tokens=* delims= " %%i in ('dir /s /b /a-d "*_v4.xml"') do (
set "_ren=%%~fi"
for /f "delims=" %%I in ('dir /w /b "%%~i"') do set "_to=%%~nxI"
ren "!_ren!" "!_to:_v4=_v5!"
set _ren=0<nul & set _to=0<nul
)
I cannot explain my code, sorry, because I don't know English well.

Extract line below Findstr criteria in Batch file for cycle

I'm trying to extract from a set of txt files the line
placed 2 rows below the one matching search criteria
and redirect output to a csv file.
I managed to do that for a specific txt file in the set,
but I'm getting troubles in writing the For Cycle
to batch-scan each txt in a given folder.
Through this, I wrote the following code to scan
a specific file. It works fine:
setlocal EnableDelayedExpansion
cd "myFolder"
if exist myOutput.csv del myOutput.csv
for /F "delims=:" %%A in ('findstr /B /N /C:"myCriteria" "myFile.txt"') do (
set /A LineBelow=%%A+2
set "LineBelow=!LineBelow!: "
)
(for /F "tokens=1* delims=:" %%A in ('findstr /N "^" "myFile.txt" ^| findstr /B "%LineBelow%"') do ^
echo %%B>>myOutput.csv)
start myOutput.csv
ENDLOCAL
When I tried to generalize the code in a For Cycle to scan each txt in myFolder
I got an error in Findstr: !LineBelow! happens to be an empty variable...
Here's the flawed For Cycle:
setlocal EnableDelayedExpansion
cd "myFolder"
if exist myOutput.csv del myOutput.csv
for %%F IN ("*.txt") do (
(
for /F "delims=:" %%A in ('findstr /B /N /C:"myCriteria" "%%F"') do (
set /A LineBelow=%%A+2
set "LineBelow=!LineBelow!: "
))
(for /F "tokens=1* delims=:" %%A in ('findstr /N "^" "%%F" ^| findstr /B "!LineBelow!"') do ^
echo %%B>>myOutput.csv))
start myOutput.csv
ENDLOCAL
Could anybody help me in correcting this code?
Thanks
#echo off
setlocal enableextensions enabledelayedexpansion
cd "myFolder"
> myOutput.csv (
for /f "tokens=1,2 delims=:" %%a in ('
findstr /b /n /c:"myCriteria" *.txt
') do (
set /a "line=%%b+2"
for /f "tokens=1,* delims=:" %%c in ('
findstr /n "^" "%%a" ^| findstr /b /c:"!line!:" 2^>nul
') do echo(%%d
)
)
This uses findstr to directly enumerate all the lines matching the myCriteria and include in the output the name of the file (%%a) and the number of the line (%%b). This information is used to retrieve the final line (%%d) from the matching files.

Skipping variable looks impossible

So I want to Echo %%a into another txt file, but seems nothing works to me.
Here's the code :
for /f "delims=" %%a in (
'dir "*.txt" /b /s /a-d'
) do Echo for /f "delims=" %%a in ^( >> "%%a"
Simple : output file looks like:
"for /f "delims=" C:\Documents and
Settings\xxxxx\Desktop\alpha\lol.txt in ( " .
I just need not a full path but "%%a". Bat file.
You want the literal %%a ?
Try %%%%a
Second suggestion:
Try %%%%^a
Third - change the metavariable (loop-control variable) to %%b
for /f "delims=" %%b in (
'dir "*.txt" /b /s /a-d'
) do Echo for /f "delims=" %%%%a in ^( >> "%%b"
Try this:
for /f "delims=" %%a in ('dir "*.txt" /b /s /a-d' ) do Echo %%a >> output.txt
As you saw, it's not directly possible to escape the %%a expression in a loop.
But like Magoo mention, it can be solved with changing the loop parameter.
You could also use the parser to create the %%a output.
setlocal EnableDelayedExpansion
set "static=for /f "delims=" %%%%a in ("
for /f "delims=" %%a in ( 'dir "*.txt" /b /s /a-d') do (
echo !static! >> "%%a"
)
The content of the variable static will be expanded, but the inner %%a will not expanded later.

Batch script to merge videos in directories

I need a batch script to list .avi|.mp4 and run a command for all directories that contain such files:
mencoder.exe <some_arguments> -o "output/(name_of_directory).mp4" <list_of_files_in_directory_spearated_by_spaces_evey_file_quoted>
Could anybody help me with that? Is it possible at all?
I made a script in PHP, compiled it with BamCompile, but it seems to act totally different on Windows 8, arrays are being converted to strings and something really weird is goin on...
try this:
#echo off &setlocal
cd /d "%userprofile%\Videos"
for /f "tokens=1*delims=:" %%a in ('dir /b /a-d *.avi *.mp4^|findstr /n $') do set "$%%a=%%~b"
setlocal enabledelayedexpansion
for /f "tokens=1*delims==" %%a in ('set "$" 2^>nul') do set "line=!line! "%%~b""
echo mencoder.exe [some_arguments] -o "output/(name_of_directory).mp4" %line%
Look at the output and remove echo if it looks good.
See if this floats your boat. ! characters are verboten in paths and filenames.
#echo off
setlocal enabledelayedexpansion
cd /d "%userprofile%\Videos"
for /f "delims=" %%a in ('dir /s /b /a-d *.avi *.mp4') do (
for /f "delims=" %%b in ("%%~dpa.") do (
set "line="
for %%c in ("%%~dpa*.*") do set line=!line! "%%c"
echo mencoder.exe [some_arguments] -o "output\%%~nxb.mp4" !line!
pause
)
)

batch rename directories from "NOTE" to "NOTES"

I would like to rename directories whose name contains the word "NOTE", but not "NOTES", to "NOTES". I first experiment with the echo command.
for /f "tokens=1-7" %%i in ('dir d:\mydirectory /s /b /ad ^|find "NOTE" ^|find "NOTES" /v') do #echo %%i %%j %%k %%l %%m %%n %%oS
Because directory names have different spaces in them, the above command may leave spaces between "NOTE" and S. Anyway to overcome this problem?
Give this a burl. If it echo's the right rename command then remove the echo and the pause to activate it. It's untested. Paths containing ! and % characters will cause an issue.
#echo off
setlocal enabledelayedexpansion
for /d /r %%a in (*) do (
set "f=%%~nxa"
if not "!f:NOTE=!"=="%%~nxa" (
if "!f:NOTES=!"=="%%~nxa" (
echo ren "%%a" "!f:NOTE=NOTES!"
pause
)
)
)