Setting variables in a for line?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Pyromaniac
Posts: 3
Joined: 03 Apr 2010 12:11

Setting variables in a for line?

#1 Post by Pyromaniac » 04 Apr 2010 09:20

I have a script im creating that searches your steam folder for missing texture files and creates them as they are found. So far its pretty much done, but I can't figure out why I can't set the variable "list" in the for loop. It comes up as undefined, but when i place it outside of the for loop, it sets fine. Is there an explanation for this? Any help is appreciated.

Heres the batch im making

Code: Select all

@echo off
SetLocal EnableDelayedExpansion
echo ---------------------------------------------------------
echo                Pyromaniac's vmt creator!
echo ---------------------------------------------------------
set filefound=                 No files were missing!
set usrdir=C:\Steam\steamapps\pyromaniac1444\half-life 2\hl2\materials\
call :getlength "%usrdir%" length
cd C:\Steam\steamapps\pyromaniac1444\half-life 2\hl2\materials\
echo Searching...
for /f "tokens=*" %%A in ('dir *.vtf /s /b') do (
set list=%%~fA
echo Scanning %%~nA...
if not exist %%~pnA.vmt (
set filefound=         Missing vmt's were found and generated.
echo "VertexLitGeneric" > %%~pnA.vmt
echo { >> %%~pnA.vmt
set list=%list:~-%length%%
echo     "$basetexture" "%list%" >> %%~pnA.vmt
echo } >> %%~pnA.vmt
echo Missing vmt for file %%~nA.
)
)
echo ---------------------------------------------------------
echo                File creation is complete!
echo %filefound%
echo ---------------------------------------------------------

:getlength
SetLocal
set len=0
set str=%~1
if not defined str goto :eof
set str=%str:"=.%987654321
:loop
if not "%str:~18%"=="" set str=%str:~9%& set /a len+=9& goto loop
set num=%Str:~9,1%
set /a len=len+num
EndLocal&set %2=%len%&goto :eof

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

Re: Setting variables in a for line?

#2 Post by avery_larry » 05 Apr 2010 10:05

set list=!list:~-%length%!
echo "$basetexture" "!list!" >> %%~pnA.vmt

or don't bother with the first set:

echo "$basetexture" "!list:~-%length%!" >> %%~pnA.vmt

Post Reply