Solved

Folder Copy Script

Posted on 2011-03-11
7
348 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

920 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

16 Experts available now in Live!

Get 1:1 Help Now