for statement will not run in batch script [duplicate] - for-loop

This question already has an answer here:
for loop working in CMD prompt but not in batch file - for loop was copy pasted
(1 answer)
Closed 3 years ago.
I'm kinda a bit of a noob to this stuff. I'm trying to make a batch script to convert Backup Exec 10 xml log files to text.
I got some great answers from here when searching, cooked up a script and have tried to run it. It bombs at the first "for" statement and I'm not sure why.
I can run every step of this script manually and it works great. But if I double click on the batch file and run it, after the second pause it bombs.
Anyone see anything out of place here? I'm at my wits end. I think the "for" statement might need tweaking. I've been messing around but haven't found the right combo for it to run successfully in the script.
#echo off
echo Starting Backup To Text SCript...
pause
cd c:\Program Files\Symantec\Backup Exec\Data
echo Get the latest BEX_TAPEBACKUP File...
pause
FOR /F "usebackq" %f in (`dir /Od /B`) do set "FILE=%f"
echo %FILE% will be converted to text
pause
cd c:\Program Files\Symantec\Backup Exec
REM This command will take the current Backup Exec XML log file and convert it to text.
pause
bemcmd -o31 -l"C:\Users\Administrator\Desktop\Backup Logs\backuplog.txt" s0 - f"C:\Program Files\Symantec\Backup Exec\Data\%FILE%"
pause
echo Done

Use the right syntax for your for loop:
FOR /F "delims=" %%f in ('dir /od /B') do set "FILE=%%~f"

You need to double your %'s in the For.
FOR /F "usebackq" %%f in (`dir /Od /B`) do set "FILE=%%f"

Related

Well-tried and tested Windows batch file stops at for loop since today - what could be the cause? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 18 days ago.
Improve this question
I have written and used a windows batch file that has been working without a problem for quite a while.
This is what it does:
The program loops through the *.pdf files of the current folder, and then performs a few tasks like extracting the text layer of the pdf file, searching for a couple of search terms and in the end it renames the pdf file according to which search term was found.
The batch file worked fine until today.
Now it just stops working at the for loop and I have no idea why.
Stripped down to the essence, the batch looks like this:
setlocal enabledelayedexpansion
for /f "delims=" %%i in ('dir /b /a-d /o-d *.pdf') do (
echo %%i
echo %%~ti
echo %%~ni
)
The echos would just be examples, but as the batch files never reaches what's after the for loop, I simplified it.
Since today, the program would just stop when it reaches the for loop.
This is what I tried to solve the problem:
examined the syntax of the for loop, found no mistakes
tried simplified variations of the for /f loop including leaving out the delims option, changing it against a usebackq option, changing the name of the loop variable, stripping down the options of the dir command, changing the file filter to *.txt
turned the for loop into a one-liner with just an echo %%i command etc.
looped through the lines of a given text file
changed the location of the batch file to another folder
tried the batch on a Windows 10 and a Windows 7 system
try again after rebooting the system
checked folder access rights, I had all of them, no restrictions
None of these helped, the batch file would always stop and close its window right at the for loop.
I also inserted the dir command before the for loop just to check the syntax - this worked with no problem, so it can't be the syntax of the dir command either.
I remember that I had this problem a few times years ago and it either disappeared by itself or I had to retype the batch word for word in a new text file copying whatever was in the original file as copy and paste wouldn't work.
Has anyone out there observed this strange behaviour and maybe found the cause and at best a solution for it? I'd be so thrilled.
EDIT: When I run the batch in the cmd window, I get a simple SYNTAX ERROR when it reaches the line with the for loop.
Try this at the command prompt:
reg query "HKEY_CURRENT_USER\Software\Microsoft\Command Processor" /v AutoRun
reg query "HKEY_LOCAL_MACHINE\\Software\Microsoft\Command Processor" /v AutoRun
Both results should be empty, but probably in your case they are set.
The problem of the AutoRun file, it's started when a new cmd instance is started, but also for the FOR/F command.
If this is the problem, you should delete these entries by
reg DELETE "HKEY_CURRENT_USER\Software\Microsoft\Command Processor" /v AutoRun /f
reg DELETE "HKEY_LOCAL_MACHINE\\Software\Microsoft\Command Processor" /v AutoRun /f
Or if you really need an AutoRun batch, then it should start with a guard.
#echo off
setlocal EnableDelayedExpansion
REM *** ALWAYS make a copy of the complete CMDCMDLINE, else you destroy the originial!!!
set "_ccl_=!cmdcmdline!"
REM *** The check is necessary to distinguish between a new cmd.exe instance for a user or for a "FOR /F" sub-command
if "!_ccl_:~1,-2!" == "!comspec!" (
REM ***** INTERACTIVE ****
REM *** Do your stuff ***
)
endlocal
exit /b

How do I format Tesseract-OCR language settings within a .bat file's "for" command

I'm new to Windows cmd and .bat, and to Tesseract. But thanks to this list I've managed a couple of successes.
My first success was this cmd-window line:
tesseract.exe -l eng+lat+ita D:\TIFs\Convivio.tiff D:\TIFs\Convivio
My next success was the .bat file:
:Start
#Echo off
ECHO.
ECHO This is a batch file
ECHO.
PAUSE
BREAK=ON
Set _SourcePath=D:\temp\TIFs\*.tif
Set _OutputPath=D:\temp\TIFs\
Set _Tesseract="D:\temp\Tesseract-OCR\tesseract.exe"
:Convert
For %%A in (%_SourcePath%) Do Echo Converting "%%A"...... &"D:\temp\Tesseract-OCR\tesseract.exe" "%%A" "%_OutputPath%%%~nA"
PAUSE
:End
Set "_SourcePath="
Set "_OutputPath="
Set "_Tesseract="
The problem now is how to include in the .bat file that"-l eng+lat+ita" bit from the cmd-window line.
I got the idea that this is possible from an explanation of the "For" command, which states that "do command" can be followed by "CommandLineOptions" (i.e., "-l eng+lat+ita").
Any help would be much appreciated... 'cause I've been banging my head on this one for hours now...
UPDATE: Found an alternative, but still would like an answer to my question.
I didn't know that "FOR" commands could be run from cmd. So, I pasted the folllowing line in the cmd window:
for %i in (*.tif) do "D:\temp\Tesseract-OCR\tesseract.exe" -l eng+lat+ita "%i" "D:\temp\%~ni"
And, it worked!
As I say, though, how to do this with a .bat file?
#ECHO OFF
SETLOCAL
:Start
#Echo off
ECHO.
ECHO This is a batch file
ECHO.
PAUSE
BREAK=ON
Set "_SourcePath=D:\temp\TIFs\*.tif"
Set "_OutputPath=D:\temp\TIFs"
Set "_Tesseract=D:\temp\Tesseract-OCR\tesseract.exe"
:Convert
For %%A in ("%_SourcePath%") Do Echo Converting "%%A"...... &"%_Tesseract%" -l eng+lat+ita "%%A" "%_OutputPath%\%%~nA"
PAUSE
:End
rem Set "_SourcePath="
rem Set "_OutputPath="
rem Set "_Tesseract="
GOTO :EOF
Since I don't have the tesseract utility, I used another. The above worked for me as I expected with that other utility, so no guarantees with tesseract.
It's normal practice to start a batch with setlocal which makes the clean-up effort unnecessary (hence remmed-out) since an implicit endlocal is executed when the batch terminates, restoring the environment to its initial state.
Assigning values containing quotes is valid but awkward when combining elements. Ditto terminating a value with a backslash. I've converted your code to my preferred syntax. Note that the syntax SET "var=value" (where value may be empty) is used to ensure that any stray trailing spaces are NOT included in the value assigned.
Will it work in your situation? Over to you to try.

bulk renaming files using batch file

I have a bunch of files (and more coming) that are downloaded from the internet (pdf printed) but they do come with a lot of names that I do not need/want; so I want to have a text file with the things that I want to remove. example
I have a text file with this two lines
Chapter 13. Text and Regular Expressions - Master-PowerShell _ With Dr. Tobias Weltner - PowerShell.com
- PowerShell Scripts, Tips, Forums, and Resources.htm
I want to keep adding lines of text to that file and then call it from another batch file to remove all the lines that are in this file, I got something working that I took from foxidrive here
I could keep adding strings to the rename, but I'm wondering if I could get all the lines to remove from a text file, and I know that there are programs out there (free ones) to do that, but I'd rather use the good old DOS batch.
Thanks
Here is my code
#echo off
if Exist %CD%/temp.txt del %CD%/temp.txt
echo #echo off >%CD%/temp.txt
echo for /f "delims=" %%%%a in ('dir /a:-d /o:n /b') do call :next "%%%%a" >>%CD%/temp.txt
echo GOTO:EOF >>%CD%/temp.txt
echo :next >>%CD%/temp.txt
echo set "newname=%%~nx1" >>%CD%/temp.txt
Echo.>>%CD%/temp.txt
for /f "delims=" %%l in (%CD%\To_remove.txt) Do (
echo set "newname=%%newname:%%l=%%">> %CD%\temp.txt)
Echo.>>%CD%/temp.txt
echo ren %%1 "%%newname%%" >>%CD%/temp.txt
Start Temp.txt
Exit
I did save it as txt and opened at the end of script to see how it looks, it's just a matter of renaming it to .cmd and works.
I had to add comments and edit my post to add it all together but works
Thanks

Run command if string match from file

I'm dabbling with a batch script in which I'm trying to run a command if the string read from a text file matches the local string. I managed to get the file reading part working but I can't get the IF condition to work and the IF crashes the batch script but when I remove it works.
My Batch Code,
#ECHO OFF
SET content=
SetLocal EnableDelayedExpansion
FOR /f "tokens=* usebackq delims=" %%p in (file.txt) do SET content=!content!%%p
IF "%content%" == "Doit"(
ECHO boy it's working
PAUSE
) ELSE (
ECHO Not working
PAUSE
)
EndLocal
I'm primarily a PHP programmer not much experience with Batch scripting. Hope this is not a duplicate because I did several searches before posting this. Any way please show me how to get this done.

CMD replace spaces with underscores [duplicate]

This question already has answers here:
How to replace all spaces by underscores in all file names of a folder?
(8 answers)
Closed 7 years ago.
I have searched on this forum but not been able to recognize what I am looking for.
IRC dosen't allow the DCC of the files having spaces. How to create a batch file which replace spaces with underscore for all the MP3/FLAC files in the folder.
I tried this but failed
#echo off
Setlocal enabledelayedexpansion
Set "Pattern= "
Set "Replace=_"
For %%a in (*.mp3) Do (
Set "File=%%~a"
Ren "%%a" "!File:%Pattern%=%Replace%!"
)
Pause&Exit
It's giving me the error that the files are being used by another process though the files were not used by any programs and now it ain't doing anything whenever I run it it just says "press any key to continue and shuts down"
Any help?
thanks.
Check with echo in front of rename command. It should works.
But if you are getting the files are being used by another process. This is certainly the case.
This can be an application blocked on closing, application hidden in systray, windows search indexer.
Restart or logoff computer may solve.
#echo off
setlocal enabledelayedexpansion
set "pattern= "
set "replace=_"
for %%a in (*.mp3) do (
set "file=%%~nxa"
echo Ren "%%~fa" "!file:%pattern%=%replace%!"
::pause
)
pause
exit /b 0

Resources