Issue with Windows Batch Script - replace -


hi have loop use entries out of lsit of drives loop through, each drive loop through entries in file, works. trying finding entry file (which file name , path without drive letter , first folder (parent folder named %_tag%)

ok far good, want replace extension of file r (*.w or *.p or *.cls must become *.r)

but reason command not work (set str=!str:.w=.r!) have tried replacing ! %

i have tried various things , still stuck. here complete batch file. reads in file list of partial path , file names. tried using function renaming

:bof     @echo on     cls     setlocal enabledelayedexpansion :init     set _tag=tst     set /p _infile="enter filename use investigation: "     set _infile=%cd%\%_infile%     set /p _tag="to environment did deploy? (tst / liv): "     if /i "%_tag%"=="liv" (       set _drive=m o p r s y x     ) else (       set _drive=m t x     ) :confirm     echo use code in the: %_tag% folder , file: %_infile% , drives: %_drive%     set /p _continue="do wish continue (y/n): "     if /i "%_continue%"=="n" goto :eof     echo use code in the: %_tag% folder , file: %_infile% , drives: %_drive%  1>> %_infile%.%_tag%.rlog 2>&1 :dowork     %%j in (%_drive%) (         echo ...         echo investigating: %%j:\%_tag%\ 1>> %_infile%.%_tag%.rlog 2>&1         %%j:         cd %%j:\%_tag%\         /f %%i in (%_infile%)  (              set "string=%%i"             call:mysetpathfunc !string! "/"    "\"             call:mysetpathfunc !string! ".w"   ".r"             call:mysetpathfunc !string! ".p"   ".r"             call:mysetpathfunc !string! ".cls" ".r"             if exist %%j:\%_tag%\!string! (                 echo found in correct folder 1>> %_infile%.%_tag%.rlog 2>&1             ) else (                  echo did not find in correct folder 1>> %_infile%.%_tag%.rlog 2>&1             )             call:mysetpathfunc !string! "cbsrc"  "tty"             call:mysetpathfunc !string! "hotfix" "tty"             if exist %%j:\%_tag%\%%string%% (                 echo found in tty 1>> %_infile%.%_tag%.rlog 2>&1             ) else (                  echo did not find in tty 1>> %_infile%.%_tag%.rlog 2>&1             )          )     ) :eof   echo done used - tag: %_tag% , used file: %_infile% drive %_drive%   echo done used - tag: %_tag% , used file: %_infile% drive %_drive% 1>> %_infile%.%_tag%.rlog 2>&1   timeout /t 8 /nobreak > nul   c:   exit /b   ::-------------------------------------------------------- ::-- function section starts below here ::--------------------------------------------------------  :mysetpathfunc    - passing variable reference       echo before %~1 1>> %_infile%.%_tag%.rlog 2>&1     set %~1=%~1:%~2=%~3       echo after %~1 1>> %_infile%.%_tag%.rlog 2>&1 goto:eof 

generation 955 of "delayedexpansion" problem.

within block statement (a parenthesised series of statements), entire block parsed , then executed. %var% within block replaced variable's value at time block parsed - before block executed - same thing applies for ... (block).

hence, if (something) else (somethingelse) executed using values of %variables% @ time if encountered.

two common ways overcome 1) use setlocal enabledelayedexpansion , use !var! in place of %var% access changed value of var or 2) call subroutine perform further processing using changed values.

where flags involved, situation changes again. set "flag=" ensure flag cleared. set "flag=somethingelse" ensure set (the value not relevant.) using if defined flag (doiftrue) else (doiffalse) works on run-time (current) status of flag - not parse-time value.

look hundreds of items delayedexpansion on solutions.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -