1

I'm trying to preserve the dates of files that I'm backing up onto an external drive, in the unlikely event that the dates get messed up for whatever reason (I had a previous experience where I lost date information and had no backup). I'm doing this through a batch file containing the following:

@ECHO OFF
cd E:\PCBackup
dir /s > dirlist.txt

I would simply run this batch file after running my backup using FreeFileSync. Then, if I need to, I can search the txt file for the filename and see its corresponding date.

However, when this batch file runs, if there is a previous dirlist.txt, then it is overwritten with the new dirlist.txt. So, in a scenario where the dates get messed up and I don't yet realize it, if I run this batch file, it will overwrite the previous dirlist.txt with one that has the messed up dates, and I'd lose the date information!

So, what I think I want it to do is, if dirlist.txt already exists, then create a new one, say something like dirlist1.txt, so that I can have several "backups" of the text file that I can manually delete if necessary.

I've seen that one can instead use >> with something like dir /s >> dirlist.txt to append to an existing file instead of overwriting, but I don't want to append if I don't have to, I'd still like to create a new file.

Is there a way to accomplish this? I'm also open to alternative/simpler ways of preserving the dates, if there are any. Please keep in mind that I know little about CMD commands or programming, outside of a computer science course I took years ago. Thank you.

  • 2
    I suggest you get the current date and time into a variable and use that with the filename you creating. See this [question](https://stackoverflow.com/questions/203090/how-do-i-get-current-date-time-on-the-windows-command-line-in-a-suitable-format) – Squashman Jul 07 '21 at 21:13
  • @Squashman Good suggestion – AlteredReality Jul 07 '21 at 22:31

1 Answers1

0

You will be told there are umpteen duplicate ways to do this so in this 22 nd year of the 1st century :-) Windows has no native way of returning a sequential Iso Date the primary answer will be use powershell and for my locale it needs to be called in a suitable format, introducing a delay.

powershell get-date -format "{yyyy-MMM-ddTHH_mm+01Z}" 

Note:- colons : are not allowed, and for me 20 seconds later on one machine (but it does get faster with use) and 12-5 seconds later on this one, I get 2021-07-07T21_55+01Z but actually its now 2021-Jul-07 21:56

I have found that the MakeCab method is faster and reliable but again the format is not pure sequencing and the Jul will NOT appear before Dec in a file list without significant batch file processing.

2021-Dec-31 23:00:00.txt
2021-Jul-08 21:54:20.txt

So in a .cmd I prefer a more instant result thus my clock is set to International dates (You will need to look at your LOCALE clock setting bottom right for your own construction.)

set isodate=%date:~0,10%

instantly returns isodate=2021-07-07 and I can then use that for filename

@ECHO OFF
cd E:\PCBackup
set "isodate=%date:~0,10%"
dir /s > %isodate%-dirlist.txt

dir returns includes 2021-07-07-dirlist.txt

If you want to run several times in a day use

@ECHO OFF
cd E:\PCBackup
set "isodate=%date:~0,10%"
set "isotime=%time:~0,2%-%time:~3,2%-%time:~6,2%"
dir /s > %isodate%T%isotime%+01Z-dirlist.txt

Amend that any way you wish for your timezone, thus your own clock whatever your date format be it :-

31/2021/12

look at the way I split %time :~ start@base 0 , # of chars %-

one example for an "English" clock date of 31/12/2021 would be simply reverse to "isodate=%date:~6,4%-%date:~3,2%-%date:~0,2%"

For American %date%=Thu 07/08/2021 use

"isodate=%date:~10,4%-%date:~4,2%-%date:~7,2%"

K J
  • 8,045
  • 3
  • 14
  • 36
  • Oh, I hadn't thought to do this! This is a good method because it's easier to quickly see when the file was from, as well as not overwriting an older one. Thank you for providing examples as well – AlteredReality Jul 07 '21 at 22:29
  • The `%date%` variable is region and locale dependent. It does not output the same for everyone. In my case, your solution would fail because my date variable outputs **Wed 07/07/2021**. This is why I linked to the question in my comment above. – Squashman Jul 07 '21 at 22:50
  • I guess I misunderstood your explanation. Regardless you answer is a duplicate of the question I already linked to in my comment above. Here on StackOverFlow our normal protocol is too lead a user to a question that already has an answer to that topic instead of having dozens of questions and answers with the same topic. Normally questions like this would be voted to be closed and then eventually the system deletes those questions after a while. – Squashman Jul 07 '21 at 22:55