批处理脚本 - 如何在定界符之前替换字符

发布于 2025-02-07 03:42:18 字数 1141 浏览 1 评论 0原文

我是新手的批次脚本,对我来说,很多事情仍然不是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

微凉徒眸意 2025-02-14 03:42:18
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

SET "sourcedir=u:\your files"
SET "replaceme=aa"
SET "replacement=duh"

FOR /f "delims=" %%b IN ('dir /b /a-d "%sourcedir%\*" ') DO (
 ECHO found filename "%%b"
 rem store name part before "_" in `fileprefix
 FOR /f "tokens=1*delims=_" %%q IN ("%%~nb") DO (
  ECHO q=%%q r=%%r
  SET "fileprefix=%%q"
  rem is the 2-character string at the end of fileprefix "aa"? - note: /i means case-insensitive
  IF /i "!fileprefix:~-2!"=="%replaceme%" (
   ECHO %%q ends %replaceme%
   ECHO so new name is !fileprefix!, except the last 2 characters + replacement + _ + %%r
   ECHO and extension is %%~xb
   ECHO REN "%%b" "!fileprefix:~0,-2!%replacement%_%%r%%~xb"
  )
 )
 ECHO ----------------------
)

GOTO :EOF

这应该证明我如何解决这个问题。这些步骤是echo ed-显然,一旦理解该方法,就可以删除这些echo es。

请注意,也可以使用“%sourcedir%\*%替换%_*”作为FileMask,这将减少检测到的不符合所需标准的文件名的发生率。

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

SET "sourcedir=u:\your files"
SET "replaceme=aa"
SET "replacement=duh"

FOR /f "delims=" %%b IN ('dir /b /a-d "%sourcedir%\*" ') DO (
 ECHO found filename "%%b"
 rem store name part before "_" in `fileprefix
 FOR /f "tokens=1*delims=_" %%q IN ("%%~nb") DO (
  ECHO q=%%q r=%%r
  SET "fileprefix=%%q"
  rem is the 2-character string at the end of fileprefix "aa"? - note: /i means case-insensitive
  IF /i "!fileprefix:~-2!"=="%replaceme%" (
   ECHO %%q ends %replaceme%
   ECHO so new name is !fileprefix!, except the last 2 characters + replacement + _ + %%r
   ECHO and extension is %%~xb
   ECHO REN "%%b" "!fileprefix:~0,-2!%replacement%_%%r%%~xb"
  )
 )
 ECHO ----------------------
)

GOTO :EOF

This should demonstrate how I might approach this problem. The steps are echoed - obviously, those echoes 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文