1.

Solve : modify batch script to delete files?

Answer»

So Sidewinder helped me create a .bat and .vbs script that work like a charm. But now I need to modify it to delete all .zip files older than a week old. I have tried various methods and i seen the main method is to use forfiles but i can not install that on any computers. So doing it via batch would be perfect if i have to use another .vbs script that is fine to but prefer .bat

Batch file code
echo Backing up your Database....This may take awhile.....
sqlcmd -S ServerName -i backupDBscript.sql
SET MY_Path=pathtofiles
set mydate=%date:~4,2%%date:~7,2%%date:~10,4%
for %%x in (*.bak) do cscript "%MY_PATH%\zip.vbs" "%MY_PATH%\%%x" "%MY_PATH%\DB_%mydate%.zip"
del "%MY_PATH%\*.bak
exit

.vbs file to compress database
Set oFSO = CreateObject("Scripting.FileSystemObject")
ToZip    = oFSO.GetAbsolutePathName(WScript.Arguments.Item(0))
ZipName  = oFSO.GetAbsolutePathName(WScript.Arguments.Item(1))

d=WindowsZip(ToZip, ZipName)

Function WindowsZip(sFile, sZipFile)
  Set oZipShell = CreateObject("WScript.Shell")
  Set oZipFSO   = CreateObject("Scripting.FileSystemObject")
  If Not oZipFSO.FileExists(sZipFile) Then
    NewZip(sZipFile)
  End If
  Set oZipApp = CreateObject("Shell.Application")
  sZipFileCount = oZipApp.NameSpace(sZipFile).items.Count
      aFileName = Split(sFile, "\")
      sFileName = (aFileName(Ubound(aFileName)))
          sDupe = False
  For Each sFileNameInZip In oZipApp.NameSpace(sZipFile).items
    If LCase(sFileName) = LCase(sFileNameInZip) Then
        sDupe = TRUE
        Exit For
    End If
  Next
  If Not sDupe Then
        wscript.echo "Adding " & sfile
        oZipApp.NameSpace(sZipFile).Copyhere sFile
        On Error Resume Next
        Do Until sZipFileCount < oZipApp.NameSpace(sZipFile).Items.Count
            Wscript.Sleep(100)
        Loop
        On Error GoTo 0
  End If
End Function

Sub NewZip(sNewZip)
  Set oNewZipFSO  = CreateObject("Scripting.FileSystemObject")
  Set oNewZipFile = oNewZipFSO.CreateTextFile(sNewZip)
  oNewZipFile.Write Chr(80) & Chr(75) & Chr(5) & Chr(6) & String(18, 0)
  oNewZipFile.Close
  Set oNewZipFSO = Nothing
  Wscript.Sleep(500)
End Sub
May I ask a question?
Is thee a good REASON not to use Vb Script?
I have read that it can be used with remote assistance,  so I don't undersand why you would prefer batch.
Just curious. Quote from: Geek-9pm on September 04, 2013, 02:28:38 PM

May I ask a question?
Is thee a good reason not to use Vb Script?
I have read that it can be used with remote assistance,  so I don't undersand why you would prefer batch.
Just curious.

For less clutter. Thats why if it can be added to the zip.vbs that would be awesome if not in the batch that is fine too. been researching this like crazy.Here is a post that might be relevant to what your want.
Need a VBScript that calls a batch file to run on remote computers
Hope that helps some.  Quote from: Geek-9pm on September 04, 2013, 03:18:55 PM
Here is a post that might be relevant to what your want.
Need a VBScript that calls a batch file to run on remote computers
Hope that helps some. 

What I am working on is not for remote. Thanks for the assistance. I think this is what you need. The only issue is it will not look for files in subfolders. You just need to uncomment the deletefile part and change the path.

Code: [Select]Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "D:\htdocs"

Set objFolder = objFSO.GetFolder(objStartFolder)

Set colFiles = objFolder.Files
nowDate = Date
For Each objFile in colFiles
fileDate = objFile.DateLastModified
howLong = DateDiff("w", fileDate, nowDate)
If howLong > 1 then
wscript.Echo objFile.Path
'objFSO.DeleteFile objFile.Path
End If
Next
Quote from: Linux711 on September 04, 2013, 07:06:08 PM
I think this is what you need. The only issue is it will not look for files in subfolders. You just need to uncomment the deletefile part and change the path.

Code: [Select]Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "D:\htdocs"

Set objFolder = objFSO.GetFolder(objStartFolder)

Set colFiles = objFolder.Files
nowDate = Date
For Each objFile in colFiles
fileDate = objFile.DateLastModified
howLong = DateDiff("w", fileDate, nowDate)
If howLong > 1 then
wscript.Echo objFile.Path
'objFSO.DeleteFile objFile.Path
End If
Next

Should I add this to the zip.vbs file or create  a new file and add the code there?


Discussion

No Comment Found