Solved

Folder Copy Script

Posted on 2011-03-11
7
347 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Visual xHarbour 1 77
word0 challenge 4 53
Arduino EDI - Programming language 3 64
Path to Python 9 46
The purpose of this article is to demonstrate how we can use conditional statements using Python.
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.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
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.

760 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

19 Experts available now in Live!

Get 1:1 Help Now