Create a Windows Daily Backup Script
This tutorial will show you how to use a simple Windows Batch file and the task scheduler to create an automated daily backup. We will use the date variable to backup the files into folders with the first three letters of the day as the folder name.
First you will need to open notepad. The easiest way is going to start > run and typing notepad.

We will use notepad to create are batch file. A windows batch file is a set of instructions that tells the computer what to do. All we really need to do is add the command to copy the folder that we want to backup. I have a folder called ‘test’ that I want to backup to a folder called ‘backup on a network drive which I have mapped as the x drive. It only takes one simple command to do that actual backup. (note: there is no | in the command next the the word date, that is just the cursors)

Now I will explain the command “xcopy c:\test\* x:\backup\%date:~0,3%\* /Y /Q /S”
The xcopy commands will copy files and folders. The syntax for the command is xcopy [destination] [arguments]. Where source is the folder that you want to backup and destination is the backup on the network drive. The arguments tell xcopy how to handle certain situations. For my script I used /Y to suppress prompting when overwriting files. If you do not include this the script will ask you before copying each file, that wouldn’t be automated. /Q doesn’t display the file names while copying. /S copies directories and sub directories. You can find more arguments by opening the command windows and typing xcopy /?.
Where you see \%date:~0,3%\ is the name of the destination folder. If you didn’t want to change the name daily you could simple call it \backup\ or something like that. The %date% is a variable. If you type %date at the command line, you will get the current date and time output. The :~0,3 tells the command line to return the first three letters of that date (0-3) and trim off the rest.
Once you have made the changes that you nee, save the file as backup.bat

Now Open the Control Panel and select Scheduled Tasks

Now we want to add a new task so click the “Add Scheduled Task” button.

Click next and then browse. Locate the backup script you just saved and click open.

Select perform this action daily and then click the next button.

Select enter the time you want the task to run at in the start time box. Then make sure daily is selected and change the date to the day you would like this task to start running on.

Enter your password and click next. (note: if you do not have a password, you may have problems with the scheduled tasks running correctly. I recommend adding a password to your account, or setting up a separate account to run scripts.)

Click finish. Now we can test the backup script. Located the script in the scheduled tasks folder, Right click it and select run.

You should see the command window open. When the task is finished, browse to your backup folder and verify that the files have been copied.

This is a very basic idea of how to use the task scheduler and batch files to make backups.
Popularity: 92% [?]











on May 17th, 2007 at 8:26 am
i am unable to copy all files & folders, if i run the script only one files is copying.
on May 17th, 2007 at 9:32 am
Look at the first part of the command. In my example it is copy c:\test\*
Make sure that you use the asterisks(*). It is a wild card that tells the command to copy everything in the folder.
You could also use the * to copy all of a certain type of file. For example copy *.jpg would copy all jpg files.
on May 23rd, 2007 at 6:16 am
This is just what I was looking for except that I need to have the whole date recorded in the folder name, preferably ‘year_month_day_backup’. When I run your script without limiting the output from date, I recieve the following error:
\Wed 05/23/2007\* /Y /Q /S
Invalid number of parameters
Obviously a format not allowed!
Can you help me achieve what I need to do?
Thanks
on June 5th, 2007 at 4:01 am
Anybody out there able to answer this please?
on June 5th, 2007 at 7:49 am
%date:~0,3% That tells the script to out put 3 characters from the 0 space. So you could tell it to out put the year by using the characters. (%date:~10,4%) and then do the same for the day and year.
Try this at command line.
echo %date:~10,4%_%date:~4,2%_%date:~7,2%_backup
Hope that helps. Sorry it took so long to get back to you, I didn’t understand the question at first.
on June 6th, 2007 at 5:54 am
Perfect!
Thank you John!
on August 2nd, 2007 at 11:01 pm
For the folder name as date to work, you need to change the date format in regional settings to replace slashes with dashes.
I also rearranged the short date format to YYYY-MM-DD since that’s how I name all folders as dates. This way, alphabetical filename sorting will produce a neat list in chronological order top to bottom.
Here’s how. Click the following:
Start
Control Panel
Regional and Language Options
Regional Options tab
Customize button
Date tab
Short date format: select yyyy-MM-dd
Date Separator: select “-” (Dash) or “.” (Dot) if you prefer.
OK
OK
My backup batch file to back up My Documents, Firefox, and Email looks like this:
xcopy C:\docume~1\user\MYDOCU~1\* I:\Backup\”%date%”\My_Documents\* /Y /Q /S
xcopy C:\docume~1\user\Desktop\* I:\Backup\”%date%”\Desktop\* /Y /Q /S
xcopy C:\docume~1\user\APPLIC~1\Mozilla\* I:\Backup\”%date%”\Mozilla\* /Y /Q /S
Hope this helps.
-Jim Barbera
on August 3rd, 2007 at 5:07 am
Hi,
Am able to do something like this at all?
xcopy \\server01\Test\* etc…..
I really need this….
Thanks
Bin
on August 3rd, 2007 at 6:44 am
Bin,
I don’t see a problem with using a UNC address when copying. Have you tried it?
on August 3rd, 2007 at 10:08 am
I tried it but hasnt worked for me. Thought that it may not work for UNC paths. Any chance you would be able to confirm?
Many Thanks
Bin
on August 3rd, 2007 at 10:15 am
Yes. UNC copying works for me.
on August 3rd, 2007 at 10:20 am
Hi, Many thanks. I will check why mine is not working.
Thanks
Binesh
on August 3rd, 2007 at 12:51 pm
Thanks John Ward! Your explanation of the Date naming function now makes sense. I was having trouble figuring it out. Here’s what I came up with…
If you don’t want to change the date format on your machine, you can set up the date naming function like this:
Instead of
%date%
Use
%date:~10,4%_%date:~4,2%_%date:~7,2%
To back up your My Documents folder to the “I” drive, your line would look like this:
xcopy C:\docume~1\user\MYDOCU~1\* I:\backup\%date:~10,4%_%date:~4,2%_%date:~7,2%\My_Documents\* /Y /Q /S
This will result in a valid backup folder name as YYYY-MM-DD.
-Jim
on August 7th, 2007 at 7:35 pm
Thanks, good tip. UNC worked fine for me as well. I used this to copy from one server to another where the backup tape resides
on August 13th, 2007 at 7:27 pm
Hi,
For more advanced users, don’t forget that WinRAR has command line features (use “rar.exe /?” and “unrar.exe /?” in the WinRAR installation directory). You can then auto-archive your backups to save place. Also, I recommand GnuPG for encryption (which is also essentially CL). I don’t have much time to write a full script here (actually just thought of it after seeing this great tutorial), but by reading the doc and help files of each software, I’m sure you can make a good auto-backup-packager-encrypter without any difficulties
.
-thib´
on August 13th, 2007 at 8:33 pm
Great suggestion thib. Adding a win rar command would be a good idea. We also used pkzip on my last job to do some backup scripts.
on August 14th, 2007 at 3:16 pm
i’m trying to set up a batch file for each day of the week and then an overall weekly backup. so i have various folders set up. i’m having trouble using the date variable to find the files that have been modified that day and copy them to the appropriate folder. the beginning of my batch file looks like:
@echo off
:: variables
set drive=F:\backups1_monday
set backupcmd=xcopy /s /c /d:%date% /e /h /i /r /y
but it bombs because %date% wants to return Tue August 14, 2007 and d:Tue doesn’t mean anything.
any ideas? sorry for being a newbie – thanks for the examples to work from as they have been very helpful.
on August 14th, 2007 at 6:32 pm
What are you trying to copy from? Is the folder named tuesday? I didn’t really write this script to copy only files that changed on a certain day. The purpose is to do a full backup daily and then name the back up whatever that date is. Then you could do a backup at the end of the week and back all those up and name it.
on August 16th, 2007 at 6:17 am
Great help! thanks dude ^_^ , im currently backing up daily and my script only has
a xcopy command. AHU! AHU! AHU! (spartans)
on August 21st, 2007 at 10:49 pm
your command help me lot, but i am need time to display
on August 22nd, 2007 at 4:47 am
please help me——this command help;s me but i want me file name should be
i.e. 2007_08_20_12:20:20
i.e. YYYY_MM_DD_HH:MM:SS
please held
on August 22nd, 2007 at 6:48 pm
To Balwinder
This command can easily be replaced with the time variable. This is a system variable and you can call it using %time% just like I did with the date. Then you will need to adjust the format using the ~and select the characters as shown in the tutorial.
on August 22nd, 2007 at 6:57 pm
@balwinder again. Where you see %date% that will display the default date for your system. So mine will display: Wed 08/22/2007. So for example if you want to display the year, it would be something like %date:~10,4%. That is telling the script to display only from the 10th position (the position is the space in front of the character, so starting in front of the W will be position 0, count over to the space in front of the character you would like to use) and the 4 tells our script to display 4 characters after the selected position(10). I don’t have time to test this so I will give it a shot, you will have to adjust the code as needed. Your code would look something like this (if you have standard us date/time settings for the environmental variable):
xcopy c:\WhatYouWantToCopy C:\BackupFolderName\%date:~10,4%_
%date:~7,2%_%date:~4,2%_%time:~0,2%_%time:~3,2%_%time:~6,2%
* No spaces. I had to add one to fit it in the comment box on this website. I had to replace your colons (:) with underscores (_) because a colon is not a valid character in a Windows folder name.
on September 1st, 2007 at 6:06 am
hi
sorry, John Ward, your script contain error please check at your end
and get me a solution to —– folder name as
c:\2007_08_20_12:20:20\
i.e. YYYY_MM_DD_HH:MM:SS
on September 1st, 2007 at 8:52 pm
I already explained to you that YOU CANNOT USE A COLON (:) TO NAME A FOLDER. This not a valid character for a windows folder name. Replace it with some other character. I am not going to rewrite the script. I am sure you can figure it out.I didn’t write this tutorial so I could write everybody’s backup script for them. I have no problem answering the occasional question, but I am not going to do your work for you. If you follow this tutorial and read through the comments you should have no problem creating your own version of the script.
on September 5th, 2007 at 5:54 am
hi
sorry, John Ward, your script contain error please check at your end
this is not a matter of
COLON (:)
please check
on September 5th, 2007 at 11:51 am
Read the tutorial again, you can figure it out.
on September 10th, 2007 at 7:07 pm
Is there any way to exclude files or folders in the script? For instance I want to backup the My Documents folder but not the My Music folder inside My Documents.
Thx in advance
on September 10th, 2007 at 7:34 pm
Thats a good question. You may have to do each folder separately. I will look into that though and try to get back to you.
on September 21st, 2007 at 4:37 pm
This script is diabolical – It answers the question of excluding folders and more. It is pieced together from a variety of sources and modified for my need. The modifications are easy to fit anyone’s need.
———————————————————————————————————–
::set username if you are mounting to a network drive or some location that
::requires a username and password. In this case it is using the windows ::account password
set USER=xxx
::mount drive – I am mounting a network drive and map it to the U drive.
::The start /wait allows time for the drive to mount before moving on to the ::next process
start /wait NET USE U: \\(your_server)\(some_directory) /USER:%USER% ‘password’
@echo off
::create a file with a running list of directories, files and/or file types you ::want to exclude. In my case I want to exclude certain folders. See ::further down example of exclude file. I copy the exclude txt file to a tmp ::file just incase the txt file gets caught up in some other process. I save ::the exclude txt in my docs because I want to back it up as well, so I will ::always have a up-to-date copy with each backup.
copy “%USERPROFILE%\My Documents\exclude_folders.txt” exclude.tmp
:: variables
set drive=U:\.backup
set folder=_%date:~10,4%_%date:~7,2%_%date:~4,2%
set backupcmd=xcopy /s /c /d /e /h /i /r /y /v
::My Documents has its own variable because of the exclude. I don’t want ::certain directories to be backed up. here is where the exclude file is ::called
set mydocbackup=xcopy /s /c /d /e /h /i /r /y /v /exclude:exclude.tmp
:: THE BACKUP
echo ### Backing up My Documents…
%mydocbackup% “%USERPROFILE%\My Documents” “%drive%\My Documents%folder%”
echo ### Backing up IE Favorites and Firefox Bookmarks…
%backupcmd% “%USERPROFILE%\Favorites” “%drive%\Favorites%folder%”
%backupcmd% “%USERPROFILE%\Application Data\Mozilla\Firefox\Profiles” “%drive%\Mozilla%folder%”
echo ### Backing up email and contacts (MS Outlook)…
::%backupcmd% “%USERPROFILE%\Local Settings\Application Data\Microsoft\Outlook” “%drive%\Outlook%folder%”
:: use below syntax to backup other directories…
:: %backupcmd% “…source directory…” “%drive%\…destination dir…”
del exclude.tmp
::Disconnect the mapped drive
start /wait NET USE U: /DELETE
echo Backup Complete!
@pause
———————————————————————————————-
This is my “exclude_folders.txt” content in the exact format
My Documents\My Music
My Documents\My Pictures
on September 21st, 2007 at 4:40 pm
Script without all the comments
set USER=xxx
start /wait NET USE U: \\(your_server)\(some_directory) /USER:%USER% ‘password’
@echo off
copy “%USERPROFILE%\My Documents\exclude_folders.txt” exclude.tmp
:: variables
set drive=U:\.backup
set folder=_%date:~10,4%_%date:~7,2%_%date:~4,2%
set backupcmd=xcopy /s /c /d /e /h /i /r /y /v
set mydocbackup=xcopy /s /c /d /e /h /i /r /y /v /exclude:exclude.tmp
echo ### Backing up My Documents…
%mydocbackup% “%USERPROFILE%\My Documents” “%drive%\My Documents%folder%”
echo ### Backing up IE Favorites and Firefox Bookmarks…
%backupcmd% “%USERPROFILE%\Favorites” “%drive%\Favorites%folder%”
%backupcmd% “%USERPROFILE%\Application Data\Mozilla\Firefox\Profiles” “%drive%\Mozilla%folder%”
echo ### Backing up email and contacts (MS Outlook)…
::%backupcmd% “%USERPROFILE%\Local Settings\Application Data\Microsoft\Outlook” “%drive%\Outlook%folder%”
:: use below syntax to backup other directories…
:: %backupcmd% “…source directory…” “%drive%\…destination dir…”
del exclude.tmp
start /wait NET USE U: /DELETE
echo Backup Complete!
@pause
on September 28th, 2007 at 2:02 pm
Anyway to have this run more than once a day?
on September 28th, 2007 at 3:05 pm
The only way I can think of to have it run multiple times a day is to set up several scheduled tasks. For example you can have 1 run everyday at 1pm and 1 run every day at 1am. I don’t think windows task scheduler has an option to do hourly. Maybe a third party scheduler would do the trick?
on March 1st, 2008 at 2:05 am
Hi tanks for Help
For Create Zip File Name From Date System You must Install “Command Line Support Add-On” on winzip 8 or 9 and cerate a .bat file exclude:
———
Path \\computer1\e$\Backup_Commander\WinZip
wzzip -P -r -m -Jhrs -whs \\computer2\d$\Zip_Surce\%date:~10,4%_%date:~4,2%_%date:~7,2% \\computer1\e$\Backup\*.*
—–
this command cerate a zip file from current date and move data in this zip file
on May 15th, 2008 at 4:42 pm
Fantastic posts I am highly impressed and if you could guide me to a list of system variables and commands available in dos.
on July 29th, 2008 at 5:46 am
hi i have a question i already tried this backup but it said invalid number of parameters what should i do ?? Tnx in advance
on August 1st, 2008 at 11:53 pm
Am I missing something or is not using the veritas backup utilitiy built into win2k and xp easier to config and use?!?!?!
on August 21st, 2008 at 8:49 am
Thanks John!!!!!
It worked great….
on August 25th, 2008 at 5:40 am
please help to archive the given file in the window
on September 2nd, 2008 at 4:53 am
Hi
I am trying to copy a ……\backup\*.bak file from MSSQL to another folder on the mapped network drive but i am having an issue that it does not copy the .bak file to another folder even if the destination folder is on the local drive
(xcopy c:\……\backup\*.bak z:\Backup) that is the command line that i am using.
I am guessing with my little knowledge of script writing that this is incorrect as its not working?LOL
Please i need a little assistance in regards to get this working
P.S. i even stopped the service of SQL to make sure that the program is not using the .bak file while copying the file but that too does not copy the file over
your help will really be appreciated
Thanks
on September 18th, 2008 at 11:27 am
hi john,
I have an issue. I need to backup at three servers whoes date format are different from one another.
I there any other option so that If I run the backup batch file once, it should backup the source data to the target folder say sunday (folder name) and similarly if I run the same batch file the next time it has to automatically backup the source data to another folder called monday and so on… for the entire week (7 days). I use xcopy …. but no clue how to achieve it.
Pls help . thanks
on September 28th, 2008 at 3:41 pm
I’ve written something similar to this , but more advanced.
I add some simple lines of code to add backup folder like this
\yyyy\mm\dd\backup
and also generate a log file
Check my post out:
http://zangoole.com/1387/07/07/batch-backup-script/
Unfortunately It’s Farsi (Persian) but take a look at the code.
on September 28th, 2008 at 3:43 pm
[...] اینجای کار را از TeamTutorials یاد گرفتم. اما زنگوله این وسط چی کاره است؟ یک مترجم خشک [...]
on October 5th, 2008 at 1:01 pm
Look at the comment above, he’s an Iranian…
on October 13th, 2008 at 3:45 am
hey John i was wondering what exactly does this backup do if u can answer me please email me at x4.20mitchx@hotmail.com
on October 13th, 2008 at 3:47 am
what is backing up files good for and y do ppl do it??? plz hlp hehe
on November 10th, 2008 at 5:01 pm
Thanks for the help, really great guide you have going here.
on December 10th, 2008 at 1:52 am
Didn’t quite see the answer to the question regarding backups of “newest” files.
I want to create a copy of a music directory and then run your script nightly so that if there are new mp3 files added to the source directory it will only copy those files.
Any ideas?
Thanks in advance.
on January 21st, 2009 at 6:45 am
how to copy directory from one location to another location using xcopy in windows
on January 22nd, 2009 at 4:15 am
Hi John,
Just wanted to let you know that you are on stumble upon.
I like your script!
A word to everyone who needs help making their own scripts: Please stop asking John to write a script for you, do it yourselves, it’s not hard. You can easily use any search engine to research the various commands in this script to find out how it works. Do your homework first, and then you won’t have to ask silly questions that aren’t worth the time to answer.
Anyway, Keep up the good work John.
-Ben
on April 28th, 2009 at 2:26 am
or, simply use ntbackup or any of the thousand backup tools out there.
nice post though.
on May 22nd, 2009 at 3:15 pm
This is not working @ time 12:00 AM
xcopy D:\xyz\* D:\Backup\%date:~10,4%-%date:~7,2%-%date:~4,2%_%time:~0,2%.%time:~3,2%\* /Y /Q /S
on June 18th, 2009 at 7:59 pm
Hi Everyone,
I have found that, if you are trying to copy a folder that has space in name (Example: Documents and Settings), then this script will not work.
To make it work:
You will have put quotation mark on both ends of folder’s address.
Example:
xcopy “C:\Documents and Settings\Administrator\Desktop\*” “\\server\admin\Backup\Hp-Desktop\%date:~10,4%_%date:~4,2%_%date:~7,2%_backup\*” /Y /Q /S
This script working perfectly on my pc, and I hope it will work on your too.
on June 18th, 2009 at 8:01 pm
I have found that, if you are trying to copy a folder that has space in name (Example: Documents and Settings), then this script will not work.
To make it work:
You will have put quotation mark on both ends of folder’s address.
Example:
xcopy “C:\Documents and Settings\Administrator\Desktop\*” “\\server\admin\Backup\Hp-Desktop\%date:~10,4%_%date:~4,2%_%date:~7,2%_backup\*” /Y /Q /S
This script working perfectly on my pc, and I hope it will work on your too.
on July 3rd, 2009 at 10:14 am
You can copy folders with spaces in the names. You just have to use the folder’s MS-DOS name in your command lines.
Examples:
My Documents = MYDOCU~1
Program Files = PROGRA~1
Application Data = APPLIC~1
To find the MS-DOS name, you’ll need to open a command prompt window and navigate to the folder using the dir /X and cd commands. Maybe there’s a better way, but this is what I do.
-Jim
on July 20th, 2009 at 8:41 pm
Pawandeep, I had the same trouble. Tried differentcommands for hours until I came across your post. Thank you thank you!!!
on August 10th, 2009 at 6:51 am
Awesome tutorial, I always converted my bat to an exe then added it to start up as I didn’t realize there was a scheduled task feature.
on September 9th, 2009 at 4:57 am
I think this is valuable to anyone running windows, because of course it fails so much. Thank you.
on October 6th, 2009 at 12:09 pm
This has been really helpful. I have been using it ever since. Thanks.
on November 6th, 2009 at 1:03 am
very easy to follow tutorials, thanks for this, i will implement it right now to my computer
on November 6th, 2009 at 9:51 am
Hey John,
This tutorial was very helpful. I am working on getting a back up scheduled and running it as a batch file might actually work. One question I have those is that the folder that I am attempting to back up is buried deep into some folders. Am I able to write it so that it copies automatically just that file or do I have to do the long string of A folder to B folder to C folder etc?
on November 17th, 2009 at 10:14 pm
hi
john ward you are the best here
keep up the good works
thanks
on December 2nd, 2009 at 9:07 am
donno if anyone said this before but if you need to copy a file with space, you can also use “” , then you don’t have to use the dos name (ex: “c:\my folder with space”) thank you for the tutorial! was the solution to my windows backup problem.
on December 18th, 2009 at 3:07 am
Hi,
I was try fallowing script is not work. Please help me…
“xcopy c:\test\* x:\backup\%date:~0,3%\* /Y /Q /S”
on December 18th, 2009 at 3:16 am
Hi,
Sorry. It’s working.
on December 19th, 2009 at 7:00 pm
[...] [...]
on January 24th, 2010 at 11:40 pm
i still have to do some efforts for this..
on January 24th, 2010 at 11:40 pm
please help me.. it is still not working
on February 3rd, 2010 at 7:02 am
I agree Niklas.Thanks.