I'm trying to get my head around the problems with call command and in particular when a batch call a second batch and if the variables are global or local.
Two questions:
[First question]
In the example below call command doesn’t work and I think because there are setlocal\endlocal in the second.bat and so variables are not visible in the environment of first.bat. Right?
(if I execute second.bat without setlocal\endlocal first.bat works).
[Second question]
when I run second.bat without setlocal\endlocal how can I test\check\trace if variables are or aren’t global?
thanks for your help.
giac
first.bat:
#echo off
setlocal enabledelayedexpansion
…
call C:\WINDOWS\system32\second.bat variable_01 variable_02
…
echo variables (worked by second.bat)
……
endlocal
#echo on
second.bat:
#echo off
setlocal enabledelayedexpansion
…
something that works variables
…
endlocal
#echo on
Related
I'm making a bat file which add/load/rename savegame slots, and I had problem with some variables, the first variable is savegame number, the second variable is the name of the savegame, here is the code I've tried:
#echo off
set savename2=FortySeven
set nb=2
echo %savename%nb%%
pause
The result I have got is nb%
Either enable delayed expansion:
#Echo Off
Set "savename2=FortySeven"
Set "nb=2"
SetLocal EnableDelayedExpansion
Echo !savename%nb%!
Pause
EndLocal
Or, use Call for that expansion:
#Echo Off
Set "savename2=FortySeven"
Set "nb=2"
Call Echo %%savename%nb%%%
Pause
I'm having trouble running !var! examples ad described here http://ss64.com/nt/delayedexpansion.html
Instead of the expected variable content output as the example describes, I get the literal "bang V A R bang" output, any idea?
C:\>Setlocal EnableDelayedExpansion
C:\>Set _var=first
C:\>Set _var=second& Echo %_var% !_var!
first !_var!
thanks.
You are getting an unexpected result because you are issuing the commands at the command prompt. Create a batch file by putting the following commands in a file with a .bat extension then run the batch file.
#echo off
Setlocal EnableDelayedExpansion
Set _var=first
Set _var=second& Echo %_var% !_var!
E.g., if I created a batch file named delayedexp.bat with the above contents, I would see the following when I run it:
C:\Users\JDoe\Documents\>delayedexp
first second
setlocal only works within the confines of a command script:
help setlocal
If you have access to the parameters of the cmd call, you can set parameter /v. Must be first. And use ! instead % for variables.
%windir%\system32\cmd.exe /v /c set a=10&echo a=!a!&echo My Path is %CD%&pause
This is how, for example, you can get the date in the Russia-France format directly in the Windows shortcut, where a simple percentage is impossible due to its doubling. In std queries with the /v parameter, both a percentage and an exclamation mark will work fine, but single % for cicles.
%windir%\system32\cmd.exe /v /c echo off&for /F "tokens=1-6 delims=:., " %A In ("!date! !time!") Do (Echo %A.%B.%C %D:%E:%F)&pause
i want to run git pull in a path for all projects, so i wrote a bat file.
#echo off
setlocal enabledelayedexpansion
set dir=%1
if "%dir%"=="" (
set dir=%~dp0
)
for /D /R "%dir%" %%i in (*) do (
if exist %%i\.git (
cd /d %%i
rem no effect
echo %cd%
rem git pull
)
)
pause
but is seems that cd in for loop does not take any effect, i don't know why.
can someone help me solve this problem?enter code here
It has effect. But, in batch files, when a block of code (the code enclosed in parenthesis) is reached (the same for a line out of a block), variable reads are replaced with the value of the variable before executing the code in the block. So, when your for command is reached, the read of the %cd% variable is replaced with the value of the %cd% variable before executing the code. This speeds and simplifies execution of the code but generate this kind of problems.
You can enable delayed expansion with setlocal enabledelayedexpansion command, and change the sintax from %cd% to !cd!. This tells cmd that that this variable read should be delayed until execution of the line.
I have made a bat script that should copy the list of folders to a variable but I don't get anything in the variable. In other words, when I echo the variable after my for loop I get the expected output, but in the shell outside after executing the script, I don't see anything set in my variable. How can I get all the variables to copy correctly?
I am using Windows 7.
Batch FIle (script.bat):
#echo off
setlocal enabledelayedexpansion enableextensions
for /r /D %%x in (*) do (
SET PATH_VALUE=%%x;!PATH_VALUE!
)
echo %PATH_VALUE%
Output of windows cmd utility
C:\test> script.bat
C:\test\1;C:\test\2
C:\test> echo %PATH_VALUE%
%PATH_VALUE%
How do I get the %PATH_VALUE% as an environment variable? I found a similar question here but it doesn't quite answer my case.
That is because of your SETLOCAL command that you use to enable delayed expansion. Yes it provides the delayed expansion you need, but it also localizes environment changes. As soon as your batch script ends, there is an implicit ENDLOCAL, and the old environment is restored.
You can pass the value across the ENDLOCAL barrier by adding the following to the end of your script:
endlocal&set "PATH_VALUE=%PATH_VALUE%"
or you could write it like:
(
endlocal
set "PATH_VALUE=%PATH_VALUE%"
)
Both of the above work because the blocks of code are expanded and parsed prior to the ENDLOCAL executing, but the SET statement with the expanded value is executed after the ENDLOCAL.
In a Windows batch file, I want to construct a classpath of .jar files in a trusted directory.
I thought this might work:
set TMPCLASSPATH=
for %%J in (*.jar) do set TMPCLASSPATH=%TMPCLASSPATH%;%%J
This doesn't seem to work, since %TMPCLASSPATH% appears to be evaluated once at the beginning of the for loop.
Any suggestions?
You need to use delayed expansion, you add SETLOCAL ENABLEDELAYEDEXPANSION to the top your batch file, and use ! rather than % around the variable names.
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
set TMPCLASSPATH=
for %%j IN (*.jar) DO set TMPCLASSPATH=!TMPCLASSPATH!;%%j
echo %TMPCLASSPATH%