Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Batch script needed to xcopy and create unique folder name

Posted on 2011-05-09
7
Medium Priority
?
1,004 Views
Last Modified: 2012-05-11
I need a windows batch script that will do a xcopy of a directory and create a unique folder name based on timestamp to copy the copied folder to.

The xcopy part is easy, but how do I create a unique folder name in this script?  

Thanks in advance
0
Comment
Question by:sknoll84
7 Comments
 
LVL 97

Expert Comment

by:Lee W, MVP
ID: 35725672
Easily.


If you are using US standard date formats, then:

set TimeStamp=%time::=%
Set DateStamp=%date:~-4%%date:~4,2%%date:~7,2%
Target=x:\Path\to\place\folder\%DateStamp%%TimeStamp%

Now you have the option of using a datestamp, a timestamp or both.

xcopy source "%Target%"

0
 
LVL 5

Expert Comment

by:vguzman
ID: 35725679
If you want folder as date and subfolder as 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%
set subfolder=%hour%_%time:~3,2%_%time:~6,2%
md c:\%folder%\%subfolder%
xcopy /e C:\TEMP\*.* c:\%folder%\%subfolder%

if you want folder as date AND 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%_%time:~6,2%
md c:\%folder%
xcopy /e C:\TEMP\*.* c:\%folder%\
0
 
LVL 10

Expert Comment

by:ReneGe
ID: 35727756
This will create the date variable in a consistant manner no matter the Regional settings.

 
@ECHO OFF

CALL :GetDateTime

REM PUT SOURCE AND DESTINATION PATH AS REQUIRED
	SET Source=C:\Temp
	SET Destination=\\server\share\%TheDate%
	IF NOT EXIST "%Destination%" MD "%Destination%"


REM HERE PUT THE COMMAND SWITCHES NEEDED FOR THE TASK
	COPY /y "%Source%" "%Destination%"

PAUSE
EXIT


:GetDateTime
REM READ DATE
FOR /F "skip=1 tokens=1-6" %%A IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') DO (
	IF %%A GTR 0 (
	SET DayVal=%%A
	SET HourVal=%%B
	SET MinVal=%%C
	SET MonthVal=%%D
	SET SecVal=%%E
	SET YearVal=%%F
	)
)

REM ADDING LEADING "0"
	IF %DayVal% LSS 10   (SET Day=0%DayVal%) 		ELSE (SET Day=%DayVal%)
	IF %HourVal% LSS 10  (SET Hour=0%HourVal%) 		ELSE (SET Hour=%HourVal%)
	IF %MinVal% LSS 10   (SET Min=0%MinVal%) 		ELSE (SET Min=%MinVal%)
	IF %MonthVal% LSS 10 (SET Month=0%MonthVal%) 	ELSE (SET Month=%MonthVal%)
	IF %SecVal% LSS 10   (SET Sec=0%SecVal%) 		ELSE (SET Sec=%SecVal%)
	SET Year=%YearVal%

	SET TheDate=%Year%-%Month%-%Day%
	SET TheTime=%Hour%.%Min%.%Sec%


EXIT /b

Open in new window

0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 

Author Comment

by:sknoll84
ID: 35728276
@ReneGe, i like your script!  can i implement xcopy with this script?  i need to copy subdirectories, acl's, etc.

this is the xcopy script i was using:
xcopy /h /e /c /k "D:\Product\Server" "D:\Product\Backups\Server"
0
 
LVL 10

Accepted Solution

by:
ReneGe earned 2000 total points
ID: 35728397

@ECHO OFF

CALL :GetDateTime

REM PUT SOURCE AND DESTINATION PATH AS REQUIRED
	SET Source=D:\Product\Server
	SET Destination=D:\Product\Backups\Server\%TheDate%
	IF NOT EXIST "%Destination%" MD "%Destination%"


REM HERE PUT THE COMMAND SWITCHES NEEDED FOR THE TASK
	XCOPY /h /e /c /k "%Source%" "%Destination%"

PAUSE
EXIT


:GetDateTime
REM READ DATE
FOR /F "skip=1 tokens=1-6" %%A IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') DO (
	IF %%A GTR 0 (
	SET DayVal=%%A
	SET HourVal=%%B
	SET MinVal=%%C
	SET MonthVal=%%D
	SET SecVal=%%E
	SET YearVal=%%F
	)
)

REM ADDING LEADING "0"
	IF %DayVal% LSS 10   (SET Day=0%DayVal%) 		ELSE (SET Day=%DayVal%)
	IF %HourVal% LSS 10  (SET Hour=0%HourVal%) 		ELSE (SET Hour=%HourVal%)
	IF %MinVal% LSS 10   (SET Min=0%MinVal%) 		ELSE (SET Min=%MinVal%)
	IF %MonthVal% LSS 10 (SET Month=0%MonthVal%) 	ELSE (SET Month=%MonthVal%)
	IF %SecVal% LSS 10   (SET Sec=0%SecVal%) 		ELSE (SET Sec=%SecVal%)
	SET Year=%YearVal%

	SET TheDate=%Year%-%Month%-%Day%
	SET TheTime=%Hour%.%Min%.%Sec%


EXIT /b

Open in new window

0
 

Author Closing Comment

by:sknoll84
ID: 35731136
thanks
0
 
LVL 10

Expert Comment

by:ReneGe
ID: 35731392
You're welcome!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

New Windows 7 Installations take days for Windows-Updates to show up and install. This can easily be fixed. I have finally decided to write an article because this seems to get asked several times a day lately. This Article and the Links apply to…
While rebooting windows server 2003 server , it's showing "active directory rebuilding indices please wait" at startup. It took a little while for this process to complete and once we logged on not all the services were started so another reboot is …
This tutorial will walk an individual through locating and launching the BEUtility application and how to execute it on the appropriate database. Log onto the server running the Backup Exec database. In a larger environment, this would generally be …
To efficiently enable the rotation of USB drives for backups, storage pools need to be created. This way no matter which USB drive is installed, the backups will successfully write without any administrative intervention. Multiple USB devices need t…

572 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