-2

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

For example:

robocopy 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. Each subfolder will contain the files that were copied from source to target on that date.

For me, I don't think it matters if the command is copy, robocopy, xcopy, or whatever. However, I believe that robocopy is most up-to-date, and therefore most likely to be of future use to me.

BTW: To keep this simple, there's no need to allow for different time zones, and no need for "automation" - I will run the BAT file myself, and add automation later.

Thanks.

OldGrantonian
  • 557
  • 1
  • 5
  • 22

4 Answers4

1

When writing in Batch, you should be able access the environment variable %date%.

This is a string of the current date.

You can try to use echo %date% to see what it looks like, and then split and recombine the string, so it fits your need.

Beware though, that the format of the string is dependent on how you set your datetime settings on your computer. If you change to use another format on the computer, the format of %date% will also change.

1

To generate a date in a certain format without worring about localization settings you can use this:

@echo off
for /f %%# in ('wMIC Path Win32_LocalTime Get /Format:value') do @for /f %%@ in ("%%#") do @set %%@
:echo %day%
:echo %DayOfWeek%
:echo %hour%
:echo %minute%
:echo %month%
:echo %quarter%
:echo %second%
:echo %weekinmonth%
:echo %year%

if %day% lss 10 set day=0%day%
if %month% lss 10 set month=0%month%
set f_date=%year%-%month%-%day%
echo %f_date%

The %date% variable format depends on the settings in the control panel and it's value will be different on different machines. For more ways to get a formatted date you can check this

npocmaka
  • 51,748
  • 17
  • 123
  • 166
  • From all the useful replies, I'm starting here. Copied your entire text into "date.bat", saved, and clicked on filename in File Explorer. Console opened and closed. Added "pause" at end (after googling). I only see "2018-06-02". Don't see any of the earlier "echo" results, for example ":echo %day%". Pls advise. – OldGrantonian Jun 02 '18 at 10:34
  • Sorry. I forgot a colon is the comment character. It used to be "REM", 26 years ago :-) – OldGrantonian Jun 02 '18 at 11:08
  • 1
    @OldGrantonian - colon is not exactly a comment but a label. I'd should use a `::` instead of a single semi column. Using labels as a comments is sometimes cleaner and it's processed faster but can be risky if it is enclosed in in brackets.You can find more information here - https://stackoverflow.com/questions/4094699/how-does-the-windows-command-interpreter-cmd-exe-parse-scripts/4095133 – npocmaka Jun 02 '18 at 13:37
  • 2
    @OldGrantonian - it's not good idea to name your bat file `date` as it could collide with the internal date command. – npocmaka Jun 02 '18 at 13:38
1

This script uses PowerShell to get the datestamp in the desired format:

@Echo off
for /f "usebackqdelims=" %%D in (
  `powershell -Nop -C "(Get-Date).ToString('yyyy_MM_dd')"`
) Do Set "datestamp=%%D"
Echo robocopy c:\evernote\ e:\evernote\%datestamp%\

Sample output:

robocopy c:\evernote\ e:\evernote\2018_06_02\
1

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

@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)

Compo
  • 30,301
  • 4
  • 20
  • 32