add suffix on files with similar file types!

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
jamesfui
Posts: 50
Joined: 27 Mar 2010 23:00

add suffix on files with similar file types!

#1 Post by jamesfui » 12 May 2010 04:31

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:

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

Re: add suffix on files with similar file types!

#2 Post by avery_larry » 12 May 2010 11:48

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

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: add suffix on files with similar file types!

#3 Post by !k » 13 May 2010 07:36

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

Post Reply