Solved

Folder Copy Script

Posted on 2011-03-11
7
349 Views
Last Modified: 2013-11-10
Hello,

I would like to copy a folder to a network share with creating a folder under the current date and time.

Comments appreciated.

Thanks
0
Comment
Question by:leolink1982
7 Comments
 
LVL 15

Accepted Solution

by:
markdmac earned 168 total points
ID: 35116203
Here you go.  This will do what you ask.  Change the source and destination paths.  End each with the backslash.
Set objFSO = CreateObject("Scripting.FileSystemObject")

SourceFolder = "C:\Temp\"
DestRoot = "\\server\share\"

'Create the Time variables
sDate=Right("0" & Month(Date),2) _
& "-" & Right("0" & Day(Date),2) _
& "-" & Right(Year(Date),2)

sTime = Right("0" & DatePart("h", Now),2) & Right("0" & DatePart("n", Now),2)

'Build the folder string
DestFolder = DestRoot & sDate & sTime

'Create the folder
If Not objFSO.FolderExists(DestFolder) then
   Set oFolder = objFSO.CreateFolder(DestFolder)
   Set oFolder = Nothing
End If

'Now copy the source to the destination
Set objSource = objFSO.GetFolder(SourceFolder)
objSource.Copy(DestFolder)

Open in new window

0
 
LVL 10

Expert Comment

by:ReneGe
ID: 35303585
Here is a batch file that will do it.

Cheers,
Rene

 
@ECHO OFF


CALL :GetDateTime

SET Source=C:\Temp
SET Destination:\\Computer\C$\Temp\%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
 
LVL 10

Expert Comment

by:ReneGe
ID: 35303592
I forget to put quots in this command line:
IF NOT EXIST %Destination% MD %Destination%

Should have been:
IF NOT EXIST "%Destination%" MD "%Destination%"

Just in case it contains spaces.

0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 10

Assisted Solution

by:ReneGe
ReneGe earned 166 total points
ID: 35303607
Corrected

 
@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
 
LVL 3

Assisted Solution

by:tempter
tempter earned 166 total points
ID: 35339425
Hi mate,

I have used the namedate utility for ages and its the simplest date tool for anyfile anywhere, simply copy to the win32 folder and it will work forever.

If your not too familiar with VBS script, then this will be much easier in BAT file

Namedate site = http://www.informatics-consulting.ch/software/namedate.htm

You can play with the date stamp output to be 13-MAR-2011 or 20110313 or what formwat u prefer

Script is sample, modify to your needs

Hope this helps
Morci  
REM --== START OF SCRIPT ==--
@ECHO OFF

SET Source=C:\SourceFolder
MKDIR \\RemotePC\C$\DestinationFolder
SET Destination:\\RemotePC\C$\DestinationFolder

IF NOT EXIST %Destination% MD %Destination%

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

NAMEDATE /D /F /K \\RemotePC\C$\DestinationFolder

ENDIF

REM --== END OF SCRIPT ==--
REM Your output should be = \\Server1\c$\16-FEB-11_DestinationFolder

Open in new window

0
 
LVL 53

Expert Comment

by:Dhaest
ID: 35735825
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

810 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