I have a file with a filename like this: Summary_20022015.xlsx
What would i like to do is to use batch file to rename.
1) FROM: Summary_20022015.xlsx
To: Summary_20150220.xlsx
2) FROM: Summary_25022015.xlsx
To: Summary_20150225.xlsx
The original is (Filename)_DDMMYYYY.xlsx
What I need is to keep the (filename) and change to YYYYMMDD instead. Just rearranging it using the actual filename.
Results is (Filename)_YYYYMMDD.xlsx
As I have multiple files in the folder. It would be troublesome to manually renaming all of them.
This worked on my test files. You are going to need to set where the files are located before you run the script. Also, it may be a good idea to put an echo
in front of the ren
statement at the bottom before you run it so that you make sure you are getting the output that you want.
@echo off
setlocal enabledelayedexpansion
:: While it's a good idea to never have spaces in your paths, sometimes it's unavoidable, so use quotes
:: The quotes being where they are will preserve the spaces without including the quotes in the value
set source_files="C:\path\to\where\the files\are"
:: Go into the %source_files% path, but remember where we were for later
pushd %source_files%
:: The /b option will only process the file names and not the other things that appear with dir output
:: Split each filename on underscores and periods
:: %%A is going to be the word Source and %%C is going to be xlsx
for /F "tokens=1-3 delims=_." %%A in ('dir /b') do (
set base_date=%%B
set date_day=!base_date:~0,2!
set date_mon=!base_date:~2,2!
set date_year=!base_date:~4,4!
ren %%A_%%B.%%C %%A_!date_year!!date_mon!!date_day!.%%C
)
:: Go back to the path we were in before the script ran
popd