Reputation: 592
I have 7 files. in bat file I want to check name of files and then raname with current date. just like an if
/else
condition
Example
abc.txt
rename to xyz20190715_1.txt
(xyzyyyymmdd_1.txt)pqr.txt
rename to def20190715_1.txt
(defyyyymmdd_1.txt)I'm new to batch files and tried this:
for /f "tokens=1-5 delims=/ " %%d in ("%date%") do rename
"abc.txt" %%e-%%f-%%g.dat
Expected output
abc.txt
rename to xyz20190715_1.txt
pqr.txt
rename to def20190715_1.txt
Upvotes: 1
Views: 495
Reputation: 21
@echo off
for /f "usebackq" %%B in (`"powershell (Get-Date).ToString('yyyyMMdd')"`) do set "datetime=%%B
echo Today's Date %datetime%
ren "contract.txt" "DNSM%datetime%_x.DAT"
ren "security.DAT"" "NSM%datetime%_x.DAT"
ren "Participant.txt" "PM%datetime%_x.DAT"
ren "SCRIP_master.txt" "BSM%datetime%_x.DAT"
ren "DPRxxxx" "BSE_YearlyHL%datetime%_x.DAT"
ren "fo_participant.txt" "DPM%datetime%_x.DAT"
ren "spd_ contract.txt" "SCM%datetime%_x.DAT"
pause
exit
Upvotes: 1
Reputation: 56154
"to loop through files" is something like
for %%I in (*.txt) do echo %%I
that's why I asked how to know the substitutions.
As there seems to be no rule, you have to rename each file individually (no big harm with only 7 files, but with a hundred ore more it would drive you crazy). I also used a differnt way to get the date string (independent of locale settings):
@echo off
for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /format:list') do set "datetime=%%I"
set "datetime=%datetime:~0,8%"
ren "contract.txt" "DNSM%datetime%_x.DAT"
ren "security.DAT"" "NSM%datetime%_x.DAT"
ren "Participant.txt" "PM%datetime%_x.DAT"
ren "SCRIP_master.txt" "BSM%datetime%_x.DAT"
ren "DPRxxxx" "BSE_YearlyHL%datetime%_x.DAT"
ren "fo_participant.txt" "DPM%datetime%_x.DAT"
ren "spd_ contract.txt" "SCM%datetime%_x.DAT"
Upvotes: 1
Reputation: 41
If the question is to get the actual date, then for today's date it's better to do the following:
for /f %%a in ('wmic path win32_LocalTime Get Day^,Month^,Year /value') do >nul set "%%a"
set Month=00%Month%
set Month=%Month:~-2%
set Day=00%Day%
set Day=%Day:~-2%
set today=%Year%%Month%%Day%
Otherwise you need to ask a real question, which starts with "how do I"
Upvotes: 1