Batch Rename File With Date

  1. Batch Script Rename File With Date
  2. Dos Rename File With Date
  3. Batch Script Rename File With Date Timestamp
  4. Get Date Batch File
  5. Batch File Date Commands

Bulk Rename Utility allows you to easily rename files and entire folders based upon extremely flexible criteria. Add date/time stamps, replace numbers, insert text, convert case, add auto-numbers, process folders and a whole lot more! File Renamer makes it easy to rename large number of files in in a few clicks. Have a large picture library and wished files were called something more descriptive than DSCsomething.jpg? File Renamer gets the job done without wasting hours. File Renamer values your privacy. When you do a batch rename using the Date/Time Original you'll get a file name like this: 20100.jpg The space can easily be removed with the Search and Replace option of the batch rename. Just replace a single space with nothing. With both the Template option and Search and Replace options selected the job can be done in one pass. Batch Rename Photo With Date free download - Batch File Rename, Batch File Renamer, FastStone Photo Resizer, and many more programs.

2004-12-10 (updated: 2020-09-30) by Philip
Tags: backup, batch file, script

Sometimes it is useful, or even necessary to simply copy existing directories to another hard disk or network drive, rather than using more complicated backup methods. Multiple directories can be backed up comparatively easy with a simple click, by creating and running a batch file. That file can be executed manually from your desktop, can be added to startup or scheduled for periodic execution as needed.

Batch files have comparatively easy syntax and can have many uses, so this method could also be a good learning experience by example. You can simply copy the text below, and paste it into Notepad. Create a new file with either .bat or .cmd extension, rather than txt.

Here is a working example of a backup script you can modify for your needs:

@echo off
:: variables
set drive=G:Backup
set backupcmd=xcopy /s /c /d /e /h /i /r /y

echo ### Backing up My Documents...
%backupcmd% '%USERPROFILE%My Documents' '%drive%My Documents'

echo ### Backing up Favorites...
%backupcmd% '%USERPROFILE%Favorites' '%drive%Favorites'

Batch script rename file with date

echo ### Backing up email and address book (Outlook Express)...
%backupcmd% '%USERPROFILE%Application DataMicrosoftAddress Book' '%drive%Address Book'
%backupcmd% '%USERPROFILE%Local SettingsApplication DataIdentities' '%drive%Outlook Express'

Batch Script Rename File With Date

echo ### Backing up email and contacts (MS Outlook)...
%backupcmd% '%USERPROFILE%Local SettingsApplication DataMicrosoftOutlook' '%drive%Outlook'

echo ### Backing up the Registry...
if not exist '%drive%Registry' mkdir '%drive%Registry'
if exist '%drive%Registryregbackup.reg' del '%drive%Registryregbackup.reg'
regedit /e '%drive%Registryregbackup.reg'

:: use below syntax to backup other directories...
:: %backupcmd% '...source directory...' '%drive%...destination dir...'

echo Backup Complete!

The above example backs up 'My Documents', Favorites, Outlook Express email/address book, (all for the current user) and the Windows Registry. It copies the files to the directory defined in the %drive% variable, or 'g:Backup'. If the script is ran multiple times, it will only rewrite if the source files are newer. It will create subdirectories as necessary, and it will retain file attributes. It can copy system and hidden files.

In the above file, all lines that begin with '::' are comments. The 'set drive=' and 'set backupcmd=' near the top define two variables (referenced by %drive% and %backupcmd%), used a number of times throughout the file; the first being the location of the top directory where we want to backup, and the second the actual copy command with all necessary switches. All the 'echo ' lines in the file simpy output the line of text to the screen, and the lines beginning with %backupcmd% are the actual commands to execute.

Batch Rename File With Date

Note that most of the folders in the above backup example are subdirectories of the %USERPROFILE%... It is possible to simply backup the entire user profile with My Documents, Favorites, Outlook Express, Outlook, etc. by backing up this one folder. Here is an example (it assumes the above 'drive' and 'backupcmd' variables are set):

%backupcmd% '%USERPROFILE%' '%drive%%UserName% - profile'

Batch Rename File With Date

Backing up Other Directories and networked PCs

You can backup other directories by simply creating more alike lines:

%backupcmd% '...source dir...' '%drive%...destination dir...'

For example, if you'd like to backup 'C:Program FilesMicrosoft Office' to our destination 'G:BackupMS Office' (and retain the directory structure) you'd need to add the following line to the batch file:

%backupcmd% 'C:Program FilesMicrosoft Office' '%drive%MS Office'

Here is another example, backing up the Administrator Profile on a machine on the LAN with computer name 'Lianli':

%backupcmd% 'LianlicDocuments and SettingsAdministrator' '%drive%Lianli - admin profile'

Remember, you have to save the batch file with either .bat or .cmd extension, then just double-click to execute it.

Using the Current Date

Sometimes it is useful to create folders with the date incorporated in the folder name. Here is how to set the variable folder to the current date (assuming US system date format):

Dos Rename File With Date

set folder=%date:~10,4%_%date:~4,2%_%date:~7,2%
%backupcmd% '...source dir...' '%drive%%folder%...destination dir...'

It is also possible to use the current time in the folder name. The following example with incorporate both the current date and time to the minute, separated by underscores. There is an extra step that cleans up possible spaces in single-digit hours in the system time:

set hour=%time:~0,2%
if '%hour:~0,1%' ' set hour=0%time:~1,1%
set folder=%date:~10,4%_%date:~4,2%_%date:~7,2%_%hour%_%time:~3,2%
%backupcmd% '...source dir...' '%drive%%folder%...destination dir...'

Example - dated directories

In the example below, we first set 3 variables: drive, folder, and backupcmd. The 'drive' variable defines the root directory of our backups. The 'folder' takes the 2 digit day value from the current date (US date format, taking 2 digits from the date command output, starting at the 7th character), which we will use as a subdirectory. The third variable, 'backupcmd' defines our backup command with the appropriate command line switches we want to use.

@echo off
:: variables
set drive=D:Backup
set folder=%date:~7,2%
set backupcmd=xcopy /s /c /d /e /h /i /r /k /y
echo ### Backing up directory...
%backupcmd% 'C:Program Filessomedirectory' '%drive%%folder%'
echo Backup Complete!

This example will backup the 'C:Program Filessomedirectory' folder to 'D:Backup[dd]' where [dd] is the current day of the month. After a month, we will have 30ish daily copies of the backup... And, because of the xcopy command line switches chosen, following backups will only overwrite files that are newer, speeding up subsequent backups. Alternatively you can add a line to delete the %folder% directory prior to executing the %backupcmd% if you prefer to start clean (and take longer).

Cleaning up

Batch Rename File With Date

It is usually a good idea to clean up temporary files, cookies, and history from the destination backup, as applicable. It is especially useful if you're backing up full, multiple user profiles and overwriting them periodically. Since temporary files and cookies change, your backed up directories will keep increasing with unnecessary files. To remedy this, the following code can be added to the backup script, or to a separate batch file. It will automatically search all subdirectories for 'cookies', 'temp' and 'history', and then remove those directories:

:: change to the destination drive first
:: your parent backup directory
echo ### Searching for files to clean up...
cd %drive%
dir /s/b/ad cookies > %drive%cleanup.txt
dir /s/b/ad temp > %drive%cleanup.txt
dir /s/b/ad history > %drive%cleanup.txt

echo ### Deleting cookies, temp files and history from backup dir
for /f 'delims=' %%J in (%drive%cleanup.txt) do rd '%%J' /Q/S

echo Cleanup complete

Batch file date commands

Note that you need to change to the destination drive, and the main backup directory before searching for files to delete. Any sub-folders that contain 'cookies', 'temp', or 'history' will be deleted automatically. You can test to see what will be deleted by commenting out the 'for /f .....' line (just add :: to the beginning of the line, or delete it from the batch file and add it again later). If that line is not present, the file will only list all files to be deleted in the cleaup.txt file, located in the destination directory (G:Backupcleanup.txt in the above example). If you add the cleanup portion to the end of your backup batch file, you may want to remove the '@pause' line at the end of the backup portion, so everything can execute without user interacion.

Alternatively, there is a simpler one-line method of deleting specific subdirectories after backing up. The disadvantage of using this method is that you'd need another line for each separate directory to be removed... In other words, it doesn't work well when removing a large number of directories by similar names. Still, here is an example:

rmdir /s /q '%drive%%UserName%Local SettingsTemporary Internet Files'

See Also: How to backup using Batch Files in Windows 10 (using Robocopy)


    Any batch file can be interrupted with CTRL+C or CTRL+Break if needed. They can also be paused with the Pause/Break key.Instead of backing up My Documents, Favorites, Outlook and Outlook Express files separately, you can combine it all into one line for the current user: %backupcmd% '%USERPROFILE%' '%drive%%UserName% - profile' . The only disadvantage being that it would save temporary IE files as well (however the default location of those can be changed, or you can automate cleanup after backing up, as described above).The Registry backup in the above examples works well only for partial registry restores, it does not save the complete system state. Read this FAQ for more info.
  • Also check: Microsoft SyncToy 2.1 for folder synchronization.

rate: avg:
For the UK people who may stumble upon this...check out the date and time line which will capture the date and time... if today was 23/06/2006 and 19:30, the variable dateNtime would be 23-06-2006_19-30
Hope this saves someone some time!! The code is below
@echo off
:: variables
set drive=M:mybackup
SET dateNtime='%date:~0,2%-%date:~3,2%-%date:~6,6%_%time:~0,2%-%time:~3,2%'
set backupcmd=xcopy /s /c /d /e /h /i /r /k /y
echo ### Backing up your Sites directory...
%backupcmd% 'C:Program FilesSites' '%drive%%dateNtime%'
echo Backup Complete - Your Backup is located at %drive%
Can some tell how I could backup multiple PC (say 40) using the backup script without writing 400+ lines of code. Is there another script I can attach this to and run a loop program. Sorry I am not a program writer
You only need one additonal line for each LAN client/directory, not 10. For example, to backup the administrator profile on 3 remote machines named COMP1, COMP2, and COMP2, you'd need something like:
@echo off
:: variables
set drive=g:Backup
set backupcmd=xcopy /s /c /d /e /h /i /r /k /y
%backupcmd% 'COMP1cDocuments and SettingsAdministrator' '%drive%COMP1 - admin profile'
%backupcmd% 'COMP2cDocuments and SettingsAdministrator' '%drive%COMP2 - admin profile'
%backupcmd% 'COMP3cDocuments and SettingsAdministrator' '%drive%COMP3 - admin profile'
echo Backup Complete!
And so on, for each client you want to backup. In other words, you do need to add one line of code for each directory you're backing up (it includes all subdirectories as well), I am not sure how you can further simplify that.
I am trying this cool script but I get an error message 'Invalid Number of Parameters' when running the script. Only on the lines that include %USERPROFILE% in them. For example
%backupcmd% '%USERPROFILE%My Documents' '%drive%My Documents'
Any suggestions???
Thanks in advance
Big sigh of relief. This is so easy. I don't have to figure out Retrospect, which imho is just appalling. Thank you thank you.
If i want to rename a folder when it's backed up deppending on the date, for example i have a folder with files that are populating every day in C:pictures and i want to make a backup of it every to D:Backup but change the folder name to 'pictures_dd_mm_yyyy' what do i have to do? Rename the folder before backing up or during the backup procedure?
The main problem of this procedure is how to avoid overwriting every time the new folder to the backup destination to the old folder.
How the system will be able to identify the current dd_mm_yyyy?
Can you send me the code for this batch?
Thanx for your time
Is there a command I can use to search the c: drive till it finds a file, then back it up?.........
I've got the backup working but want to use it as a daily backup of one drive to another. What do I need to do to replace the previous day's backup?
The xcopy '/d' command-line parameter specifies that the command is to copy all source files that are newer than existing destination files. Using this option allows you to update the backup directory only with files that have changed since last time you ran it (rather than copying all files). You should still end up with identical directories/files after execuring the batch file, while saving time for consecutive backups.
I hope this helps. Please direct any further questions to our forums. Thanks.
I was in bad need of creating a batch file for backup and the script you have given in the website has really satisfied my requirement. Thanks for the help. Wishing you ALL THE BEST!!!
Ravi Verma Vegesna
how do I make a batch file to run weekly that copies the source directory (which is the 'c' drive in my case) ato the backup directory (which is the 'd' drive for me) in windows?

Batch Script Rename File With Date Timestamp

Hi guys, i'm having trouble figuring out how to create one script that copies different files at specified time intervals?
I have an application that outputs files everyhour with a different file name and i need to be able to copy each at a specified time interval.
I am using this batch with a few changes. It is working great. I am having to manually go in and delete older backups though. How do I get it to only keep 7 days of backups? always overwriting the oldest date? thanks
Your code works great! Buttt.... When I put the batch file in the Scheduled Tasks and try to get it to run it Bombs. I look in the logfile and it says it ran successfully. I been trying to search the problem and the only thing I come up with is someone posted about having problems with drive letters in the batch file not working and having to put in serverfolder. I tryed but to no avail. Help!! Thanks in advance.

Get Date Batch File

i want to back up a network drive drive letter(x) with a folder name ( ma) X:ma
to D:ma
code @echo off
:: variables
set drive=x:Backup
set backupcmd=xcopy /s /c /d /e /h /i /r /y
%backupcmd% 'x:ma' '%drive% d:ma'
echo Backup Complete!
????? where have i gone wrong Capt Kaos Australia
The batch file works great and has saved me a lot of time, but what about a code that will automatically cancel the script should a network drive not be detected? It would be great for those remote customers not to get the error when a mapped drive is not there.
Any help on this would be great.
captainhaos, just use this:
@echo off
:: variables
set backupcmd=xcopy /s /c /d /e /h /i /r /y
%backupcmd% 'x:ma d:ma'
echo Backup Complete!

Batch File Date Commands

Hi Philip,
This is great, just what i searched for, thank you.
Is it possible (WinNT) to create a directory, in which its name is made of the PC host name and date, and if not, how can i prompt the user to insert it manually?
Thanks in advance,
Very good and simple batch file, I edited the original to suit and added the UK date part but that didnt give folders by date. Other than that, All works great. Thanx loads.
How can I add a couple of lines to make my backup automatically save to one of the following folders depending on the day of the week?
Today is i.e. Friday
Save to:
Im sorry that i dont understand most of them cause im new in batch files but i also want to backup & zip some files, lets say:
'C:program Fileswinzipwzzip' -a -p -r 'C:Program Filesahead*.*'
i run this batch every day and i want to verify that the backup is done, otherwise prompt me with an error message
I think that the best way is to compare the date that the backup was run with the date that the file is modyfied. that was a thought but i dont know if is the best way
thnx in adnvance
Here is another batch file. I have it running on my machine so that when it is run it backs up all of my documents in the C: drive to my E: drive. See what you think. Nice and simple. However I do want to add a command to tell it to copy ONLY what has been changed since the last backup. I know this can be done using /D:m-d-y (leaving them blank checks target directory and only copies if the date of the last edit of the source has been changed) However I am not sure how to add it in. Any help would be greatly appreciated.
@echo off
color F9
xcopy /R /E /H /Y 'c:Documents and SettingsUser1My Documents*.*' 'E:my documents*.*'
Just add the '/D' parameter to your xcopy command... Something like:
xcopy /R /E /H /Y /D '...source...' '...destination...'
A great article!!!! I modded it a little bit for what I needed it to do and when I double click on it it runs great. I added it as a scheduled task, it runs, I see the window flash but there is not any output. Any help would be appreciated . Thanks
Hi, I have problems with getting the dates on my folder. I live in Sweden so I tried the other settings. The backup works but I would like to get dates on my folders when I backuo them.
@echo off
:: variables
set drive=G:ProjektArbetsdokumenttest
SET dateNtime='%date:~0,2%-%date:~3,2%-%date:~6,6%_%time:~0,2%-%time:~3,2%'
set backupcmd=xcopy /s /c /d /e /h /i /r /k /y
echo ### Backing up your Sites directory...
%backupcmd% 'C:Documents and SettingsJohnMy Documentsbackup' '%drive%%dateNtime%'
echo Backup Complete - Your Backup is located at %drive%
Related Articles: