Reputation: 11
I have a series of files that have names like this:
CHART_LOAN_6516_20130502.PDF
CHART_LOAN_2158_20130502.PDF
CHART_LOAN_78986_20130502.PDF
Each file always starts with CHART_LOAN_ but the next number is different and the last number is the date it was created.
I would like to insert an 0_ after CHART_LOAN_number_ for each file. As listed below:
CHART_LOAN_6516_0_20130502.PDF
CHART_LOAN_2158_0_20130502.PDF
CHART_LOAN_78986_0_20130502.PDF
Through my research I've found out to insert the char but not when the name is changing with each file.
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET old=CHART_LOAN_
SET new=CHART_LOAN_0_
for /f "tokens=*" %%f in ('dir /b *.jpg') do (
SET newname=%%f
SET newname=!newname:%old%=%new%!
move "%%f" "!newname!"
)
The above code will change the static portion of the file to something I want, but I don't know how to modify the code to compensate for the changing loan number.
Upvotes: 1
Views: 2761
Reputation: 79983
for /f "tokens=*" %%f in ('dir /b /a-d %old%*.pdf^|find /i /v "%new%"') do (
should see you right – but I'd add an ECHO
temporarily before the MOVE
to check it's working properly.
Interesting that you'd use *.jpg
in your original though. Rather unlikely to work, I'd guess.
The renamed files will match the pattern ..._0_...
but the originals won't – the closest they'll come is with CHART_LOAN_0.pdf
. Hence, you find all the filenames that don't match the new mask (/v
) case-insensitive (/i
). The /a-d
guards against the remote possibility of a directory name that matches the old
mask. The caret before the pipe tells CMD that the pipe is part of the command to be executed by the for
.
Upvotes: 1
Reputation: 37569
try this:
@echo off &setlocal
for /f "tokens=1-3*delims=_" %%i in ('dir /b /a-d *.pdf ^| find /i /v "_0_"') do ren "%%i_%%j_%%k_%%l" "%%i_%%j_%%k_0_%%l"
Upvotes: 1