Solved

Folder Copy Script

Posted on 2011-03-11
7
352 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
incorrect syntax near the order by 10 79
Adding a countdown to HTA 12 116
Crashing when deleting value with no child in Binary Tree 4 66
Problem to macro 5 24
The purpose of this article is to demonstrate how we can use conditional statements using Python.
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.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
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.

752 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