2015-12-11 67 views
1

我正在嘗試使用批處理腳本來重命名多個文件。 例子: 後284_51WZ如何使用批處理文件在第5個字符後插入字符

到目前爲止 之前28451WZ,我知道如何插入一個後綴,前綴,但有麻煩插入特定位置的字符。

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

回答

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!" 
) 
相關問題