Concatenate file paths to an environment variable in batch script - windows

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.

Related

Batch, How do I execute code stored in a variable or txt file?

How can I execute code saved in a variable? I'm trying to execute an if statement that I have saved in another file (that must remain a text file). Clues on how to execute an if statement just from a variable might help, as I assume the problem is that it can't read the %%s.
Text file contains:
if %var%==0301 (echo Yay)
Batch file contains:
for /f "tokens=*" %%s in (code.file) do (
%%s
)
This normally executes the code in code.file by setting everything in code.file to the variable %%s and then executing the variable.
The result is this: 'if' is not recognized as an internal or external command, operable program or batch file.
This method works for executing echo and set, but I need it to work for if.
The IF is detected by the parser in phase2 only, but %%s will be expanded in a later phase.
You need to use a percent expansion for it, like in this sample
for /f "tokens=*" %%s in (code.file) do (
set "line=%%s"
call :executer
)
exit /b
:executer
%line%
exit /b
But for your sample: if %var%==0301 (echo Yay)
It will never say Yay, because %var% will never be expanded.
This is because %line% is already a percent expansion, it will not work recursively.
That could be solved by changing the text in code.file to
if !var! == 0301 (echo Yay)
This works, because the delayed expansion happens after the percent expansion
Or a much simpler solution:
copy code.file tmp.bat
call tmp.bat
del tmp.bat
The major problem at hand is that the command interpreter particularly handled the commands if, for and rem: These commands are recognized earlier than every other one, even before for meta-variables like %%s become expanded. Therefore, these commands are no longer detected after expansion of %%s.
Refer to: How does the Windows Command Interpreter (CMD.EXE) parse scripts?
According to this, said commands are detected during Phase 2, while expansion of for meta-variables happens in Phase 4. Other commands are found later in Phase 7.
A possible way to work around that is to use a sub-routine, which %-expansion occurs in, which happens in Phase 1, hence before recognition of the three special commands:
for /f "tokens=*" %%s in (code.file) do (
rem // Execute the read command in a sun-routine:
call :SUB %%s
)
goto :EOF
:SUB
rem // Take the whole argument string as a command line:
%*
goto :EOF

Returning string from external function batch

I have two batch files. One is the main (caller) file and one is a function.
The function takes one parameter, does some stuff and then i wanna return the string to the caller (main.bat). %ERRORLEVEL% is not an option, as it can only return integers.
main.bat:
call function.bat hello
function.bat:
REM some code.......
[HERE THE CODE TO RETURN A STRING TO CALLER FILE]
Seems like a basic operation, so there must be a way. Also, i prefer not making files with the output and reading it in the main.bat, because i want to make it work as a easy-to-use function.
The two batch files are executed both by same Windows command processor process and share therefore all environment variables.
Main.bat:
#echo off
set "MyVariable="
call Function.bat hello
echo MyVariable=%MyVariable%
Function.bat:
#echo off
rem Some code ...
set "MyVariable=%~1"
It could be that Function.bat uses for some reason command SETLOCAL. In this case all environment variables defined as well as all modifications made on environment variables after the command SETLOCAL are lost after corresponding ENDLOCAL. This command is implicitly called by Windows command processor on exiting execution of a batch file for each SETLOCAL not yet completed with explicit execution of corresponding ENDLOCAL. Read this answer for details about the commands SETLOCAL and ENDLOCAL.
It is necessary to explicitly set an environment variable on same command line as the command ENDLOCAL with using immediate environment variable expansion to pass value of an environment variable from current environment variables list to previous environment variables list.
Function.bat:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem Some code ...
set "MyVariable=%~1"
endlocal & set "MyVariable=%MyVariable%"
The last command line is preprocessed first by Windows command interpreter to
endlocal & set "MyVariable=hello"
So the command line after preprocessing does not contain anymore any variable reference. The command ENDLOCAL restores previous environment which results in deletion of environment variable MyVariable. But the second command SET specified on same command line sets this variable once again with the value hello in restored environment.
See also Single line with multiple commands using Windows batch file.

call command in batch file - global local variables

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

String replacement within FOR /F into batch file

There are a handful of questions on SO that look similar, but I cannot figure out some behaviour and I am looking for help.
Below is a snippet from a batch file I am trying to write which will load in a set of directories and potentially replace letter substitutions with an expanded path, e.g. the properties file might look like:
location1=C:\Test
location2=[m]\Test
Where location1 points to C:\Test and location2 points to C:\Program Files(x86)\MODULE\Test, because [m] is a shorthand to C:\Program Files(x86)\MODULE.
The batch script, to this point, is simply trying to read in the list of file paths and expand/replace the [m].
SET build.dir=%~dp0%
SET progfiles=%PROGRAMFILES(X86)%
IF "%progfiles%"=="" SET progfiles=%ProgramFiles%
SET local.properties=%build.dir%local.properties
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "tokens=1* delims==" %%i IN (%local.properties%) DO (
SET local.dir=%%j
SET local.dir=!local.dir:[m]=%progfiles%\MODULE!
echo !local.dir!
)
ENDLOCAL
Running this kicks out an error:
\MODULE was unexpected at this time.
If I replace the FOR with the following instead:
set test="[m]\Proj\Dir"
set test=!test:[m]=%progfiles%\MODULE!
echo %test%
I get the desired C:\Program Files(x86)\MODULE\Proj\Dir printed out...so I'm confused why it works fine outside of the FOR loop.
My understanding about delayed expansion is that it 'expands' at runtime...which you get to happen using !! instead of %% wrapped around the variable. Furthermore, as I'm creating the local.dir variable inside the FOR loop scope, I must use delayed expansion in order to access it with the updated value for the iteration.
I feel like the problem is using %progfiles%, like there's some special syntax I need to use in order to make it work but nothing is adding up for me. When I echo %progfiles%, it prints out as C:\Program Files(x86 -- note the missing trailing ).
Any ideas? Thanks
Tested suggestion:
D:\Projects\Test\Build>test
*** "D:\Projects\Test\Build\local.properties"
*** "","C:\Program Files (x86)"
[m]=C:\Program Files (x86)\MODULE
Adding quotes around the whole expression makes it work -- can't use other characters for some reason (like []) -- and since I want to append to the path later, we can safely remove the quotes afterwards:
SET local.dir="!local.dir:[m]=%progfiles%\MODULE!"
SET local.dir=!local.dir:"=!
Test this to see if you can nut out the issue:
The double quotes are to provide robust handling in a system with long file/path names.
The () are unquoted which are a problem in a batch script, when inside a loop.
#echo off
SET "build.dir=%~dp0%"
SET "progfiles=%PROGRAMFILES(X86)%"
IF "%progfiles%"=="" "SET progfiles=%ProgramFiles%"
SET "local.properties=%build.dir%local.properties"
echo *** "%local.properties%"
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "usebackq tokens=1* delims==" %%i IN ("%local.properties%") DO (
SET "local.dir=%%j"
echo *** "!local.dir!","%progfiles%"
SET "local.dir=!local.dir:[m]=%progfiles%\MODULE!"
echo !local.dir!
)
ENDLOCAL
pause
It has to do with the () characters that end up in your progfiles string. If you take them out, the substitution seems to work fine.
My suggestion is to ditch command for this particular purpose and use one of the other standard tools that Windows comes with. While my personal preference would be Powershell (since it's so much more powerful and expressive), you may just need something quick that you can integrate into existing cmd.exe stuff.
In that case, try the following VBScript file, xlat.vbs:
set arg = wscript.arguments
wscript.echo Replace(arg(0),arg(1),arg(2))
Your batch file then becomes something like, noting the inner for /f which captures the output of the VBS script and assigns it to the variable:
#echo off
SET build.dir=%~dp0%
set progfiles=%PROGRAMFILES(X86)%
if "%progfiles%"=="" set progfiles=%ProgramFiles%
set local.properties=%build.dir%local.properties
setlocal enabledelayedexpansion
for /f "tokens=1* delims==" %%i in (%local.properties%) do (
set local.dir=%%j
for /f "delims=" %%x in ('cscript.exe //nologo xlat.vbs "!local.dir!" "[m]" "%progfiles%\MODULE"') do set local.dir=%%x
echo !local.dir!
)
endlocal
Running that, I get the output:
C:\Test
C:\Program Files (x86)\MODULE\Test
which I think is what you were after.

use cd in windows .bat file has no effect

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.

Resources