Advanced Batch-File: Dynamically setting a variable with set /p and redirection
Hey there.
Basically, I have a batch-file, that goes through all model and texture files, saves their absolut path and then passes the gathered pathes to my converter.exe. Currently I do this by appending the path of each input file to aset /p
variable, with the output of that variable redirected into a params.txt.
for /f "tokens=*" %%a in ('dir "%MODEL_SRC%" /a:d-h /b') do (
set /p=-o="%MODEL_DST%\%%a.res" >cvtr_params.txt<nul
for /f %%b in ('dir "%MODEL_SRC%\%%a\*.obj" /a:-d-h /b') do (
<nul (set/p=""%MODEL_SRC%\%%a\%%b" ") >>cvtr_params.txt
)
for /f "tokens=*" %%c in ('dir "%TEX_SRC%\%%a\*.png" /a:-d-h /b') do (
<nul (set/p=""%TEX_SRC%\%%a\%%c" ") >>cvtr_params.txt
)
start /b /wait "" bin\converter.exe @cvtr_params.txt >nul
)
del cvtr_params.txt
The params.txt would look like this:
-o="ConvertedPath\Ingame.res" "ModelPath\Model1.obj" "ModelPath\Model2.obj" "TexPath\Tex1.png" "TexPath\Tex2.png"
And so on and so forth.
And yes, the code HAS to be this complicated for actually appending to the end of the line, not a new line.
Now I thought, that the cvtr_params.txt is unnecessary, and had this approach, which did nearly the same, just not redirecting the "question" of the set /p
variable into a params.txt but actually just assigning set /p params
to the "question" of set /p z
.
setlocal EnableDelayedExpansion
for /f "tokens=*" %%a in ('dir "%MODEL_SRC%" /a:d-h /b') do (
set /p params=<nul
set /p z=-o="%MODEL_DST%\%%a.res" <nul
for /f %%b in ('dir "%MODEL_SRC%\%%a\*.obj" /a:-d-h /b') do (
<nul (set/p=""%MODEL_SRC%\%%a\%%b" ")
)
for /f "tokens=*" %%c in ('dir "%TEX_SRC%\%%a\*.png" /a:-d-h /b') do (
<nul (set/p=""%TEX_SRC%\%%a\%%c" ")
)
st开发者_开发问答art /b /wait "" bin\converter.exe !params!
)
endlocal
But somehow, the !params!
variable appears as empty. (an echo delivers "ECHO is set to <OFF>
")
Now I'm at the end of my wits, and I just started seriously working with batch files like 3 days ago, so if anyone has a good idea, let me hear it! :)
Greetz
In your code, params
is set to be empty at the beginning and never changed afterwards, so what do you expect?
If you want to concat elements in params
, you should try something along the lines of
set params=!params! ""%TEX_SRC%\%%a\%%c" "
but beware, environment variables have a maximum length of 8191 characters since WinXP or later (less on earlier systems).
精彩评论