1

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

  1. abc.txt rename to xyz20190715_1.txt (xyzyyyymmdd_1.txt)
  2. 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

  1. abc.txt rename to xyz20190715_1.txt

  2. pqr.txt rename to def20190715_1.txt

Prakash Mhasavekar
  • 574
  • 1
  • 5
  • 16
  • 1
    where do `xyz` and `def` come from? – Stephan Jul 15 '19 at 17:40
  • 1
    Since we can't see what the code did on your system, you need to tell us. You should also specify the date-format you are using by showing a sample of the value of `%date%`. – Magoo Jul 15 '19 at 17:42
  • xyz and def is sample name that should include at start of file name – Prakash Mhasavekar Jul 15 '19 at 17:57
  • I understood, what it does, but where does it come from? – Stephan Jul 15 '19 at 17:59
  • it need to be harcoded in batch file – Prakash Mhasavekar Jul 15 '19 at 18:00
  • (*sigh*) How to know that `abc` should be translated to `xyz` while `pqr` should become `def` - if you have a lot of files, we will have to know how that substituion should work. – Stephan Jul 15 '19 at 18:09
  • Suppose I have total 7 files with different name. Have to loop through all files and according to that file names change there names1) contract.txt to DNSMyyyymmdd_x.DAT 2) security.dat to NSMyyyymmdd_x.DAT 3) Participant.txt to PMyyyymmdd_x.DAT 4) SCRIP_master.txt to BSMyyyymmdd_x.DAT 5) DPRxxxx to BSE_YearlyHLyyyymmdd_x.DAT 6) fo_participant.txt DPMyyyymmdd_x.DAT 7) spd_ contract.txt to SCMyyyymmdd_x.DAT – Prakash Mhasavekar Jul 15 '19 at 18:13
  • have only 7 files – Prakash Mhasavekar Jul 15 '19 at 18:14

3 Answers3

1

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"

1

"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"
Stephan
  • 53,940
  • 10
  • 58
  • 91
  • @PrakashMhasavekar, the code above uses `_x.DAT`, as in your comment. If you wanted `_1.txt` as in your question, please adjust the `7` lines as necessary. – Compo Jul 15 '19 at 19:33
  • how to get next date in same format. I'm trying to add with +1 but it is not working – Prakash Mhasavekar Jul 16 '19 at 12:55
  • Doable in pure batch, but I suggest to take the help of some other language (like Powershell) to keep your sanity intact. There are [a lot of similar questions](https://stackoverflow.com/search?q=%5Bbatch-file%5D+get+tomorrows+date) - take your choice. – Stephan Jul 16 '19 at 13:14
  • Problem is my one file comes with next day's date DPR_ddmmyy format and second with current date script_ddmmyy. I managed to find and rename Script_ddmmyy which comes with current date. Now one file remaining with tommorrows date... – Prakash Mhasavekar Jul 16 '19 at 13:32
  • I don't really understand - and code is incredibly hard to read in comments. Please ask a new question instead (as actually it's another problem). – Stephan Jul 16 '19 at 13:45
  • Solved Sir... using (`"powershell (Get-Date).AddDays(1).ToString('ddMMyy')"`) I got next date.. Thank you for your support @Stephan Sir – Prakash Mhasavekar Jul 16 '19 at 13:47
  • 1
    You're welcome. Btw: don't mess with the system variable `%date%` - choose another name (like `%tomorrow%`) – Stephan Jul 16 '19 at 13:53
  • Yes sir. I added variable as %nextDate%. – Prakash Mhasavekar Jul 16 '19 at 13:56
1
@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
Aanand
  • 21
  • 5