Page 1 of 1

add suffix on files with similar file types!

Posted: 12 May 2010 04:31
by jamesfui
hi dostips batch scriptwriter.
what is the batch scripts that add suffix "_001" to all the files with the same file types ".doc" in a specified folder!

for example;
c:\documents\office\jan.doc
c:\documents\office\feb.pdf
c:\documents\office\mar.doc
c:\documents\office\apr.doc

add suffix "_001", "_002", "_003", etc..
as shown below;

c:\documents\office\jan_001.doc
c:\documents\office\feb.pdf
c:\documents\office\mar_002.doc
c:\documents\office\apr_003.doc

thanks :wink:

Re: add suffix on files with similar file types!

Posted: 12 May 2010 11:48
by avery_larry
It'll be much easier if you can start with _100 and count up instead of _001, otherwise you have to pad the leading zeros.

*untested*

Code: Select all

set /a idx=100
for %%a in (*.doc) do call :process "%%~a"
goto :eof

:process
ren "%~1" "%~n1_%idx%%~x1"
set /a idx+=1
goto :eof

Re: add suffix on files with similar file types!

Posted: 13 May 2010 07:36
by !k

Code: Select all

setlocal enableextensions
set /a idx=1
for /f "delims=" %%a in ('dir /b/s /a-d /od /tc *.doc') do call :process "%%~a"
goto :eof

:process
set "idx00=00%idx%"
set "idx00=%idx00:~-3%"
ren "%~1" "%~n1_%idx00%%~x1"
set /a idx+=1
goto :eof