How to create a subfolder based on current date?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
OldGrantonian
Posts: 4
Joined: 01 Jun 2018 12:47

How to create a subfolder based on current date?

#1 Post by OldGrantonian » 01 Jun 2018 12:56

I want to use a command to copy the entire contents of a folder (including subfolders) to a different drive.

For example:
copy c:\evernote e:\evernote\<datestamp>

where <datestamp> is a new folder whose name represents the current date in this format:

2018_05_21

So, the target evernote folder will contain a series of subfolders, one folder for each date.

For me, I don't think it matters if the command is copy, robocopy, xcopy, or whatever.

Added: There's no need to allow for different time zones :)

Thanks.
Last edited by OldGrantonian on 02 Jun 2018 01:50, edited 1 time in total.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: How to create a subfolder based on current date?

#2 Post by Squashman » 01 Jun 2018 14:12

Lots of threads on this forum that talk about getting the date into a normalized format. Shouldn't be to hard to search the forums.

OldGrantonian
Posts: 4
Joined: 01 Jun 2018 12:47

Re: How to create a subfolder based on current date?

#3 Post by OldGrantonian » 02 Jun 2018 01:25

.
Hi Squashman. Thanks for response.

I've already done searches, but I'll do these again and post the search results here.

That might help other newbies like you and me. I suppose I might be a "born-again" newbie - after 26 years :)
.

OldGrantonian
Posts: 4
Joined: 01 Jun 2018 12:47

Re: How to create a subfolder based on current date?

#4 Post by OldGrantonian » 02 Jun 2018 02:55

.
Hi @Squashedman. Here are the results of my searches. I'm sorry I wasn't able to find anything. But I hope the results help you and other newbies.

Search string: folder timestamp
252 matches
I scanned all the post headings. I only opened a post if the heading looked promising. No posts referred to folder timestamps, only file timestamps.

No post referred to creating a new folder in the target.
-----
For info, to help other people, I think a better search string might have been "create new folder timestamp", because I thought it would be more "restrictive", but still valid. But that still produces 252 results :-)
----
Search string: create new folder timestamp
Ignored: create new folder

So that explains the identical results. The first query was ignoring "folder", and the second query was ignoring "create new folder". Interesting concept :-)
---
I then tried a google search using this forum link:
site:www.dostips.com folder timestamp robocopy OR xcopy

That produced "about 46" results. Based on the titles, plus the 3-line preview, none of these helped. (I suppose some people on this forum might say that I should read every line of every search result - but I don't really have the time for that - sorry.)

I tried StackOverflow. I'll include the links I searched, because these might help dozens of other forum users duplicating each other's searches.

The following list has each search term followed by a link resulting from that term.
---
difference xcopy robocopy
https://stackoverflow.com/questions/241 ... d-robocopy
---
filename timestamp
https://stackoverflow.com/questions/110 ... t/11037921
---
xcopy append date to directory path
https://stackoverflow.com/questions/169 ... h-in-xcopy
---
robocopy create folder in destination with date
https://stackoverflow.com/questions/213 ... -with-date
---
Stack Overflow is a great forum. The guys are super-helpful, and you can see that each post gets dozens of replies plus comments. The only problem for newbies such as you and me is that every answer is different, and in many cases the OP does not say which answer solved her problem. The other problem of course, is that they speak to each other in hexadecimal rather than English :-)

There are still a lot of other forums to search. I'll report back with the answers in order to help other people on this forum.

Thanks.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: How to create a subfolder based on current date?

#5 Post by Compo » 02 Jun 2018 06:01

Here's a replication of my answer to you on StackOverflow.

Because you're using RoboCopy for the copying process, here's a method which also uses it to determine the date:

Code: Select all

@Echo Off
Set "sd=C:\EverNote"
Set "dd=E:\EverNote"
Set "ds="
If Not Exist "%sd%\" Exit /B
For /F "Tokens=1-3Delims=/ " %%A In ('RoboCopy/NJH /L "\|" Null'
) Do If Not Defined ds Set "ds=%%A_%%B_%%C"
If Not Defined ds Exit /B
RoboCopy "%sd%" "%dd%\%ds%" /E
You can edit the locations in lines 2 and 3, (but do not remove the existing doublequotes or introduce your own, and do not include trailing backslashes with those folder names)

OldGrantonian
Posts: 4
Joined: 01 Jun 2018 12:47

Re: How to create a subfolder based on current date?

#6 Post by OldGrantonian » 02 Jun 2018 07:33

.
As I mentioned on StackOverflow, your script worked perfectly. Thanks.
.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: How to create a subfolder based on current date?

#7 Post by Squashman » 02 Jun 2018 09:21

Shocking with all that research, you could not come up with a single line of code on your own to make a directory named with the current date.

Post Reply