Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: modify batch script to delete files  (Read 4829 times)

0 Members and 1 Guest are viewing this topic.

jormeno

    Topic Starter


    Rookie

    Thanked: 1
    • Experience: Familiar
    • OS: Windows 7
    modify batch script to delete files
    « on: September 04, 2013, 12:04:27 PM »
    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
    You won't know unless you try.

    Geek-9pm


      Mastermind
    • Geek After Dark
    • Thanked: 1026
      • Gekk9pm bnlog
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 10
    Re: modify batch script to delete files
    « Reply #1 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.

    jormeno

      Topic Starter


      Rookie

      Thanked: 1
      • Experience: Familiar
      • OS: Windows 7
      Re: modify batch script to delete files
      « Reply #2 on: September 04, 2013, 02:33:58 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.
      You won't know unless you try.

      Geek-9pm


        Mastermind
      • Geek After Dark
      • Thanked: 1026
        • Gekk9pm bnlog
      • Certifications: List
      • Computer: Specs
      • Experience: Expert
      • OS: Windows 10
      Re: modify batch script to delete files
      « Reply #3 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.  8)

      jormeno

        Topic Starter


        Rookie

        Thanked: 1
        • Experience: Familiar
        • OS: Windows 7
        Re: modify batch script to delete files
        « Reply #4 on: September 04, 2013, 03:24:34 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.  8)

        What I am working on is not for remote. Thanks for the assistance.
        You won't know unless you try.

        Linux711



          Mentor

          Thanked: 59
          • Yes
          • Programming Blog
        • Certifications: List
        • Computer: Specs
        • Experience: Experienced
        • OS: Windows 7
        Re: modify batch script to delete files
        « Reply #5 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
        YouTube

        "Genius is persistence, not brain power." - Me

        "Insomnia is just a byproduct of, "It can't be done"" - LaVolpe

        jormeno

          Topic Starter


          Rookie

          Thanked: 1
          • Experience: Familiar
          • OS: Windows 7
          Re: modify batch script to delete files
          « Reply #6 on: September 05, 2013, 08:36:40 AM »
          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?
          You won't know unless you try.