我有一个for循环,我使用它从要循环的驱动器列表中获取条目,对于每个驱动器,我循环遍历文件中的条目,这一切都有效。但接下来我要做的是从文件中查找条目(这是一个文件名和路径,不带驱动器号和第一个文件夹(名为%_TAG%的父文件夹)
好的,到目前为止还不错,现在我想做的是用r替换文件的扩展名(*.w或*.p或*.cls必须变成*.r)
但是由于某种原因,我的命令无法工作(设置str=!str:.w=.r!)我已经尝试替换!使用%
我尝试过各种方法,但还是卡住了。这是我的完整批处理文件。它读取包含部分路径和文件名列表的文件。我甚至尝试过使用一个函数来重命名
:bof
@echo on
cls
setlocal enabledelayedexpansion
:init
set _TAG=TST
set /p _INFILE="Enter Filename to use for investigation: "
set _INFILE=%CD%\%_INFILE%
set /p _TAG="To which environment did you deploy? (TST / LIV): "
if /i "%_TAG%"=="LIV" (
set _drive=M O P R S Y X
) else (
set _drive=M T X
)
:confirm
echo We will use code in the: %_TAG% folder and file: %_INFILE% and drives: %_drive%
set /p _continue="Do you wish to continue (Y/N): "
if /i "%_continue%"=="N" goto :eof
echo We will use code in the: %_TAG% folder and file: %_INFILE% and drives: %_drive% 1>> %_INFILE%.%_TAG%.rlog 2>&1
:dowork
for %%j in (%_drive%) do (
echo ...
echo investigating: %%j:\%_TAG%\ 1>> %_INFILE%.%_TAG%.rlog 2>&1
%%j:
cd %%j:\%_TAG%\
for /f %%i in (%_INFILE%) DO (
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 I found you in correct folder 1>> %_INFILE%.%_TAG%.rlog 2>&1
) else (
echo I did not find you 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 I found you in tty 1>> %_INFILE%.%_TAG%.rlog 2>&1
) else (
echo I did not find you in tty 1>> %_INFILE%.%_TAG%.rlog 2>&1
)
)
)
:eof
ECHO Done used - tag: %_TAG% and used file: %_INFILE% to drive %_drive%
ECHO Done used - tag: %_TAG% and used file: %_INFILE% to drive %_drive% 1>> %_INFILE%.%_TAG%.rlog 2>&1
timeout /t 8 /nobreak > NUL
c:
exit /b
::--------------------------------------------------------
::-- Function section starts below here
::--------------------------------------------------------
:mySetPathFunc - passing a variable by 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发布于 2016-02-08 19:06:31
955代的“延迟扩展”问题。
在block语句(a parenthesised series of statements)中,解析整个块,然后执行和。块中的任何%var%都将在解析该块时被该变量的值替换为-在该块被执行之前-同样的事情也适用于FOR ... DO (block)。
因此,在遇到IF时,将使用%variables%的值执行IF (something) else (somethingelse)。
解决此问题的两种常见方法是: 1)使用setlocal enabledelayedexpansion并使用!var!代替%var%来访问更改后的var值;2)调用子例程以使用更改后的值执行进一步的处理。
在涉及到标志的地方,情况再次发生变化。set "flag="将确保清除标志。set "flag=somethingelse"将确保设置了该值(该值不相关)。使用if defined flag (doiftrue) else (doiffalse)可以处理运行时(当前) flag的状态,而不是解析时的值。
在SO上查找成百上千条关于delayedexpansion的信息,以获得解决方案。
https://stackoverflow.com/questions/35267046
复制相似问题