vb script to copy and delete file system items
Took a few minutes this morning to help a buddy of mine script the archiving of files accross his network. There are tons of variations of this but here is what I came up with.
strFolder = "\\unc\path" 'or local path
dtstart = cdate(FormatDateTime(DateAdd("m",-1,Now),2) + " " + FormatDateTime(DateAdd("m",-1,Now),3))
copydir = "\\unc\path" 'or local path
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strFolder)
For Each objFile in objFolder.Files
dtmFileDate = objFile.DateLastModified
If dtmFileDate > dtStart Then
'Wscript.Echo objFile.Name
'objFile.copy(copydir + "\" + objFile.name)
'objFile.Delete True
End If
Next
wscript.echo "Done"
Let's quickly break this done line at a time.
Line 1
strFolder = "\\unc\path" 'or local path
Declaring a local variable to hold the name of the folder we want to copy from.
Line 2
dtstart = cdate(FormatDateTime(DateAdd("m",-1,Now),2) + " " + FormatDateTime(DateAdd("m",-1,Now),3))
Declaring and setting a value for a date that we will use to compare to the dates of files. Lokk at the dateadd "m" -1. this is adding a minus 1 to the current date. The second portion of this is for the time portion of the date. You can change the interval by increasing the -1 to however many months back you want to go.
Line 3
copydir = "\\unc\path" 'or local path
Declaring a local variable to hold the name of the folder we want to copy to.
Line 5
Set objFSO = CreateObject("Scripting.FileSystemObject")
Initializing a new variable for object creation.
Line 6
Set objFolder = objFSO.GetFolder(strFolder)
setting the folder attribute of the File System Object.
Line 8
For Each objFile in objFolder.Files
Start our "For" loop.
Line 9
dtmFileDate = objFile.DateLastModified
Declare and set a variable each time in this loop that will coorespond to the last modified date of the file now being processed.
Line 11
If dtmFileDate > dtStart Then
Begin our comparison. If the last modified date is newer than the start date (Set in Line 2) then go to the next line of code in this loop.
Line 12
'Wscript.Echo objFile.Name
This line has been commented out. It will not run because of the single tick ' character. Uncomment this line by removing the ' and it will print thwe current file name to the screen. This is helpful in debugging.
Line 13
objFile.copy(copydir + "\" + objFile.name)
Do the work. Copy the file in the loop to the destination.
Line 14
objFile.Delete True
Delete the file from the original location.
Line 15
End If
This is the end of the if statement block.
Line 16
Next
Loop to the next record.
Line 18
wscript.echo "Done"
This will send a message to the display stating the the job is completed. This can be commented but is also helpful in debugging.
This script can be referenced and schedule in a windows acheduled task or a SQL Server agent job and run on a schedule in an automated fashion to help alleviate the burdon of administration. There are also many error catching and handling features and steps that can be added to this script to help when the process runs into hiccups.
Error handling and logging is a seperate arena and will be tackled on the next post. Until the Happy Coding and Good Luck!
strFolder = "\\unc\path" 'or local path
dtstart = cdate(FormatDateTime(DateAdd("m",-1,Now),2) + " " + FormatDateTime(DateAdd("m",-1,Now),3))
copydir = "\\unc\path" 'or local path
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strFolder)
For Each objFile in objFolder.Files
dtmFileDate = objFile.DateLastModified
If dtmFileDate > dtStart Then
'Wscript.Echo objFile.Name
'objFile.copy(copydir + "\" + objFile.name)
'objFile.Delete True
End If
Next
wscript.echo "Done"
Let's quickly break this done line at a time.
Line 1
strFolder = "\\unc\path" 'or local path
Declaring a local variable to hold the name of the folder we want to copy from.
Line 2
dtstart = cdate(FormatDateTime(DateAdd("m",-1,Now),2) + " " + FormatDateTime(DateAdd("m",-1,Now),3))
Declaring and setting a value for a date that we will use to compare to the dates of files. Lokk at the dateadd "m" -1. this is adding a minus 1 to the current date. The second portion of this is for the time portion of the date. You can change the interval by increasing the -1 to however many months back you want to go.
Line 3
copydir = "\\unc\path" 'or local path
Declaring a local variable to hold the name of the folder we want to copy to.
Line 5
Set objFSO = CreateObject("Scripting.FileSystemObject")
Initializing a new variable for object creation.
Line 6
Set objFolder = objFSO.GetFolder(strFolder)
setting the folder attribute of the File System Object.
Line 8
For Each objFile in objFolder.Files
Start our "For" loop.
Line 9
dtmFileDate = objFile.DateLastModified
Declare and set a variable each time in this loop that will coorespond to the last modified date of the file now being processed.
Line 11
If dtmFileDate > dtStart Then
Begin our comparison. If the last modified date is newer than the start date (Set in Line 2) then go to the next line of code in this loop.
Line 12
'Wscript.Echo objFile.Name
This line has been commented out. It will not run because of the single tick ' character. Uncomment this line by removing the ' and it will print thwe current file name to the screen. This is helpful in debugging.
Line 13
objFile.copy(copydir + "\" + objFile.name)
Do the work. Copy the file in the loop to the destination.
Line 14
objFile.Delete True
Delete the file from the original location.
Line 15
End If
This is the end of the if statement block.
Line 16
Next
Loop to the next record.
Line 18
wscript.echo "Done"
This will send a message to the display stating the the job is completed. This can be commented but is also helpful in debugging.
This script can be referenced and schedule in a windows acheduled task or a SQL Server agent job and run on a schedule in an automated fashion to help alleviate the burdon of administration. There are also many error catching and handling features and steps that can be added to this script to help when the process runs into hiccups.
Error handling and logging is a seperate arena and will be tackled on the next post. Until the Happy Coding and Good Luck!
