I am writing a function to execute shell commands and capture its output in a batch script.
:runShellCmd
setlocal EnableDelayedExpansion
SET lf=-
FOR /F "delims=" %%i IN ('%~1') DO if "%out%" == "" (set out=%%i) else (set out=!out!%lf%%%i)
echo "Cmd output: %out%"
SET "funOut=%out%"
ENDLOCAL & IF "%~1" NEQ "" SET %~2=%out%
goto :EOF
I have been successful in passing simple commands and getting output. But for calls like
CALL :runShellCmd "echo Jatin Kumar | find /c /i "jatin"" it fails with error unexpected | character.
I know we need to escape | with ^ in for but if I try to pass ^| in the function argument string, it changes it to ^^| which again throws error.
Am I missing something?
This is an effect of the CALL command.
The CALL command doubles all carets in one of the batch parser phases.
Normally you wouldn't see this, as the carets will be used as an escape charater directly after the doubling.
See this
call echo ^^^^
call call echo ^^^^
call call call echo ^^^^
call echo "^^^^"
call call echo "^^^^"
call call call echo "^^^^"
Output
^^
^^
^^
"^^^^^^^^"
"^^^^^^^^^^^^^^^^"
"^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"
But how do you can escape your pipe character?
You can't!
But you can add a caret remover in your function.
:runShellCmd
setlocal EnableDelayedExpansion
set "param=%~1"
set param
set "param=!param:^^=^!"
for .... ('!param!')
Or you could use an escaping trick when calling your function.
set "caret=^"
CALL :runShellCmd "echo Jatin Kumar %%caret%%| find /c /i "
This works, as the %%caret%% will be expanded after the CALL caret doubling phase.
#echo off
setlocal enableextensions disabledelayedexpansion
call :test "echo(this|find "t""
exit /b
:test
set "x=%~1"
for /f "delims=" %%f in ('%x:|=^|%') do echo [%%f]
I think i'm missing something, because this works for me.
EDITED - This should be a more general solution. Not bulletproof but a skeleton.
#echo off
setlocal enableextensions disabledelayedexpansion
call :test "(echo(this&echo(that)|find "t" 2>nul|sort&echo end"
exit /b
:test
set "x=%~1"
set "x=%x:|=^|%"
set "x=%x:>=^>%"
set "x=%x:<=^<%"
set "x=%x:&=^&%"
set "x=%x:)=^)%"
for /f "delims=" %%f in ('%x%') do echo [%%f]
Related
I've been trying to make a user input prompt that displays itself as multiple lines.
Example of how I want it to look:
Welcome to the program!
What would you like to do?
option [1]
option [2]
option [3]
This is how I tried to do that:
set /p selection="Welcome to the program! ^ What would you like to do? ^ option [1] ^ option [2] ^ option [3]"
But when I run that, it comes out like this:
Welcome to the program! What would you like to do? option [1] option [2] option [3]
I googled it but I couldn't find anything that helped, so if someone could tell me a way to do this that would be great!
Maybe it's simple.
#echo off
echo welcome to program!
echo what would you like to do?
echo option[1]
echo option[2]
echo option[3]
set /p selection=
There is a way to do it, and that is by creating a linefeed variable.
SET /P version:
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Rem Define linefeed as a variable.
(Set BR=^
%NULL%
)
:AskIt
Set "VARIABLE="
Rem Delayed expansion is needed to use the linefeed variable.
SetLocal EnableDelayedExpansion
Set /P "VARIABLE=Welcome to the program^!!BR!What would you like to do?!BR!option [1]!BR!option [2]!BR!option [3]!BR!"
(Set VARIABLE) 2>NUL | %SystemRoot%\System32\findstr.exe /RX "^VARIABLE=[123]$" 1>NUL || GoTo AskIt
Echo You chose %VARIABLE%& Pause
EndLocal
CHOICE.EXE version (recommended)
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Rem Define linefeed as a variable.
(Set BR=^
%NULL%
)
Rem Delayed expansion is needed to use the linefeed variable.
SetLocal EnableDelayedExpansion
%SystemRoot%\System32\choice.exe /C 123 /N /M "Welcome to the program^!!BR!What would you like to do?!BR!option [1]!BR!option [2]!BR!option [3]!BR!"
Echo You chose %ERRORLEVEL%& Pause
EndLocal
I have two batch file here, test.bat and len.bat.
len.bat is a function that receives input from test.bat, processes it and then return a result value back to test.bat.
test.bat
#echo off
setlocal EnableDelayedExpansion
call len tesla
echo !result!
pause
len.bat
#echo off
setlocal EnableDelayedExpansion
set "string=%~1"
for /l %%a in (0,1,10000) do if "!string:~%%a,1!" == "" (
set result=%%a
exit /b
)
When I open test.bat, I expect it would print a value. Instead, it says Echo is OFF.
There seem to be a problem passing the variable from len.bat to test.bat.
setlocal can be thought of as a sandbox, and anything created inside of that sandbox exists for as long as the sandbox exists. The sandbox stops existing when endlocal is called - either explicitly, or implicitly when the script ends.
In len.bat, you add a sandbox inside of the sandbox with a second setlocal enabledelayedexpansion command, and !result! is created inside of that inner sandbox. When len.bat ends, !result! is destroyed since it didn't exist before len.bat was called.
I suspect that you added the second setlocal enabledelayedexpansion because you thought it was needed to use delayed expansion inside of len.bat, but delayed expansion is actually still enabled because it was turned on in test.bat.
Delete the setlocal enabledelayedexpansion from len.bat and test.bat will correctly return "5".
If you feel that you need to keep the setlocal enabledelayedexpansion in len.bat to use it separately from test.bat, you can explicitly call endlocal and chain the set command to it to trick the interpreter into letting the variable escape the sandbox.
#echo off
setlocal EnableDelayedExpansion
set "string=%~1"
for /l %%a in (0,1,10000) do if "!string:~%%a,1!" == "" (
endlocal & set result=%%a
exit /b
)
Does this help you out? The string length is returned as %ErrorLevel%:
New len.bat
#Set "str=#%~1" & SetLocal EnableDelayedExpansion
#Set "len=0" & For /L %%G In (12,-1,0) Do #(Set /A "len |= 1 << %%G"
For %%H In (!len!) Do #If "!str:~%%H,1!" == "" Set /A "len &= ~ 1 << %%G")
#EndLocal & Exit /B %len%
Example test.bat
#Call "len.bat" four
#Echo %ErrorLevel% & Pause
#Call "len.bat" "nineteen inc spaces"
#Echo %ErrorLevel% & Pause
I made this script that finds all directories and echoes the directorie's name to a .txt file. The script is working but it ends up echoing only %A without any value. My script is below!
set /a count=0
setlocal EnableDelayedExtensions
FOR /D %%A in ("*") DO (call :sub)
endlocal
pause
exit
:sub
(echo [DIR] %%A)>>%count%.txt
set /a count+=1
The output in the .txt files is [DIR] %A.
Any idea how to fixe this? Thanks -David
First remark, you are using an invalid option for setlocal but that is probably just a typo.
The problem is that you are try to use a for-parameter where it cannot be used.
The rule is "A for-parameter can only be used within the command or command block () of a for loop"
Your subroutine is not within the command block of a for loop, but you can start a dummy for loop in the subroutine which will give you access to all available for-parameters.
set /a count=0
setlocal EnableDelayedExpansion
FOR /D %%A in ("*") DO (call :sub)
endlocal
pause
exit/b
:sub
For %%. in (.) do (echo [DIR] %%A)>>%count%.txt
set /a count+=1
You need to pass the parameter to the subroutine.
From https://www.informit.com/articles/article.aspx?p=1154761&seqNum=11 :
for %%f in (*.dat) do call :onefile %%f
exit /b
:onefile
echo Processing file %1...
echo ... commands go here ...
exit /b
As you've already enabled delayed expansion, there's no need to use a Call to a label, just do it within the loop.
#Echo Off
SetLocal EnableExtensions EnableDelayedExpansion
Set "count=0"
For /D %%G In (*) Do (
Set /A count += 1
(Echo [DIR] %%G) 1>"!count!.txt"
)
On this version, I've started at 1 instead of 0 for the first text file name, if you really want to start at 0, change line 3 to Set "count=-1"
I have a folder structure, which is like for example C:\Temp\ and there are a lot of folder and file, and within each folder there are a "callme.bat". I would like to create a so called main.bat which is one after another call the callme files within the main' window. But there is a problem, within the callme files are some echo which contains "!" mark what make a problem for me.
I realized the problem with the setlocal-endlocal combo, because the batch scrip wants to interpret the message within the "!" marks, so I must use endlocal, but if I did I not able to run the callme bats.
callme.bat
#echo off
echo !!! hidden message !!! not hidden message
pause
main.bat variant 1
#echo off
setlocal enabledelayedexpansion
set PATH=C:\Temp
for /F %%x in ('dir /B/A:D %PATH%') do (
set CURR_DIR=%PATH%\%%x
set ACTUAL_BATCH=!CURR_DIR!\callme.bat
echo !ACTUAL_BATCH!
call !ACTUAL_BATCH!
pause
)
pause
exit
main.bat variant 2
#echo off
set PATH=C:\Temp
for /F %%x in ('dir /B/A:D %PATH%') do (
setlocal enabledelayedexpansion
set CURR_DIR=%PATH%\%%x
set ACTUAL_BATCH=!CURR_DIR!\callme.bat
echo !ACTUAL_BATCH!
ENDLOCAL & SET VAR=!ACTUAL_BATCH!
echo %VAR%
pause
)
pause
exit
main.bat variant 3
#echo off
set PATH=C:\Temp
for /F %%x in ('dir /B/A:D %PATH%') do (
setlocal enabledelayedexpansion
set CURR_DIR=%PATH%\%%x
set ACTUAL_BATCH=!CURR_DIR!\callme.bat
echo !ACTUAL_BATCH!
REM source: https://stackoverflow.com/questions/3262287/make-an-environment-variable-survive-endlocal
for /f "delims=" %%A in (""!ACTUAL_BATCH!"") do endlocal & set "VAR=%%~A"
echo %VAR%
call %VAR%
pause
)
pause
exit
So I don't know what to do. Anyone has an idea?
variant 1's output:
C:\Temp\1\callme.bat
not hidden message
C:\Temp\2\callme.bat
not hidden message
variant 2-3's output:
C:\Temp\1\callme.bat
ECHO is off.
C:\Temp\2\callme.bat
ECHO is off.
TL;DR
ENDLOCAL&set "varname=%sourcevarname%"
probably, where varname is the variablename to set and sourcevarname is the variable whose value is to be assigned to varname - and they CAN be the same name, even if the statement appears logically null - it's exporting the variable from within the setlocal/endlocal block.
Key point: MUST be on one physical line and may be repeated if necessary (ie
ENDLOCAL&set "varname=%sourcevarname%"&set "varname2=%sourcevarname2%"
So
ENDLOCAL&set "fred=%fred%"&set "bill=%george%"
is perfectly valid, to set the value of fred outside the setlocal/endlocal bracket to its final value inside and of billoutside to the final value of george inside.
Some points about your code:
Never use PATH as a variable name, as it destroys the PATH variable for searching executable files.
Use the extended SET syntax set "varname=content" to avoid problems with trainling spaces.
You only need to disable the delayed expansion mode by using setlocal DisableDelayedExpansion
#echo off
setlocal EnableDelayedExpansion
set MY_PATH=C:\Temp
for /F %%x in ('dir /B/A:D %PATH%') do (
set "CURR_DIR=%MY_PATH%\%%x"
set "ACTUAL_BATCH=!CURR_DIR!\callme.bat"
call :execute ACTUAL_BATCH
pause
)
pause
exit /b
:execute ACTUAL_BATCH
set "batFile=!%~1!"
echo Calling !batFile!
setlocal DisableDelayedExpansion
call %batFile%
endlocal
exit /b
I have whittled down a more complex CMD script to the essentials. It reads an input file line by line, unquotes it (if quoted) and writes it out to another CMD file.
The problem is that if the input file contains exclamation marks (! or bang) the character gets stripped out somewhere along the line.
Here is the CMD script, BANG1.CMD:
#echo off
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
if exist bang2.cmd del bang2.cmd
for /f "tokens=*" %%a in (bang1.txt) do call :doit1 %%a
exit /b
:doit1
set P1=%1
if %P1%. EQU . exit /b
call :unquotex P1 %P1%
echo>>bang2.cmd echo P1:[%P1%]
exit /b
:unquotex
set X=%2
set Q=%X:~0,1%
if "!Q!" EQU ^""" SET X=!X:~1,-1!
set %1=%X%
exit /b
Here is the input file BANG1.TXT:
HelloWorld
"Hello World"
Hello!World
"Hello!World"
The resulting file BANG2.CMD ends up containing this:
echo P1:[HelloWorld]
echo P1:[Hello World]
echo P1:[HelloWorld]
echo P1:[HelloWorld]
The question is, what happened to the embedded bangs? I have tried with and without ENABLEDELAYEDEXPANSION. I have even tried escaping (^) the bangs in the input file, still with no luck.
Is there any way to preserve them?
Thanks.
The problem at all is delayed expansion here.
With delayed expansion, exclamation marks are used to expand variables, but when there is only one exclamation mark in a line it will be removed.
Specially in FOR /F loops delayed expansion is tricky to handle, as the expansion of the FOR parameter is directly affected by the delayed expansion. The only solution is to disable it temporarily.
The next problem is the CALL, you can't transfer content with CALL (without destroying it).
It's better to transfer the variable by reference (only the variable name) and then get the content in the called function.
The last problem in your code are the percent expansions, do not use them
when delayed expansion is enabled, as the delayed expansion is evaluated after the percent expansion an expanded line will be expanded a second time by the delayed expansion.
Sample.
Assume the content of var is Bang!
echo %var% expands to Bang! but then the delayed expansion will evaluate Bang! to Bang.
With echo !var! you simply get Bang!
#echo off
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
if exist bang2.cmd del bang2.cmd
for /f "tokens=*" %%a in (bang1.txt) do (
setlocal DisableDelayedExpansion
set "line=%%a"
setlocal EnableDelayedExpansion
call :doit1 line
endlocal
endlocal
)
exit /b
:doit1
set "P1=!%1!"
if "!P1!" EQU "" exit /b
call :unquotex P1
echo>>bang2.cmd echo P1:[!P1!]
exit /b
:unquotex
set "param=!%~1!"
if "!param:~0,1!" == ^""" (
set "param=!param:~1,-1!"
)
set "%1=!param!"
exit /b
Like this :
#echo off
(for /f "delims=" %%a in ('type bang1.txt') do echo echo P1:[%%~a])>bang2.cmd
Try this:
#echo off
if exist bang2.cmd del bang2.cmd
for /f "tokens=*" %%a in (bang1.txt) do call :doit1 %%a
exit /b
:doit1
set "P1=%1"
if %P1%.==. exit /b
call :unquotex P1 %P1%
echo>>bang2.cmd echo P1:[%P1%]
exit /b
:unquotex
set "%1=%~2"
exit /b
Using parameters, you can get the version without quotes using %~1 instead of %1. If %1 contains "hello world" for example, then %~1 contains hello world. This allows for an easier unquoting mechanism, removing the need for delayed expansion.