Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script to copy files from one folder to another 2

Status
Not open for further replies.

yoursdavinder

Programmer
Aug 23, 2007
4
IN
I am trying to write a script to copy files from a specific date range to some specific range to another folder.
For E.g. I want to copy all the files that are having the Date modified from July 1st to July 31st from C:\Download to C:\Download\July.

Please help ASAP.

Thanks.
 
Look into using the File System Object.

It has a function MoveFile, which can take the path of your original file and the path of your destination file as arguments.

As far as the date goes, the FSO also has methods for finding the CreationDate. Check
 
You will probably want to use File.DateLastModified instead of File.CreationDate.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
I have written the below script and when executing the same it is moving only one file at a time from source to destination. I need to execute the script manually everytime to move every file.
Please check and correct the same accordingly and let me know.

Thanks.

Set objFSo = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("D:\Songs")

Set colFiles = objFolder.Files

dtmOldestDate = Now

For Each objFile in colFiles
If objFile.DateCreated < dtmOldestDate Then
dtmOldestDate = objFile.DateLastModified
strOldestFile = objFile.Path
End If
Next

objFSO.MoveFile strOldestFile, "D:\Songs\Old Files Test\
 
Move the worker code inside the For Each/Next

objFSO.MoveFile strOldestFile, "D:\Songs\Old Files Test\"
Next

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Thanks for the above information Mark.

Can you help me in making some more changes to the above script?

I would like to add condition that will move all the files from 1st of a month to the 31st of a month in another folder.

For E.g. If I execute the script on 1st September then it should catch all the files created from 1st August to 31st August and move to the folder I desire.

Thanks,
Dave
 
You need to manipulate the dates to make such determinations.

So something like this:
Code:
Dim fso, oFile, CurrentMonth,NextMonth
Set fso = CreateObject("scripting.filesystemobject")

Set oFile = fso.GetFile("C:\UpdatePatch.log")

CurrentMonth = Month(oFile.DateLastModified)
If CurrentMonth = 12 Then
	NextMonth = 1
Else
	NextMonth = Month(oFile.DateLastModified)+1
End If

WScript.Echo CurrentMonth
WScript.Echo NextMonth

Once you know the month you want you can use the variable "NextMonth" as part of your copy command to indicate the destination location.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
dim objFSO, objFolder
dim colFiles, oldLocation, newLocation
dim currentDate, lastMonthDate, lastMonth, numDays, begDate, endDate

oldLocation = "D:\Songs"
newLocation = "D:\Songs\Old Files Test\"

Set objFSo = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(oldLocation)
Set colFiles = objFolder.Files

currentDate = Now()
lastMonthDate = DateAdd("m",-1,currentDate)
lastMonth = Month(lastMonthDate)
begDate = DateSerial(Year(lastMonthDate), lastNMonth,1)

select case lastMonth
case 1,3,5,7,8,10,12
numDays = 31
case 4,6,9,11
numDays = 30
case 2
if isLeapYeap(Year(lastMonthDate)) then
numDays = 29
else
numDays = 28
end if
end select

endDate = DateSerial(Year(lastMonthDate), lastMonth,numDays)

For Each objFile in colFiles
    If ((objFile.DateCreated > begDate) AND (objFile.DateCreated < endDate)) Then
     objFSO.MoveFile oldLocation, newLocation 
    End If
Next


 
Oops, I meant to actual post something other than just code. :)

The above script will grab the most previous month in realtion to the server's time, then move all the files that are within it.

You'll need a script for the isLeapYear function to make it work, but that's pretty easy. I'll see if I can't get you one before the end of the day.
 
Code:
dim objFSO, objFolder
dim colFiles, oldLocation, newLocation
dim currentDate, lastMonthDate, lastMonth, numDays, begDate, endDate

oldLocation = "D:\Songs"
newLocation = "D:\Songs\Old Files Test\"

Set objFSo = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(oldLocation)
Set colFiles = objFolder.Files

currentDate = Now()
lastMonthDate = DateAdd("m",-1,currentDate)
lastMonth = Month(lastMonthDate)
begDate = DateSerial(Year(lastMonthDate), lastNMonth,1)

select case lastMonth
	case 1,3,5,7,8,10,12
		numDays = 31
	case 4,6,9,11
		numDays = 30
	case 2
		if isLeapYeap(Year(lastMonthDate)) then
			numDays = 29
		else
			numDays = 28
		end if
end select

endDate = DateSerial(Year(lastMonthDate), lastMonth,numDays)

For Each objFile in colFiles
    If ((objFile.DateCreated > begDate) AND (objFile.DateCreated < endDate)) Then
    	objFSO.MoveFile oldLocation, newLocation 
    End If
Next
 
Here's the code for finding the leapYear.

Code:
function isLeapYear(thisYear)
	if thisYear % 400 = 0 then
		isLeapYear = true
  else
		if thisYear % 4 = 0 and thisYear % 100 > 0 then
			isLeapYear = true
		else      
			isLeapYear = false			
		end if
  end if
end function
 
Oh and the line

Code:
If ((objFile.DateCreated > begDate) AND (objFile.DateCreated < endDate)) Then

should be

Code:
If ((objFile.DateCreated >= begDate) AND (objFile.DateCreated <= endDate)) Then
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top