Solved

Folder Copy Script

Posted on 2011-03-11
7
350 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

 
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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
firstswap challenge 20 83
rhino JavaScript import, load 25 95
T-SQL:  Sigh---Boy, this is fun.... 12 57
learn programming 8 43
This is an explanation of a simple data model to help parse a JSON feed
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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…

808 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