我正在尝试使用批处理脚本重命名多个文件。 例: 在28451WZ之前 在284_51WZ之后
到目前为止,我知道如何插入后缀和前缀,但无法在特定位置插入字符。
@echo off
for %%A in (*.pdf ^/ find /i /v ) do ren "%%~fA" "-%%~nA.*"
答案 0 :(得分:0)
使用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`
答案 1 :(得分:0)
对于那些想知道答案的人。
@echo on
setlocal EnableDelayedExpansion
for %%A in (*.pdf) do (
set "name=%%A"
set "name=!name:~0,5!_!name:~5!"
ren "%%A" "!name!"
)