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%
Related
It's a bit of a convoluted title and I apologise for my poor English, it's not my first language and I'm far from fluent. I hope my current code explains my goal better than my written explanation.
#echo off
Setlocal enableextensions enabledelayedexpansion
set BCAT_PATH="C:\\Users\\USER\\Downloads\\FMOD conversion to packable\\0Tools\\bincat"
CD "9temp\\zzz_FSBS_Extract_test"
for /D %%D in (\*) do
"%BCAT_PATH%\\bincat" "%%D\*.ogg" -o "..\\zzz_BuiltOGG_test%%\~ni.tmp"
PAUSE
#echo off
Setlocal enableextensions enabledelayedexpansion
set "BCAT_PATH=C:\Users\USER\Downloads\FMOD conversion to packable\0Tools\bincat"
CD "9temp\zzz_FSBS_Extract_test"
for /D %%D in (*) do FOR %%i in ("%%D\*.ogg") do ECHO "%BCAT_PATH%\bincat" "%%i" -o "..\zzz_BuiltOGG_test\%%~ni.tmp"
PAUSE
Use set "var1=data" for setting string values - this avoids problems caused by trailing spaces. In comparisons; don't assign a terminal \, space or quotes - build pathnames from the elements - counterintuitively, it is likely to make the process easier.
Your CD statement will change to a directory RELATIVE to your current directory, so if you are currently at C:\somewhere to C:\somewhere\9temp\zzz_FSBS_Extract_test. If 9temp\zzz_FSBS_Extract_test is an absolute location, then you'd need \9temp\zzz_FSBS_Extract_test
for /D %%D in (\*) do would set %%D to each directoryname in the root directory. Since you've changed to ..?..9temp\zzz_FSBS_Extract_test, you need * to scan the current directory. You could also use "..?..9temp\zzz_FSBS_Extract_test\*" without changing directory. *I don't know where 9temp\... is, so I've used ..?.. to represent its location.
Note that the command to be executed must follow directly after the do, on the same physical line. I've added ECHO to show the command that would be executed. After you've verified that the command is correct, remove the echo keyword to actually execute the command.
Note that BCAT_PATH is set to C:\Users\USER\Downloads\FMOD conversion to packable\0Tools\bincat The command generated will thus be "C:\Users\USER\Downloads\FMOD conversion to packable\0Tools\bincat\bincat".
I've no idea where %%i is defined in your program. I've inserted it where I believe it should go. That should set %%i to each .ogg filename in the directory %%D in turn. %%~ni should return the name part of that file.
Your output directory would be ..?..9temp\zzz_BuiltOGG_test since your current directory is ..?..9temp\zzz_FSBS_Extract_test . The \ should be placed between the directoryname and the filename.
There is not need for delayedexpansion although setlocal is a good idea.
This will not do for each directory, but instead find each .ogg file recursively, then run the command on each file. Also note, I've added the .exe extension to bincat
#echo off
setlocal & set "BCAT_PATH=C:\Users\USER\Downloads\FMOD conversion to packable\\0Tools\bincat"
cd /d "9temp\\zzz_FSBS_Extract_test"
for /R %%i in (*.ogg) do "%BCAT_PATH%\bincat.exe" "%%~i" -o "..\zzz_BuiltOGG_test\%%~ni.tmp"
Have a directory, say \configuration. There are multiple files in it. I am trying to convert the backslashes in the contents of all the files to forward slashes, apart from one file. So, I need to run a loop for the contents of the file, and if the name of the file is "X", skip it, else convert the slashes.
Very new to Windows batch scripting, can anyone help me out? I am writing a batch file for it, not using Powershell.
This is what I have tried so far:
#echo off
set TabbedPath=%C:\Users\Documents\Error;;=%
setlocal enabledelayedexpansion
set content=
for /r %TabbedPath% %%g in (*.properties) do (
echo %%g
set content=!content! %%g
echo !content!
set "g=!content!"
set "g=!g:\=/!"
echo !g!
)
endlocal
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.
I'm looking for a way to find and replace multiple words in a text file using a Windows batch script.
I know replacing a word can be done so with this bat script:
#echo off &setlocal
set "search=%1"
set "replace=%2"
set "textfile=Input.txt"
set "newfile=Output.txt"
(for /f "delims=" %%i in (%textfile%) do (
set "line=%%i"
setlocal enabledelayedexpansion
set "line=!line:%search%=%replace%!"
echo(!line!
endlocal
))>"%newfile%"
del %textfile%
rename %newfile% %textfile%
Now, I just have to take it one step further and feed in the search and replace strings from another file similar to this format:
search_string1, replace_string1
search_string2, replace_string2
search_string3, replace_string3
.
.
.
I would think that I would somehow process the file line by line, and parse them into two variables (search, replace) and then feed that into the script above. Any thoughts? I'm new to Windows batch scripts and have never really made one before so mind my newbie questions.
This type of text replacements are slow and prone to fail when performed via a Batch file. I wrote FindRepl.bat program that is a Batch-JScript hybrid script that not only run much faster and with no errors, but it also allows to perform the multiple replacements you are looking for in just one processing pass of the data file. JScript is a programming language that is included in all Windows versions from XP on. Using FindRepl.bat program you may solve your problem this way:
#echo off
setlocal EnableDelayedExpansion
set "search="
set "replace="
for /F "tokens=1,2 delims=," %%a in (replacements.txt) do (
set "search=!search!|%%a"
set "replace=!replace!|%%b"
)
set "search=!search:~1!"
set "replace=!replace:~1!"
< Input.txt FindRepl =search /A =replace > Output.txt
Note that all text placed after the comma in the replacements file is the replacement string, including spaces.
You may download FindRepl.bat program from this site. Place it in the same folder of previous program or, better yet, in a folder included in %PATH%, so you may use it directly.
Below is my script. I am trying to look into folders one level below and pick out only those folders, hence the ~-9 which extracts the last 9 chars from the path. But the set var= does not unset the variable because the output comes back with the same folder name repeated # times. Also batch doesn't allow me to do this extract trick directly on %%i, hence the need for the local variable.
How do I clear this variable so that it takes the new value in the next iteration?
#echo off
for /d %%i in (%1\*) do (
set var=%%i
echo %var:~-9%
set "var="
)
http://judago.webs.com/variablecatches.htm has an explanation for my problem. The magic lines were setlocal enabledelayedexpansion and calling var as echo !var:~-9!. ! vs % ...wow! cmd still amazes me.
You found the source of your problem, as well as the solution - delayed expansion.
But using FOR while delayed expansion is enabled can cause problems if any of the filenames contain the ! character. The expansion of the for variable %%i will be corrupted if the value contains ! and delayed expansion is enabled. This is not a frequent problem, but it happens.
The solution is to toggle delayed expansion on and off within the loop
#echo off
setlocal disableDelayedExpansion
for /d %%i in (%1\*) do (
set var=%%i
setlocal enableDelayedExpansion
echo !var:~-9!
endlocal
)
I'm also wondering what you mean by "I am trying to look into folders one level below and pick out only those folders, hence the ~-9 which extracts the last 9 chars from the path". I suspect your are trying to get the name of the child folder, without the leading path information. If that is so, then using the substring operation is not a good solution because the length of folder names varies.
There is a very simple method to get the name of the folder without the leading path info:
for /d %%i in (%1\*) do echo %%~nxi