-1

I want to make a script which copies a folder upon opening and saves it to a folder named with the date.

I have already seen this post and it worked.

It does not however include any subfolders which I need it to do.

I would also like to be able to run this script every day with Windows Task Scheduler.

Compo
  • 36,585
  • 5
  • 27
  • 39
S. Dik
  • 1
  • 1
  • 2
    To clarify, your question is 'How do I copy a directory tree'? Please read the help for the following commands by typing their names followed by `/?`, then choose the one you need with the appropriate options, `XCopy` & `RoboCopy`. You should test your script, and if it fails to work as intended, [edit your question](https://stackoverflow.com/posts/51689879/edit), adding your code and fully explain what happened when you ran it. You should then use your favourite search engine to learn how to run your script using the Task Scheduler. This site doesn't provide free tutorials we help to fix code – Compo Aug 04 '18 at 23:24

2 Answers2

1

Due to the issues with %DATE%, as noted in the comments to the answer from Cyberclops, here's single line command, which can be run from a batch file or Command Prompt, cmd.exe:

@PowerShell -NoP "XCopy \"C:\Users\DikS\Documents\" \"D:\Backups\DikS\Docs\$(Get-Date -F yyyy-MM-dd)\\\" /S/Y">Nul 

In the Command Prompt, you can optionally remove the leading @.


You can obviously run it from a Powershell Prompt or script too:

XCopy "C:\Users\DikS\Documents" "D:\Backups\DikS\Docs\$(Get-Date -F yyyy-MM-dd)\" /S/Y >$Null

Please remember to adjust the source, C:\Users\DikS\Documents, and destination, D:\Backups\DikS\Docs as necessary.

How to use the Task Scheduler is out of the scope of this site, which is about programming as opposed to the use of OS GUI tools.


Edit
If you are struggling with understanding what to replace, here's a batch script version of the upper code to make it easier for you:

@Echo Off
Set "SrcDir=C:\Users\DikS\Documents"
Set "DstDir=D:\Backups\DikS\Docs"
PowerShell -NoP "XCopy \"%SrcDir%\" \"%DstDir%\$(Get-Date -F yyyy-MM-dd)\\\" /S/Y">Nul

…and the lower code as a powershell script:

$SrcDir = "C:\Users\DikS\Documents"
$DstDir = "D:\Backups\DikS\Docs"
XCopy "$SrcDir" "$DstDir\$(Get-Date -F yyyy-MM-dd)\" /S/Y >$Null

Please note, in order to run a PowerShell script you may need to modify the execution policy on your system, the default setting is Restricted.
You can do that by typing the following in an PowerShell Prompt, Run as Administrator: Get-ExecutionPolicy, if Restricted is returned, you can type Set-ExecutionPolicy RemoteSigned and accept the prompt.
.

Compo
  • 36,585
  • 5
  • 27
  • 39
  • Thanks for your reply, but when I tried it it made a file called $Null and when i opened it it said 0 files copied, the solution from @Cyberclops worked. But anyway thanks for your help – S. Dik Aug 05 '18 at 12:19
  • @S.Dik, the only piece of code I provided which contained the string `$Null` was the command I asked you to run from `PowerShell`. I can guarantee that it will not write to a file named `$Null` unless you failed to replace your source and destination locations as explained in the answer. **_In fact I've just opened up somebody's Windows 7 unit, running PowerShell 2.0, and both pieces of code worked exactly as shown_**. Please note that the doublequotes and slashes are important, you should only replace the text strings I indicated, do not touch anything else unless you know what you're doing. – Compo Aug 05 '18 at 12:33
  • Also @S.Dik, as a side note, the `>Nul` in the upper code and `>$Null` in the lower code can be removed, _they only prevent the output to screen, and since Cyberclops answer doesn't output to Nul, you obviously don't need that feature anyhow!_ To re-iterate, you must not remove any or introduce doublequotes, this part must also remain `\$(Get-Date -F yyyy-MM-dd)\ `. **Please provide the exact code you used, and we could perhaps determine what happened.** – Compo Aug 05 '18 at 12:42
  • @S.Dik, I have further expanded my answer above to provide script versions which should make it simpler for you to update without making a mistake. I have also included information about how to modify the execution policy for `PowerShell` scripts, _because you cannot run them by default without doing so_. – Compo Aug 05 '18 at 13:19
0

You could try something like this ...

xcopy /s dir2copy "%DATE:/=-%\"

The "xcopy /s" says to copy the contents of a directory and all subdirectories.

dir2copy indicates the directory to copy.

%DATE% give today's date, but it will have slash (/) characters in it so it will create unwanted subdirectories.

The ":/=-" tells CMD.exe to substitute dashes (-) for all slashes (-) in the date.

You have to put the quotes around it because of the spaces in the date.

The backslash (\) at the end tells xcopy that the target is a directory and not a file.

If you don't want the Day in the date try this instead ...

xcopy /s dir2copy "%DATE:~4,2%-%DATE:~7,2%-%DATE:~10,4%\"
Cyberclops
  • 311
  • 2
  • 17
  • 1
    How do you know that there are forward slashes or spaces in the date? The `%DATE%` variable is User/PC/Locale changeable, for that reason any script using it is only safe to use under the that same environment in that moment in time. – Compo Aug 05 '18 at 00:16
  • @Compo good point. However, whatever the format is, if it has slashes and spaces in it or not, what I presented will still work. I was just trying to explain how what I presented worked. – Cyberclops Aug 05 '18 at 00:47
  • 1
    @Cyberclops, nope! The date variable can also have the day of week as the leading element. So you also need another set statement to remove that. – Squashman Aug 05 '18 at 01:14
  • @Squashman ... The original question was not specific as to the format of the date. I just gave the most expeditious answer I could at the time. However, I have updated my answer with another code snippet that removes the day from the date ... and it did not require me to use a set statement to do it. – Cyberclops Aug 05 '18 at 04:00
  • Read the answers in this question that use WMIC and Powershell to get the date and time in a region independent format: https://stackoverflow.com/q/203090/1417694 – Squashman Aug 05 '18 at 04:12