我正在尝试编写Windows批处理文件以获取给定输入的OUTPUT以下
输入:
//Dev-420/PAVAN/src/main/java/test/abcd/mnop/HealthCheck.java - edit change 1111111
//Dev-420/PAVAN/src/main/java/test/abcd/mnop/HealthStatus.java - edit change 1111111
//Dev-420/PAVAN/src/main/java/test/xyz/Relations.java - edit change 1111111
输出:
target/classes/test/abcd/mnop/, target/classes/test/abcd/mnop/, target/classes/test/xyz
下面是我使用的脚本,但是替换文件名无效。
@echo off
set "File2Read=files_list.tmp"
SET "BINARY_PATH="
set "FILENAME="
setlocal EnableDelayedExpansion
set "BINARY_FILENAME="
set "lastPart="
set "replaceBinaryPath=target/classes/"
SET "replaceBinaryFileName=,"
set "basePath=//ATT/Dev-420/PAVAN/src/main/java/"
for /f "delims=" %%a in ('Type "%File2Read%"') do (
set "line=%%a"
for /f "tokens=1,2,3,4,5 delims= " %%a in ("!line!") do set FILENAME=%%a
set "BINARY_FILENAME=!FILENAME:%basePath%=%replaceBinaryPath%!"
if not !FILENAME!==!BINARY_FILENAME! (
for %%a in ("!BINARY_FILENAME!/.") do set "lastPart=%%~nxa"
set "BINARY_PATH=!BINARY_FILENAME:!lastPart!=%replaceBinaryFileName%!"
echo !BINARY_PATH!
)
)
endLocal
在这里,一切都按预期进行,除了下面的声明
set "BINARY_PATH=!BINARY_FILENAME:!lastPart!=%replaceBinaryFileName%!"
由于'lastPart
'是动态变化的值,为了将其替换为'逗号',本来可以与!lastPart!
一起使用,但它不起作用并且输出为单词{{1 }}
我们不能使用‘lastPart’
,因为它是动态变量。
请帮助我将动态字符串替换为“逗号”。
请在这里帮助我!
答案 0 :(得分:1)
您可以直接使用%%~nxa
表达式,我只将参数名称更改为%%C
..
for /f "delims=" %%a in ('Type "%File2Read%"') do (
set "line=%%a"
for /f "tokens=1,2,3,4,5 delims= " %%a in ("!line!") do set FILENAME=%%a
set "BINARY_FILENAME=!FILENAME:%basePath%=%replaceBinaryPath%!"
if not !FILENAME!==!BINARY_FILENAME! (
for %%C in ("!BINARY_FILENAME!/.") do (
set "BINARY_PATH=!BINARY_FILENAME:%%~nxC=%replaceBinaryFileName%!"
echo !BINARY_PATH!
)
)
)
答案 1 :(得分:1)
由于@jeb的版本对我不起作用,因此可以选择
%%~dpa
的使用将非Windows路径分隔符/
更改为\
时,它会得到纠正。:: Q:\Test\2018\09\24\SO_52482593.cmd
@echo off & setlocal EnableDelayedExpansion
set "File2Read=files_list.tmp"
set "BINARY_PATH="
set "replaceBinaryPath=target/classes/"
set "replaceBinaryFileName=, "
set "Output="
for /f %%a in ('Type "%File2Read%"') do (
for /f "tokens=5* delims=\/" %%b in ("%%~dpa") do (
Set "Output=!Output!%replaceBinaryFileName%%replaceBinaryPath%%%c"
)
)
Set "Output=%Output:\=/%"
Echo %Output:~2%
endLocal
示例输出:
> Q:\Test\2018\09\24\SO_52482593.cmd
target/classes/test/abcd/mnop/, target/classes/test/abcd/mnop/, target/classes/test/xyz/