Solved

how to write a bat file to copy a folder [with sub-directory inside] to another hard disk and name the folder with system date, then delete all the original files

Posted on 2011-03-23
31
492 Views
Last Modified: 2012-05-11
I would like to move my videos and pictures from my laptop to an external hard disk, I think to create a .bat file to carry out the task will be ideal.
1. I want to create a folder on the external hard disk with the name of system date e.g. if today is  23 March 2011, the folder will be named 23-03-2011 or similar format
2. then move all my files  created under d:\my documents\my picture and d:\my documents\my videos\  [with sub-directories under both] to this new folder name 23-03-2011.
which is the best waqy to do it, to move all files in all these folders to the new one, or to copy all files to the new folder, then, delete the files in the original folders, I am afraid some system files might not be able to be moved as well. please advise
can some one show me the best way how to create this batch file ?
0
Comment
Question by:Timhkg
  • 8
  • 7
  • 7
  • +3
31 Comments
 
LVL 7

Expert Comment

by:Pr1z
Comment Utility
You could try something like this:
REM ***** Populate variables
set source=<path to source files>
set destination=<path to destination files>
set year=%date:~6,4%
set month=%date:~3,2%
set day=%date:~0,2%
set fulldate=%year%-%month%-%day%

REM ***** Make the backup folder
mkdir %destination%\%fulldate%

REM ***** Move the files to the folder
move /Y %source% %destination%\%fulldate%

Open in new window


I am in the UK so date format is dd/mm/yyyy ==> you may need to change that.  I have not tested it so you might want to try it with a subset of your data first.

Hope this helps

Priz
0
 
LVL 4

Expert Comment

by:FireW0lf
Comment Utility
Not tested, but this should do you

Get a copy of robocopy since it can interrogate file creation dates easily (free - grab it from Microsoft)

Rem Get the date and store it as a variable
for /f "delims=" %a in ('date /t') do @set MyDate=%a

Rem Replace the "/" in the date with "-" so we can use it in a filename
set MyDate2=%MyDate:/=-%

Rem Make the directory (change the "E:\" to the path of your backup folder)
md E:\%MyDate2%

Rem Win7 format - delete as applicable
robocopy C:\Users\YourUserName\Pictures E:\%MyDate2% /COPYALL /MOVE /MAXAGE:1
robocopy C:\Users\YourUserName\Videos E:\%MyDate2% /COPYALL /MOVE /MAXAGE:1

Rem XP format - delete as applicable
robocopy C:\Docume~1\YourUserName\mydocu~1\Pictures E:\%MyDate2% /COPYALL /MOVE /MAXAGE:1

Rem remove variables
set MyDate=
set MyDate2=

Open in new window

0
 
LVL 4

Expert Comment

by:FireW0lf
Comment Utility
Oh, in case you were wondering, the robocopy flags I used:

/COPYALL = copy all the file attributes
/MOVE = move, not copy
/MAXAGE:1 = only act on files less than 1 day old
0
 
LVL 5

Expert Comment

by:Angelgeo
Comment Utility

@echo off
For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @( 
	Set mm=%%A
	Set dd=%%B
	Set yyyy=%%C
)

set date=%dd%-%mm%-%yyyy%
mkdir E:\%date%
mkdir E:\%date%\"my picture"
mkdir E:\%date%\"my videos"

xcopy /y /s /H /E /R "d:\my documents\my picture" E:\%date%\"my picture"
xcopy /y /s /H /E /R "d:\my documents\my videos" E:\%date%\"my videos"

RMDIR /S /Q "d:\my documents\my picture"
RMDIR /S /Q "d:\my documents\my videos"

Open in new window

0
 
LVL 7

Expert Comment

by:Pr1z
Comment Utility
Robocopy is a fantastic tool.  It is part of the Windows 2003 Resource Kit.  You can download it here ==> http://www.microsoft.com/downloads/en/details.aspx?familyid=9d467a69-57ff-4ae7-96ee-b18c4790cffd&displaylang=en

It is fantastic for file operations that are just a step beyond simple right up to quite complex.

Might be over kill in this case though, but a tool well worth investing some time in learning.

Priz
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
Is "d:\my documents\my picture" the actual true path to the first folder to be copied, or is it where one would normally find it, like "%userprofile%\pictures" ?

Also, if you do the following at a command prompt, what does it display:

ECHO %DATE%

~bp
0
 
LVL 7

Expert Comment

by:Pr1z
Comment Utility
ECHO %date% will return the short date format specified in your regional settings.  Fo the UK this is dd/mm/yyyy (24/03/2011) for the US it would be mm/dd/yyyy (03/24/2011).

%userprofile% is typically "c:\documents and settings\%username%" so you would probably need to append "\my documents\My Pictures" to get at the "My Pictures" folder.  Of course, you could have moved the folders or install Windows on the D:\ drive or have done any other kind of weirdness to make any or all of these comments incorrect :-)

Hope that helps

Priz
0
 

Author Comment

by:Timhkg
Comment Utility
how can I change it if my local date date format is dd/MM/yy
0
 

Author Comment

by:Timhkg
Comment Utility
Angelgeo:
when I use this :

@echo off  
For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @(  
        Set mm=%%A  
        Set dd=%%B  
        Set yyyy=%%C  
)  
set date=%dd%-%mm%-%yyyy%

the new folder name is [2011-3-Sun] in E:\
when I replace all yyyy  with yy, the new folder name is [11-03-] in E:\
how can I correct this, I am in HOng KOng and short date format is dd-MM-yy
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
Try this:

@echo off  
For /F "tokens=1-3 delims=/ " %%A in ('Date /t') do (  
        Set mm=%%B  
        Set dd=%%A  
        Set yyyy=%%C  
)  
set date=%dd%-%mm%-%yyyy%

Open in new window

~bp
0
 

Author Comment

by:Timhkg
Comment Utility
But :
I am in HOng KOng and date format is dd-MM-yy  NOT  dd-mm-yyyy
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
Don't think you ever answered my earlier question about what the following at a command prompt displayed:

ECHO %DATE%

~bp
0
 

Author Comment

by:Timhkg
Comment Utility
28/03/11
0
 

Author Comment

by:Timhkg
Comment Utility
also, I would like to move all files under d:\my documents\ my pictures\*.* and the files in its subfolders to E:\%date%\my pictures and the same subfolders.
Same as moving all files inder d:\my documents\my videos\*.* and the files in its subfolders to e:\%date%\my videos and the same subfolders.

is there an easy way to do so other than:
1. first xcopy all files including the subfolders files
2. remove directory d:\my documents\my pictures and d:\my documents\my videos to get rid of all files
3. re-maike the directory d:\my documents\my pictures and the subfolders under it - ditto \my videos
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
Are you okay with using ROBOCOPY, that would allow you to move the files, but not delete the folders and subfolders from D:.

~bp
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:Timhkg
Comment Utility
yes, but how to do so
0
 
LVL 4

Expert Comment

by:FireW0lf
Comment Utility
Timhkg: I posted right up near the top on how to use robocopy to do this - it will create a directory of the correct name, and move your daily files to it
0
 
LVL 7

Expert Comment

by:Pr1z
Comment Utility
But :
I am in HOng KOng and date format is dd-MM-yy  NOT  dd-mm-yyyy

To change it system wide, this is done in Start ==> Control Panel ==> Regional Settings ==> then change the value in "Short Date"

Alternatively you could use (with %yyyy% set as the full 4 digit year) %yyyy:~2,2% to use whatever 2 characters 3 in from the left (the left most character is character number 0).

Try this:
set yyyy=2011
echo %yyyy%
echo %yyyy:~2,2%
echo %yyyy%

Open in new window


You should get:
     2011
     11
     2011

Hope this helps

Priz
0
 
LVL 4

Expert Comment

by:FireW0lf
Comment Utility
So simply hybrid mine with angelogeo's to adjust the date format

For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @( 
	Set mymm=%%A
	Set mydd=%%B
	Set myyyyy=%%C
)

set myDate=%mydd%-%mymm%-%myyyyy%

Rem Make the directory (change the "E:\" to the path of your backup folder)
md E:\%myDate%

Rem Win7
robocopy %userprofile%\Pictures E:\%myDate% /COPYALL /MOVE /MAXAGE:1
robocopy %userprofile%\Videos E:\%myDate% /COPYALL /MOVE /MAXAGE:1

Rem XP

robocopy %userprofile%\mydocu~1\mypict~1 E:\%myDate% /COPYALL /MOVE /MAXAGE:1

Rem remove variables
set myDate=
set myyyyy=
set mymm=
set mydd=

Open in new window

0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
I think this should do what you wanted.  It will place the files under a folder names E:\YYYY-MM-DD.  I'd recommend using that format (year, month, then day) so that you can sort then easily in chronological order.  It works better than things like MM-DD-YYYY.

See if this makes sense, let me know of any questions.

@echo off

REM Build todays date for destination folder name
set YYYYMMDD=20%DATE:~6,2%-%DATE:~3,2%-%DATE:~0,2%

REM Make sure destination folders exist
if not exist "E:\%YYYYMMDD%\"             md "E:\%YYYYMMDD%\"
if not exist "E:\%YYYYMMDD%\my pictures\" md "E:\%YYYYMMDD%\my pictures\"
if not exist "E:\%YYYYMMDD%\my videos\"   md "E:\%YYYYMMDD%\my videos\"

REM Move all files to dated destination folders
robocopy "d:\my documents\my pictures\*.*" "E:\%YYYYMMDD%\my pictures" /S /MOV
robocopy "d:\my documents\my videos\*.*"   "E:\%YYYYMMDD%\my videos"   /S /MOV

Open in new window

~bp
0
 

Author Comment

by:Timhkg
Comment Utility
Billprew:

I have installed the package under:
C:\Program Files\Windows Resource Kits\Tools

However, the following 2 lines cannot be executed:
 
robocopy "d:\my documents\my pictures\*.*" "E:\%YYYYMMDD%\my pictures" /S /MOV  
robocopy "d:\my documents\my videos\*.*"   "E:\%YYYYMMDD%\my videos"   /S /MOV

Can u help to fix the problem.


0
 
LVL 7

Expert Comment

by:Pr1z
Comment Utility
Try addid the following lines at the top of your bat file:
c:
cd\Program Files\Windows Resource Kits\Tools

Open in new window


Could you post the error message that you receive??

Priz
0
 
LVL 7

Expert Comment

by:Pr1z
Comment Utility
ooops .... missed out the " characters but I am sure you can figure that out.

Priz
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
Is robocopy.exe in the folder "c:\Program Files\Windows Resource Kits\Tools"?

If so, then you could do this:

@echo off

REM Define path to ROBOCOPY
set Robocopy=c:\Program Files\Windows Resource Kits\Tools\robocopy.exe


REM Build todays date for destination folder name
set YYYYMMDD=20%DATE:~6,2%-%DATE:~3,2%-%DATE:~0,2%

REM Make sure destination folders exist
if not exist "E:\%YYYYMMDD%\"             md "E:\%YYYYMMDD%\"
if not exist "E:\%YYYYMMDD%\my pictures\" md "E:\%YYYYMMDD%\my pictures\"
if not exist "E:\%YYYYMMDD%\my videos\"   md "E:\%YYYYMMDD%\my videos\"

REM Move all files to dated destination folders
"%Robocopy%" "d:\my documents\my pictures\*.*" "E:\%YYYYMMDD%\my pictures" /S /MOV
"%Robocopy%" "d:\my documents\my videos\*.*"   "E:\%YYYYMMDD%\my videos"   /S /MOV

Open in new window


Your other option would be to add that folder to the PATH variable in the system environment settings.

~bp
0
 
LVL 4

Expert Comment

by:FireW0lf
Comment Utility
Or just copy robocopy into C:\Windows|System32 - I do it on all my servers since I use it so often - Windows looks in here for any commands if you've not specified the full path
0
 

Author Comment

by:Timhkg
Comment Utility
Billprew:
it still does not work completely following 100% your file.

1. it only make folder on E:\, but does not execute the robocopy command.
2. I noticed that after making the folder: e:\%YYYYMMDD%, there is an extra space after  E:\2011-04-06[extra space],m could it be the reason why the following line of 'robocopy' did not run correctly
0
 
LVL 7

Expert Comment

by:Pr1z
Comment Utility
What error message do you get?? If you type the command by hand does it work??

Priz
0
 
LVL 4

Expert Comment

by:FireW0lf
Comment Utility
Usually, the cmd line sysntax for robocopy is not as billprew showed

It should be:

robocopy (source folder) (destination folder) (files - eg *.*)
eg:
robocopy "d:\my documents\my pictures" "E:\%YYYYMMDD%\my pictures" /S /MOV

Open in new window

(The syntax I put in mine)
The *.* is not required, and should only be used as a filter eg *.jpg etc

Hope this helps
0
 
LVL 51

Accepted Solution

by:
Bill Prew earned 500 total points
Comment Utility
Okay, give this a try.

@echo off

REM Define path to ROBOCOPY
set Robocopy=c:\Program Files\Windows Resource Kits\Tools\robocopy.exe

REM Build todays date for destination folder name
set "YYYYMMDD=20%DATE:~6,2%-%DATE:~3,2%-%DATE:~0,2%"

REM Make sure destination folders exist
if not exist "E:\%YYYYMMDD%\"             md "E:\%YYYYMMDD%\"
if not exist "E:\%YYYYMMDD%\my pictures\" md "E:\%YYYYMMDD%\my pictures\"
if not exist "E:\%YYYYMMDD%\my videos\"   md "E:\%YYYYMMDD%\my videos\"

REM Move all files to dated destination folders
"%Robocopy%" "d:\my documents\my pictures\" "E:\%YYYYMMDD%\my pictures\" /S /MOV
"%Robocopy%" "d:\my documents\my videos\"   "E:\%YYYYMMDD%\my videos\"   /S /MOV

Open in new window

~bp
0
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

This collection of functions covers all the normal rounding methods of just about any numeric value.
Citrix XenApp, Internet Explorer 11 set to Enterprise Mode and using central hosted sites.xml file.
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now