batch-file , Maximum setlocal recursion level reached in batch

Maximum setlocal recursion level reached in batch


Tag: batch-file

The task is to replace the reference path with some new path in collection of html files. I used the below code for that and it throws the maximum setlocal recursion level reached error,

@echo off
for /r ".\" %%f in (\html\*.htm) do (
    call :SUB ../icons ../../icons "%%f">"%%f_new" 

    del "%%f"
for /r ".\" %%f in (*.htm_new) do rename "%%f" "*.htm"
exit /b

if "%~1"=="" findstr "^::" "%~f0"&GOTO:EOF
for /f "tokens=1,* delims=]" %%A in ('"type %3|find /n /v """') do (
    set "line=%%B"
    call set "line=echo.%%line:%~1=%~2%%"
    for /f "delims=" %%X in ('"echo."%%line%%""') do %%~X
exit /b

Can anyone please let me know how to resolve this error?

Thanks in advance..


First, you should ident your code.
The parenthesis aren't balanced, there are more opened than closed parenthesis.

You call a label/function which is a part of your first FOR /r loop, that will never work.

Perhaps this is what you want (but I can't even guess what you try withyour code)

@echo off
for /r ".\" %%f in (\html\*.htm) do (
    call :SUB ../icons ../../icons "%%f">"%%f_new" 
    del "%%f"
for /r ".\" %%f in (*.htm_new) do rename "%%f" "*.htm"
exit /b

if "%~1"=="" findstr "^::" "%~f0"&GOTO:EOF
for /f "tokens=1,* delims=]" %%A in ('"type %3|find /n /v """') do (
        set "line=%%B"
        call set "line=echo.%%line:%~1=%~2%%"
        for /f "delims=" %%X in ('"echo."%%line%%""') do %%~X
exit /b

After edited your code:
The setlocal/endlocal should be in the same block, in your case you call SETLOCAL for every html file, but only call ENDLOCAL once. But each SETLOCAL needs an ENDLOCAL

After your comment:
You try to modify a html file with percent expansion, that will fail in many cases, as it's tricky to handle the special characters in an html file like <>&|.

Btw. Your For /f loop to read the file content will fail when a line begins with ].

This one should work

if "%~1"=="" findstr "^::" "%~f0"&GOTO:EOF
setlocal DisableDelayedExpansion
for /f "tokens=* delims=" %%A in ('"type %3|find /n /v """') do (
        set "line=%%B"        
        setlocal EnableDelayedExpansion
        set "line=!line:%~1=%~2!"
        set "line=!line:*]=!"
exit /b

But there is a much simpler solution using the repl.bat tool from dbenham


Counting lines in a text file; use result to split text files into sections

Using Windows batch or possibly another language I am looking to split a text file containing 85K+ results into separate text files. There is also a header at the top that needs to be present on every new text file. The short but only code I have and can get...

String check incorrectly returning true

So I have been playing around in batch trying to make a simple batch game. So now what have I done wrong? Here is the code: :promotions_sucessful cls if %division%==Bronze V ( set league=Bronze IV set lp=0 echo You have been promoted! del promotion.txt pause>nul goto menu ) if %division%==Bronze...

Create a bat file that will run git and then run script

I'm trying to write a batch file that when I run it will clone a repository locally. To open the git I have the following command: C:\Windows\System32\cmd.exe /c "%LOCALAPPDATA%\Atlassian\SourceTree\git_local\bin\sh.exe" --login -i However no matter what command I put in after this it never runs. I have read somewhere that this...

Batch script ends after for loop

I've got a batch file that executes a program along with sequential (numbered) macros and calls another batch file that monitors when it is finished before it begins the next iteration. I can't use Start /wait or other "ordering" commands when launching the program because it is started with a...

Parsing the text file line-by-line using batch script (batch file)

So, I am programming in the batch script and I came across this issue. The following code will take yourwords.txt file and parse it. The existedWord variable will have the last word of the text file. So, everytime when I run this program it will only compare the user's input...

Execute a batch file before executing in a shortcut (.lnk)

I have multiple versions of a program called Siemens NX. NX uses environmental variables for configuration. I need NX 10.0 to use a different set of environmental variables than my NX 7.5 which uses the system environmental variables. Therefore, I have written a batch file that setups the environmental variables...

How to extract a string from the first line of a file using batch?

I have these files that contain a name in the first line I wish to extract. All the methods I have approach either result in error or give me nothing. An example file can be found here: I wish to extract "PN-G013.3+01.1", which lies on the first line (column...

Getting Respond Time from Ping Command

I'm trying to create a batch file that will ping a certain server and will only display the respond time. I don't mind if value is stored in a variable or not. If you do a normal ping you get: Reply from <hostname/server>: bytes=#byte_value time=#time_value TTL=#TTL_value And I only want:...

Batch - Comparing two txt files

I have some difficulties comparing two txt files with batch. I used the "findstr" function with many option matchings but none works (for example FINDSTR /I /V /B /G:file1.txt file2.txt). I have a first txt file as following: File1.txt Object 1 Argument 50 Object 2 Argument 10 Object 3 Argument...

Win7 Batch File - Moving Subfolders(& Files) to Grand-Parent Directory

I have a somewhat complicated problem. I've downloaded an archived website from using Httrack and now I have thousands of subfolders and files I need to merge before I can rebuild it. I'm trying to write a batch file to solve the problem. But my search results never come...

Batch file %%i was unexpected at this time

I am trying to get a patch file to set a variable to the number of instances that are running for a particular executable but am getting this error: %%i was unexpected at this time Here is my code: for /f %%i in ('wmic process where name="chrome.exe" ^| find "chrome.exe"...

Batch script terminates in case of error when using pipe operator

I need to perferm error handling (check ERRORLEVEL) on an operation involving the pipe operator, but instead of the script continuing with a non-zero ERRORLEVEL, it terminates immediately. How can I avoid this behavior? Consider the following example. (Note that is a simplified constructed example to illustrate the problem -...

Logstash exec input plugin - Remove command run from @message

I'm using logstash 1.5.1 on a windows machine. I have to make a rest call, that delivers me JSON output. Therefore I'm using exec. The result is no json anymore :-(. The @message of this event will be the entire stdout of the command as one event. My logstash...

Is there a way to close a single window of a process from a BAT file?

Here's my situation: I have my Windows 7 computer set up so that every morning, a task runs that plays my alarm and causes my morning routine to open up (in a WordPad window). The thing is, I would like to also have the window automatically close at the time...

My batch files if else statement isn't working

So I have created a file that works and when testing it without the if/else statement. When I put in this in a if statement it doesn't work at all though, is my syntax wrong or what? Thanks. @echo off set /p id= Folder Name: set /p yn= Subfolders? (y/n):...

Change OS Language Silently (Windows 7)

I'm writing a script (powershell) for SQL Server 2014 Express Install. The problem is: I have to change the OS Language (Region and languages) to fr_FR (French from France) Silently. The current language is fr_CA (French from Canada). My question is: Can i change the language from intl.cpl (Region and...

String parsing with batch scripting

I have a file called pictures.xml and it contains some pictures information like: <ResourcePicture Name="a.jpg"> <GeneratedPicture Name="b.jpg"/> <GeneratedPicture Name="c.jpg"/> </ResourcePicture> <ResourcePicture Name="z1.jpg"> <GeneratedPicture Name="z2.jpg"/> <GeneratedPicture Name="z3.jpg"/> <GeneratedPicture Name="z4.jpg"/> </ResourcePicture> What I want do do is to get each line in for loop and print the names of the pictures. Sample...

Batch file to check program installation

I am trying to determine if some program has been installed and, if it has, open it. If the program has not been installed, an alternate program should open. For example, if Notepad++ has been installed, the script should open that; if not, Notepad should be opened instead. Any advice...

Delete files from folder [Windows]

The problem is as follows - I have a text file with an imagelist which contains the names of photos. The photos reside somewhere else on a hard drive. How do I delete/move those photos which names are in imagelist? Is there any batch routine?...

batch file for runing a java command

I have to run the following command for hundreds of .docx files in a directory in a windows in order to convert them to .txt. java -jar tika-app-1.3.jar -t somedocfile.doc > converted.txt I was wondering if there is any automatic way such as writing a ".bat" file to do this....

Batch ffmpeg and compress the files

I am doing a bunch of conversions with ffmpeg, where I need to watermark the content. I am using a batch file to run the ffmpeg application, but I want to automate all of this. for %%a in ("*.mp4*") do ffmpeg -i "%%a" -i logo.png -filter_complex overlay "newfiles\%%~na.mp4" pause Source...

String manipulation with batch scripting

I need to save the variable in %%c temporarily which comes from a for loop. But when I try to do that, the content changes unexpectedly. Some space characters appear at the end of the string. The content of %%c is a.jpg by the way. echo %%ca REM prints a.jpga...

I cannot use the msg command in cmd (or batch for that matter). How can I fix this?

While in cmd or making a batch file, I cannot use the command msg. When I try to use it, it returns the error msg is not recognized as an internal or external command, operable program or batch file." I'm pretty sure the error is that im missing a msg.exe...

Batch File Return Codes with executables

Possibly a really dumb question: If a batch file runs an executable and the executable fails, does the batch file return the error code of the executable or does it return 0 for finishing its code? (or some other return code?) Relevance: I'm created tasks for a task scheduler to...

Batch file to conditionally execute ifortvars.bat

I am using a make.bat file to compile my Fortran code. To compile using the ifort command, I first need to initialize the compiler by executing ifortvarsbat intel64 command where I had to setup the path variable. This works fine, however, the path keeps increasing in size and says command...

Format a command in powershell including a comma, can't find the right way to escape

I have a command that I need to run in a Powershell script, the command is : ".\pacli DELETEUSER DESTUSER='"[email protected]`,com"' sessionid=333" | invoke-expression The comma (,) in here :[email protected],com is not a mistake, and that's what is giving me the hard time. I tried to escape the comma with `...

Passing argument to python script within a batch file in Windows

I have a python script that can be called in Windows as: python.exe param > output.csv param is an input parameter. I also have a txt file called params.txt that contains many lines, each line is a value for the parameter of the python script: hello world this is...

As only show the words found in the findstr

How do I display only the words found in the search instead of displaying the lines? My code: Findstr /li /G:"List.txt" "File.txt">"Result.TxT" List: Disc Music Song Album Result: DISC1312we7das67da 13dsdsa67dsahdsa7aMUSIC dsadsdfdsaSONG1223234235 The desired result: Disc Music Song ...

How do i handle ERRORLEVEL 9 in cmd

I have developed a batch script which checks for the version of the software installed on a machine and based on the version it picks up the correct version patch and installs it (runs) on the machine. However i am facing an issue where when i check for a particular...

Execute multiple command lines with multiple arguments

I can't figure out why my command lines doesn't function. I tried subprocess.Popen()` function : `Popen('C:/Test/test.exe -scan "C:/Users/Vince/test.txt" "C:/Users/Vince/test.xml"', cwd='C:/Users/Vince/Working_Directory') But when I run a batch file from my Python program by using os.system("E:/test.bat"), it functions. Here is the content of my batch file: C: cd Users\Vince\Working_Directory C:\Test\test.exe -scan "C:\Users\Vince\test.txt"...

Batch For Do Test if exist rename url link on desktop

Why does this not work? I want to test each users desktop for a file and then if it is there, rename it. FOR /F "tokens=*" %%G IN ('DIR c:\users /B') DO if exist "C:\Users\%%G\Desktop\My Link With Spaces.url" ( rename "C:\Users\%%G\Desktop\My Link With Spaces.url" "My NEW Link With Spaces.url" )...

Is there a way to output file size on disk in batch?

Is there a way to get the size on disk of a file like in the properties window: I have tried: (inside a batch file) echo %~z1 , for %i in (TestFile.txt) do echo %~zi , dir But they only return the size of the file(s). Is there any way...

Wildcard with Service Stop/Start

I know we could do this in VBS, PowerShell, etc. However, we do not want to do this with another language other than good ol' batch script. Have figured out the following: TASKKILL /F /IM "tomcat*" And this will forcibly kill/stop any persistent instances of Tomcat. Also, this would cover...

batch result me “echo off” instead the proper result

I have a little problem with the next code @Echo off Title STARTING cls echo. echo Checking running services... echo. timeout /t 2 /nobreak >NUL tasklist /fi "imagename eq cmd.exe" /v | find /I /N "DATABASESERVER" >NUL if "%ERRORLEVEL%"=="1" ( cls echo. echo Database is not running, now will start!...

Batch file rename output folder

So I have the following code to overlay a logo image over some videos. Its working great, the only problem I have is since my logo has the naming like "0001.logo.png", the output rar file doesnt set the right file extension, it sets the "myFolder_0001.logo" extension. Is it possible to...

If exist and errorlevels in a batch (.bat) file

I'm trying to run a delete command on the result of an SQL command but only if it returns an error code of 0. Below is my code: SqlCmd command... REM if SqlCmd command is successful run the below if exist statement if errorlevel 0 ( REM if the file...

Fastest way to count lines in a file

I have huge CSV file, which I want to know the total number of rows in which. Currently I am using something like this, which works perfectly, but it's slow: FIND /c ";" FILENAME.csv But I dunno if that's the fastest way to iterate through all lines in a CSV....

Get the PID of a Windows service by the name of the service

Is there a way of getting the PID of a windows service with a command in a batch script by just knowing the name of the service?

Garbage char returned by Shell.StdOut.ReadAll

Here is my code Set objFSO = CreateObject("Scripting.FileSystemObject") a = "@echo off && " & _ "pushd ""\\xxxxx.local\cfs\Development\Docs\Baseline"" &&" & _ "cls &&" & _ "dir /b /a-d &&" & _ "popd" Set objShell = CreateObject("WScript.Shell").Exec("%comspec% /c " & a) execStdOut = objShell.StdOut.ReadAll() msgbox execStdOut Basically I am trying to...

Batch-Script: Replace every “@” in file name with “_” in network drive including subfolders

My Problem is this: I want to replace every Special character such as "@" in file names. Example: old file: [email protected] new file: test_home.txt This should be done in a special chosen Folder including subfolders. The user should easily Change the Location (Folder). Thx guys Alright Well, I will give...

Can I separate some long expression of BAT file into several lines?

How can I split some long expression of BAT file into several lines (for convenient reading)? For example, I have a long line: call "%NUNIT%\nunit-console.exe" /out="%~dp0%file_name%.out" /err="%~dp0%file_name%.err" /noshadow /xml="%~dp0%file_name%.xml" "%~dp0%file_name%.dll" I want to get the more readable variant, for example three lines instead of one: call "%NUNIT%\nunit-console.exe" /out="%~dp0%file_name%.out" /err="%~dp0%file_name%.err" /noshadow...

Convert batch command to python

I am trying to convert a .bat file to python, almost everything is working fine, only the compression part is not working, I point out the flags and the path to the "rar.exe", but it's not working at all in the python version. What I need to correct to have...

bat file script to check if string contains other string

I need to write a batch file that will check if a variable contains specific value. I tried to do the following: If "%%a"=="%%a:%pattern%" ( echo Yes ) else ( echo No ) input example: %%a="bob binson" %patern%="binson" I never get Yes printed! can anyone please tell what i missed...

Windows script batch to copy files from different folders to other folders

The easiest way to explain the problem is by an example. I want to do something like this: file x in /path_1/A/x copy in /path_2/A/ file x in /path_1/B/x copy in /path_2/B/ ... ... file x in /path_1/Z/x copy in /path_2/Z/ From command line with a script in Windows....

Batch file to open multiple instances of cmd and run Ruby script in each instance

I'm trying to open multiple instances of cmd up which each run a ruby script at the same time. For background into the scripts, each of these ruby scripts run watir-webdriver and spawn new browser instances. All of this should happen instantaneously. Here's what I've tried: start cmd /k cd...

Open website url links with parameter by using a batch file

I am trying to open some website url links with a parameter in my browser. I made a batch file using this code : @echo off start "images" "" start "images" "" start "images" "" ... ... start "images" "" All I want is to create a For loop that...

How to show the place and only the words found Findstr?

I wonder how do I show the location where the words were found in findstr? My Code: Set "LFiles=%temp%\Files\*.txt">nul 2>&1 Findstr /li /G:"List.txt" "%LFiles%">"Result.TxT" (for /F %%a in (List.txt) do ( Findstr /li /C:"%%a" "%LFiles%" > NUL if not errorlevel 1 echo %%a ))>"Result2.TxT" List: Disc Music Song Album Result:...

.bat - insert text before a period

I have a .bat that I use to quickly query basic information from servers. After it gets the FQDN from DNS, I need to insert a "-r" (minus quotes) after the servername, but before the "". The area that it will be added to the script is below - for...

Changing input path in Batch - path manipulating/modification

I have a simple batch like this: SETLOCAL @echo off SET sourceRoot=d:\SVN\Project\Debug\General\ServicePortal\bin SET destinationRoute=d:\SVN\Project\Debug\General\Admin Project\ I can pass "d:\SVN\Project\Debug\General\Admin Project" through parameter to the bacth, but I don't know how can I cut the last folder "\Admin Project" and combine with "\ServicePortal\bin". Is it possible? Thx in advance...

Formating issue with md5deep

So I am making a program in batch and it requires a text file to be hashed then the hash has to be saved to a text file. I already have that part done but when it saves to the text file it saves like this f558e01b798b0390ab6206679a6926a7 C:\Users\computer\Desktop\tmpfile.txt So my...