|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
WSH - Copying/Deleting files from one server to another.
Ok.......first let me explain I am not a scripting guru........but always willing to learn.
I have a server that creates a DB backup to the local file system in two different locations. The DB backup alternates between the two different folders. Now, I have a batch file that copies the contents of the (DB backup) folders to a mapped network drive on another server. The batch file creates a folder based on the current date, and copies the DB files into that date-named folder. However, I frequently run out of space.......as the batch file runs twice daily, every day. What I am needing to do, is execute something in the batch file that deletes the copied folders that are older than X amount of days. And to have this done prior to the copying of the current days files. I know this has to be possible, and am sure it is something simple. However, I am not as savvy with this stuff as most of you folks. Thanks in advance for your expertise! Bob |
|
#2
|
||||
|
||||
|
Hey, no1ncf,
I'll tell you like I tell most people. This is much easier to do using WSH than it is in batch. WSH provides much more flexibility and has a lot more built-in functions for working on remote computers. It also runs more efficiently which means that it will typically run faster than batch. Simply put, the easiest way to accomplish this in batch is with the help of a third-party program. There are several available for download that can delete files and folders based on date. If you're interested in a WSH approach, let me know.
__________________
Click the image if at any point you don't like my decision.Scripting problems? Windows questions? Ask the Windows Guru! |
|
#3
|
|||
|
|||
|
I am into using whatever is easier..........Let me know what I need to do in order to get it to work thru WSH.
THANKS!!!! Bob |
|
#4
|
||||
|
||||
|
Let me get a working model for us to work from and we'll customize it to your needs.
|
|
#5
|
|||
|
|||
|
Pardon my ignorance........But what do you mean by a working model??
Are you wanting the specs or the current batch file that I am using?? Thanks again............... |
|
#6
|
||||
|
||||
|
By working model, I meant a working script that we could tweak to your specific needs.
If you could post your batch file, that would help greatly. |
|
#7
|
|||
|
|||
|
Deleting Files
Hello,
I too am interested in knowing how to code WSH to delete files that are n days old. Would you share your solution with me also? Regards, Sharonp |
|
#8
|
||||
|
||||
|
I'm short on time at the moment, but I throw up a basic script for this shortly.
|
|
#9
|
|||
|
|||
|
Quote:
This post may help! Automatic File Delete |
|
#10
|
||||
|
||||
|
Here's a simple function that will delete a file if it is older than a given number of days. I've written both examples below to run against your Windows Temp folder so feel free to give them a test run.
Set the numDays variable equal to the number of days to check. I've included it in the function so you can effectively change it every time you call it. (Maybe different folders should be checked against different dates.) This script will check all files in a specified folder.... Code:
numDays = 7
strPath = "C:\Windows\Temp"
Set objfso = CreateObject("Scripting.FileSystemObject")
Set objFolder = objfso.GetFolder(strPath)
Set colFiles = objFolder.Files
For Each objFile In colFiles
DeleteOldFile objFile, numDays
Next
Function DeleteOldFile(objFile, numDays)
On Error Resume Next
dateFile = objFile.DateLastModified
dateToday = Date()
' dateToday = Now() will also work
If dateFile <= dateToday Then
daysOld = dateToday - dateFile
If daysOld > numDays Then
strFile = objFile.Name
objFile.Delete
If objFile Then
WScript.Echo "Could not delete", strFile
End If
End If
Else
WScript.Echo "The script has detected an incorrect date stamp."
End If
End Function
If want to perform a recursive delete for subfoldes as well, use the script below: Code:
numDays = 7
strPath = "C:\Windows\Temp"
Set objfso = CreateObject("Scripting.FileSystemObject")
Set objFolder = objfso.GetFolder(strPath)
' Use True to perform a recursive delete, False if not
CheckFolder objFolder, numDays, True
Function CheckFolder(objFolder, numDays, bRecurse)
Set colFiles = objFolder.Files
If colFiles.Count > 0 Then
For Each objFile In colFiles
DeleteOldFile objFile, numDays
Next
End If
If bRecurse Then
Set colSubfolders = objFolder.SubFolders
If colSubfolders.Count > 0 Then
For Each SubFolder In colSubfolders
CheckFolder SubFolder, numDays, True
Next
End If
End If
End Function
Function DeleteOldFile(objFile, numDays)
On Error Resume Next
dateFile = objFile.DateLastModified
' You may wish to use:
' dateFile = objFile.DateLastAccessed
' DateLastModified only reflects saved changes
dateToday = Date()
' dateToday = Now() will also work
If dateFile <= dateToday Then
daysOld = dateToday - dateFile
If daysOld > numDays Then
strFile = objFile.Path
objFile.Delete
If objFile Then
WScript.Echo "Could not delete", strFile
End If
End If
Else
WScript.Echo "The script has detected an incorrect date stamp."
End If
End Function
Hope this helps. |
|
#11
|
||||
|
||||
|
Note that the scripts above will not delete read-only/hidden/system files or files that are currently in use. If you would like to add this functionality let me know.
|
|
#12
|
||||
|