gpt4 book ai didi

windows - 用 'comma' 替换动态字符串 - Windows 批处理文件

转载 作者:可可西里 更新时间:2023-11-01 11:43:36 24 4
gpt4 key购买 nike

我正在尝试编写一个 Windows 批处理文件以获得给定输入的以下输出

输入:

//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' 将是动态变化的值,为了用 'comma' 替换它它应该与 !lastPart! 一起工作,但它不是工作和输出是单词 'lastPart'我们不能使用 %lastPart%,因为它是动态变量。

请帮我用“逗号”替换动态字符串。

请帮帮我!

最佳答案

可以直接用%%~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!
)
)
)

关于windows - 用 'comma' 替换动态字符串 - Windows 批处理文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52482593/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com