?
Solved

Need to Copy a folder to a new location and append copied folder with date and time

Posted on 2011-05-05
10
Medium Priority
?
365 Views
Last Modified: 2012-05-11
Hi, I need some help please

I have a requirement to copy a folder "folderA" to a new location and append the copied folder with the date and time ie "folderA_date_time" from within a batch file.

Any help greatly appreciated.

Many thanks
0
Comment
Question by:Orchard
  • 7
  • 3
10 Comments
 
LVL 33

Expert Comment

by:knightEknight
ID: 35699231
It depends on the default date/time format of your system.  Please run the following command at a command prompt and post the results here:

  echo %date% %time%
0
 

Author Comment

by:Orchard
ID: 35699286
Hi Knight

the result is

05/05/2011 16:33:32:01

0
 
LVL 33

Accepted Solution

by:
knightEknight earned 500 total points
ID: 35699310
@echo off
setlocal
set NEWFOLDERNAME = "folderA_%date:~6,4%%date:~0,2%%date:~3,2%_%time:~0,2%%time:~3,2%%time:~6,2%"
md  "C:\newLocation\%NEWFOLDERNAME%"
xcopy/e  "C:\OLDlocation\folderA"  "C:\newLocation\%NEWFOLDERNAME%"
0
Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

 
LVL 33

Expert Comment

by:knightEknight
ID: 35699326
unfortunately there is a bit of ambiguity in todays date - 05/05/2011.  Is that MM/DD/YYYY or is it DD/MM/YYYY ... and which way do you want it in the folder name?
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 35699363
(personally I recommend YYYYMMDD for sorting purposes)
0
 

Author Comment

by:Orchard
ID: 35699389
Yes I agree YYYYMMDD would be great
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 35699405
well, if your system date format is MM/DD/YYYY then use this:

  set NEWFOLDERNAME = "folderA_%date:~6,4%%date:~0,2%%date:~3,2%_%time:~0,2%%time:~3,2%%time:~6,2%"

but if your system date format is DD/MM/YYYY then use this:

  set NEWFOLDERNAME = "folderA_%date:~6,4%%date:~3,2%%date:~0,2%_%time:~0,2%%time:~3,2%%time:~6,2%"
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 35699457
(I mean use that line in the script posted above)
0
 

Author Comment

by:Orchard
ID: 35699573
Hi Knight

I had to remove the spaces before and after the = in the set NEWFOLDERNAME line, but other than that it works a dream

Many thanks
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 35699579
Good catch on the spaces!  That one gets me every time.  Glad it works!
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses
Course of the Month15 days, 2 hours left to enroll

840 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