【问题标题】:How to use batch file to insert a character after 5th character如何使用批处理文件在第 5 个字符后插入一个字符
【发布时间】:2016-03-17 15:15:43
【问题描述】:

我正在尝试使用批处理脚本重命名多个文件。 例子: 28451WZ之前 284_51WZ之后

到目前为止,我知道如何插入后缀和前缀,但是在特定位置插入字符时遇到了麻烦。

@echo off for %%A in (*.pdf ^/ find /i /v ) do ren "%%~fA" "-%%~nA.*"

【问题讨论】:

    标签: batch-file rename file-rename


    【解决方案1】:

    使用 cmd 的内置字符串替换功能。

    @echo off
    
    echo Setting envar to 'helloworld'
    set "envar=helloworld"
    echo envar=`%envar%`
    echo.
    
    echo Using substr
    echo   example: `%envar:~0,5%_%envar:~5%`
    echo.
    
    echo.
    echo Additional notes:
    echo.
    echo If you are _not_ using `setlocal EnableDelayedExpansion` you will need
    echo to assign the result to a new variable using:
    echo set "envar2=%envar:~0,5%_%envar:~5%"
    echo     Note the `_` here   ^^
    set "envar2=%envar:~0,5%_%envar:~5%"
    echo.
    echo   envar2=`%envar2%`
    echo.
    
    echo If you _are_ using `setlocal EnableDelayedExpansion` you will need
    echo to use variable expansion (ie: use the `!` instead of the `%%`):
    echo set "envar=!envar:~0,5!_!envar:~5!"
    setlocal EnableDelayedExpansion
    set "envar=!envar:~0,5!_!envar:~5!"
    echo.
    echo   envar=`%envar%`
    echo.
    endlocal
    

    上面的批处理文件产生以下输出:

    Setting envar to 'helloworld'
    envar=`helloworld`
    
    Using substr
      example: `hello_world`
    
    
    Additional notes:
    
    If you are _not_ using `setlocal EnableDelayedExpansion` you will need
    to assign the result to a new variable using:
    set "envar2=%envar:~0,5%_%envar:~5%"
        Note the `_` here   ^
    
      envar2=`hello_world`
    
    If you _are_ using `setlocal EnableDelayedExpansion` you will need
    to use variable expansion (ie: use the `!` instead of the `%`):
    set "envar=!envar:~0,5!_!envar:~5!"
    
      envar=`hello_world`
    

    【讨论】:

    • 非常感谢您的回复!!由于您的示例,我能够弄清楚!
    【解决方案2】:

    对于那些想知道答案的人。

    @echo on
    setlocal EnableDelayedExpansion
    for %%A in (*.pdf) do (
    set "name=%%A" 
    set "name=!name:~0,5!_!name:~5!"
    ren "%%A" "!name!"
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-26
      • 1970-01-01
      • 2022-08-06
      • 2015-12-04
      • 2022-07-30
      • 2017-01-13
      • 2017-08-24
      • 2020-04-10
      相关资源
      最近更新 更多