I have a script that I've put together that should copy the list of files to a variable but the only thing I receive is the last file. In other words, when I echo the variable in my for loop, I see 20 or so files but only the last one gets copied to my variable. How can I get them all to copy correctly?
I am using Windows 7.
#echo off
setlocal enabledelayedexpansion enableextensions
for /r %%x in (*) do (
echo %%x
SET PATH_VALUE=%%x;%PATH_VALUE%
)
One way is to use delayed expansion. You've enabled it – half the job done. Now you only want to use it. Replace the %s around PATH_VALUE with !s and you are done:
#echo off
setlocal enabledelayedexpansion enableextensions
for /r %%x in (*) do (
echo %%x
SET PATH_VALUE=%%x;!PATH_VALUE!
)
Related
I want to replace a variable's substring, previously stored on a variable inside a for loop, I tried to do it like this but it didn't work:
setlocal EnableDelayedExpansion
set checkVar=abcd
FOR %%Y IN (*.pdf) DO (
SET meu=%%Y
CALL SET meuWithoutChar=!meu:%%%checkVar%%%=!
ECHO meuWithoutChar=!meuWithoutChar!
)
For example here if %%Y==blepabcdnnnn.pdf; I want to have meuWithoutChar=blepnnnn.pdf on the output
Thank you in advance
You are bit confused on the concept of delayed expansion and the use of CALL to get an extra phase of expansion. Here are the examples. I am just using your single file example. You can change it back to using the wildcard.
CALL example
#echo off
set checkVar=abcd
FOR %%Y IN (blepabcdnnnn.pdf) DO (
SET "meu=%%Y"
CALL SET "meuWithoutChar=%%meu:%checkVar%=%%"
CALL ECHO meuWithoutChar=%%meuWithoutChar%%
)
pause
Delayed Expansion
#echo off
setlocal Enabledelayedexpansion
set checkVar=abcd
FOR %%Y IN (blepabcdnnnn.pdf) DO (
SET "meu=%%Y"
SET "meuWithoutChar=!meu:%checkVar%=!"
ECHO meuWithoutChar=!meuWithoutChar!
)
pause
As a supplement/extension to Squashmans answer.Only cycles through necessary files and ignores the file's extension.
Without delayed expansion:
#Echo Off
SetLocal DisableDelayedExpansion
Set "strChr=abcd"
For %%A In ("*%strChr%*.pdf") Do (Set "objFileName=%%~nA"
Call Set "objNewFile=%%objFileName:%strChr%=%%%%~xA"
Call Echo %%%%objNewFile%%%%=%%objNewFile%%)
Pause
With full script delayed expansion, (will have issues with filenames containing !'s):
#Echo Off
SetLocal EnableDelayedExpansion
Set "strChr=abcd"
For %%A In ("*%strChr%*.pdf") Do (Set "objFileName=%%~nA"
Set "objNewFile=!objFileName:%strChr%=!%%~xA"
Echo %%objNewFile%%=!objNewFile!)
Pause
With toggled delayed expansion, (protects filenames containing !'s):
#Echo Off
SetLocal DisableDelayedExpansion
Set "strChr=abcd"
For %%A In ("*%strChr%*.pdf") Do (Set "objFileName=%%~nA"
SetLocal EnableDelayedExpansion
Set "objNewFile=!objFileName:%strChr%=!%%~xA"
Echo %%objNewFile%%=!objNewFile!
EndLocal)
Pause
I recently started converting all my video files to MP4 for streaming purposes, and was using batch so far without issues.
Now I have 6000+ videos to convert and needed some sort of queue system I could manipulate.
This is my current code to convert everything from directories into commands in a separate folder.
#echo off
setlocal disabledelayedexpansion
set dirs=D:\ANIME E:\ANIME F:\ANIME
for %%a in (%dirs%) do (
echo %%a
pushd %%a
for /f "tokens=* delims= " %%f IN ('dir/b/s *.avi *.mkv *.m4v *.mpeg *.mpg') do (
echo D:\handbrake\HandBrakeCLI.exe -i "%%f" -e qsv_h264 -q 20 -E fdk_haac -B 196 -R Auto -D 0,0,0,0 --audio-copy-mask aac,ac3,dtshd,dts,mp3 --audio-fallback copy:* --subtitle=1 --subtitle-default --subtitle-forced --subtitle-burn -o "%%~dpf%%~nf.mp4" > "D:\batch\%%~nf.bat"
echo del /F "%%f" >> "D:\batch\%%~nf.bat"
)
)
This works great and makes me 1000's of bat files to call and that is where the issue arises.
A lot of files have special characters like ! in their names which messes up the for loop I use to loop through these. I set the file to read-only before processing, so my other batch won't try to convert the same file (I run 3 at once).
I found a workaround using ENABLEDELAYEXPANSION, but after a few loops I will get the error saying I exceeded the maximum calls for that.
#echo off
setlocal enableextensions
echo Starting loop
goto loop
:loop
for %%f in (D:\batch\*.bat) do (
set ATTRIBS=%%~af
setlocal enabledelayedexpansion
set READ_ATTRIB=!ATTRIBS:~1,1!
if !READ_ATTRIB!==- (
setlocal disabledelayedexpansion
attrib +R "%%f"
call "%%f"
)
timeout /t 2 /nobreak
goto loop
)
endlocal
Normally you'd say just change the ! variables to % and remove delayedexpansion, but that gives me errors in getting the file attributes.
set READ_ATTRIB=%ATTRIBS:~1,1%
if %READ_ATTRIB%==- (
Will give me no result and the error ( is unexpected
If I echo READ_ATTRIB the result will be ~1,1 always and when echoing others I will get ECHO IS OFF.
Is there any way to not use delayedexpansion or in a way that I won't get errors after a few loops?
32 is the deepest level of setlocal when used in batch file. You need to use setlocal - endlocal pair analogously to a pair of ( left and ) right parentheses. See capitalized ENDLOCAL below.
#echo off
setlocal enableextensions
echo Starting loop
goto loop
:loop
for %%f in (D:\batch\*.bat) do (
set ATTRIBS=%%~af
setlocal enabledelayedexpansion
set READ_ATTRIB=!ATTRIBS:~1,1!
if !READ_ATTRIB!==- (
setlocal disabledelayedexpansion
attrib +R "%%f"
call "%%f"
ENDLOCAL
)
ENDLOCAL
timeout /t 2 /nobreak
REM this was totally mazy: goto loop
)
endlocal
Moreover, latter goto loop in your original code causes that for %%f loop is initialised over and over indefinitely against the same data set, i.e. %%f in the loop body refers to the only file all the time. See capitalized REM.
I have a batch file to Find and replace text string in a file with a portion of the its own file-name in multiple files within a folder using windows Batch script but it does not work and simply replace YYY with null or nothing. Any help appreciated. thank you
#echo off
SETLOCAL
SET stringtofindreplace=YYY
for %%f in (*.fmw) do (
#echo Processing %%f...
fOR /F "delims=" %%l IN (%%f) DO (
SET "line=%%l"
SET fname=%%~nf
SET fname=!fname:~6,3!
SETLOCAL ENABLEDELAYEDEXPANSION
set "x=!line:%stringtofindreplace%=%fname%!"
echo(!x!
ENDLOCAL)
)>%%~nf.txt
)
GOTO:EOF
here is updated code that still does not work
#echo off
SETLOCAL
SET stringtofindreplace=YYY
for %%f in (*.fmw) do (
#echo Processing %%f...
(
fOR /F "delims=" %%l IN (%%f) DO (
SET "line=%%l"
SET fname=%%~nf
SETLOCAL ENABLEDELAYEDEXPANSION
SET fname=!fname:~6,3!
SET "x=!line:%stringtofindreplace%=%fname%!"
echo(!x!
ENDLOCAL
)
)>%%~nf.txt
)
GOTO:EOF
You have a number of problems.
1) Your name substring computation attempts to use delayed expansion within your loop before you enable it.
2) The computed replacement string cannot be expanded using normal expansion. You need delayed expansion there as well. But you cannot use delayed expansion within delayed expansion. The trick is to transfer the inner value to a FOR variable.
3) You have got an extra, unbalanced ) after ENDLOCAL. I don't think it is causing any problems with the code you have posted, but you probably should remove it.
You are also computing the name substring once for every line, when it really only need be done once per file. This isn't a bug, but it is inefficient.
Here is corrected code.
#echo off
setlocal
set stringtofindreplace=YYY
for %%f in (*.fmw) do (
#echo Processing %%f...
setlocal enableDelayedExpansion
set "fname=%%~nf"
for /f "eol=: delims=" %%A in ("!fname:~6,3!") do (
endlocal
for /f "delims=" %%l IN (%%f) do (
set "line=%%l"
setlocal enableDelayedExpansion
set "x=!line:%stringtofindreplace%=%%A!"
echo(!x!
endlocal
)
)>"%%~nf.txt"
)
You still could have problems if any lines begin with ;. Also, empty lines will be stripped. Both limitations could be solved with a bit more code.
But even if you fix the limitations, it will be quite slow. It could be painfully slow if any files are large.
The code is much simpler, faster, and more reliable if you use my hybrid JScript/batch REPL.BAT utility:
#echo off
setlocal
set stringtofindreplace=YYY
for %%f in (*.fmw) do (
#echo Processing %%f...
setlocal enableDelayedExpansion
set "fname=%%~nf"
type "!fname!.fmw"|repl "%stringtofindreplace%" "!fname:~6,3!" li >"!fname!.txt"
endlocal
)
I need to make a batch file that can read a text file and I am using windows 7 ultimate 32 bit
I am currently using this code:
#ECHO OFF
for /f "delims=" %%x in (test.txt) do set "Var=%%x"
ECHO %Var%
pause
But this only reads the first line and I need it to read the whole thing. I need it to display the full text file.
Try this:
#ECHO OFF
SetLocal EnableDelayedExpansion
for /f "delims=" %%x in ('type test.txt') do (
set "Var=%%x"
ECHO !Var!
)
pause
You need to enclose the for loop with brackets if you are performing multiple commands inside the for loop. Besides that, SetLocal EnableDelayedExpansion will helps to expand the variable at execution time rather than at parse time.
Hope this helps.
Actually, this should read the whole file and set Var to the last line.
If you need to process the whole file, you have several options:
If you just need to do something for every line, then just use a block instead of the set "Var=%%x":
for /f "delims=" %%x in (test.txt) do (
rem Do something with %%x
)
If you need the complete file line-by-line in memory, use a counter and emulate arrays with lots of variables:
setlocal enabledelayedexpansion
set cnt=0
for /f "delims=" %%x in (test.txt) do (
set "ine[!cnt!]=%%x"
set /a cnt+=1
)
set /a numlines=cnt-1
and afterwards you can just use a for /l loop to access them again.
I have a long list of files that I want to merge into few files.
part-m-00000
part-m-00001
part-m-00002
part-m-00003
part-m-00004
part-m-00005
part-m-00006
part-m-00007
part-m-00008
part-m-00009
part-m-00010
part-m-00011
part-m-00012
...
part-m-01267
(Notice the padding of number at the end of the file name)
I want to merge every 100 files into 10 individual files and the last remaining 67 into the 11th. I'm having trouble padding those numbers to merge the files.
Here's what I was able to get to -
#echo off
if exist merge.txt del merge.txt
echo. >merge.txt
for /l %%a in (0,1,3) do (
Set Number=00000%%a
Set Number=%Number:~-5%
copy/b merge.txt+"part-m-%number%.txt" merge.txt
)
I don't think the padding of numbers is working as expected. It just doesn't work for me in the for loop. If I do something like this in general -
Set Number=768
Set Number=00000%Number%
Set Number=%Number:~-5%
echo %Number%
it correctly pads the number.
I cant figure out what I'm doing wrong. Appreciate any help.
The variables with the %XXX% are expanded only once for the for loop, not in every iteration.
You need to use delayed expansion with the !XXX! syntax like this:
setlocal enableextensions enabledelayedexpansion
for /l %%a in (0,1,3) do (
Set Number=00000%%a
Set Number=!Number:~-5!
echo !Number!
)
For more details refer to question Batch file variables initialized in a for loop.
Try this:
#echo off
setlocal enabledelayedexpansion
if exist merge.txt del merge.txt
echo. >merge.txt
for /l %%a in (0,1,3) do (
Set Number=00000%%a
Set Number=!Number:~-5!
copy /b merge.txt+"part-m-%number%.txt" merge.txt
)
If you do not use delayed expansion, the variable %Number:~-5% will be expanded only once - before the whole for command is processed. With delayed expansion used, the variable !Number:~-5! will be expanded each time it is run, so the number will be different each time.
Another (perhaps simpler and faster) approach is this:
#echo off
setlocal enabledelayedexpansion
if exist merge.txt del merge.txt
echo. >merge.txt
set Number=99999
for /l %%a in (0,1,3) do (
Set /A Number+=1
copy /b merge.txt+"part-m-!Number:~-5!.txt" merge.txt
)