批处理脚本 - 如何在定界符之前替换字符
我是新手的批次脚本,对我来说,很多事情仍然不是100%可以理解的。
我找到了一些来源( source1 , source2 )我仍然不知道如何在定界线之前替换字符。
我有这样的文件:
File 1 : rem1020aa_10.zip
File 2 : com12909aa_32.zip
File 3 : fig129439aa_324.zip
您可以看到我的字符串可以具有不同的长度。我要做的就是用其他东西替换“ AA”。
更新 - 对不起,我想提到我想在_定界符之前替换。
for for for for for for for:rem1020 duh _10.zip
观看我的当前代码:
@echo off
setlocal enableDelayedExpansion
set arg1=%1
for /f "delims=" %%b in ('dir /b /s /a-d "C:\Test"') do (
set "arg1=%%~nxc"
echo !arg1:~-6,2!
)
我得到的结果:
09
43
0a
我知道在这种情况下,它将无法使用,因为长度有所不同,但这是我尝试过的多个方法之间的一种方法。
我尝试了第一个答案来自 source2 ,但我仍然可以t找到一种方法。
I'm new in batch scripting and a lot of things are still not 100% understandable for me.
I've found some sources (Source1, Source2) about substrings but I still can't figure how to replace characters before a delimiter.
I have files called like this :
File 1 : rem1020aa_10.zip
File 2 : com12909aa_32.zip
File 3 : fig129439aa_324.zip
As you can see my strings can have different lengths. All I want to do is replace the "aa" with something else.
Update - Sorry forgot to mention that I want to replace before the _ delimiter.
For exemple : rem1020duh_10.zip
Watch my current code :
@echo off
setlocal enableDelayedExpansion
set arg1=%1
for /f "delims=" %%b in ('dir /b /s /a-d "C:\Test"') do (
set "arg1=%%~nxc"
echo !arg1:~-6,2!
)
The result I get :
09
43
0a
I know that it will not work in that case because the lengths are different, but it's one method between multiple I tried.
I tried the first answer from Source2 too but still I can't find a way to do that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这应该证明我如何解决这个问题。这些步骤是
echo
ed-显然,一旦理解该方法,就可以删除这些echo
es。请注意,也可以使用
“%sourcedir%\*%替换%_*”
作为FileMask,这将减少检测到的不符合所需标准的文件名的发生率。This should demonstrate how I might approach this problem. The steps are
echo
ed - obviously, thoseecho
es could be deleted once the method is understood.Note that it would also be possible to use
"%sourcedir%\*%replaceme%_*"
as the filemask which would reduce the incidence of filenames detected that do not fit the criteria required.