Announcement

Collapse
No announcement yet.

VBS script to move files X days old to another folder

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • VBS script to move files X days old to another folder

    I have been trying to get a vbs file that would move log files on a sql server to another one after the file has reached 24 hours of age.

    Is there anyone here that has such a file or know where i can go to get one. also a good resource to learn some vbs as i see this would be a very powerful tool

  • #2
    Re: VBS script to move files X days old to another folder

    Start the sticky topic for more info.
    Why not using Robocopy to copy the data and batch it?
    Marcel
    Technical Consultant
    Netherlands
    http://www.phetios.com
    http://blog.nessus.nl

    MCITP(EA, SA), MCSA/E 2003:Security, CCNA, SNAF, DCUCI, CCSA/E/E+ (R60), VCP4/5, NCDA, NCIE - SAN, NCIE - BR, EMCPE
    "No matter how secure, there is always the human factor."

    "Enjoy life today, tomorrow may never come."
    "If you're going through hell, keep going. ~Winston Churchill"

    Comment


    • #3
      Re: VBS script to move files X days old to another folder

      I need this to work for windows server 2003

      Comment


      • #4
        Re: VBS script to move files X days old to another folder

        robocopy works on windows 2003..
        So whats the problem
        Marcel
        Technical Consultant
        Netherlands
        http://www.phetios.com
        http://blog.nessus.nl

        MCITP(EA, SA), MCSA/E 2003:Security, CCNA, SNAF, DCUCI, CCSA/E/E+ (R60), VCP4/5, NCDA, NCIE - SAN, NCIE - BR, EMCPE
        "No matter how secure, there is always the human factor."

        "Enjoy life today, tomorrow may never come."
        "If you're going through hell, keep going. ~Winston Churchill"

        Comment


        • #5
          Re: VBS script to move files X days old to another folder

          looks this this will do the trick, thanks, this can be actived by the task service correct?

          Comment


          • #6
            Re: VBS script to move files X days old to another folder

            Originally posted by stopufool View Post
            I have been trying to get a vbs file that would move log files on a sql server to another one after the file has reached 24 hours of age.

            Is there anyone here that has such a file or know where i can go to get one. also a good resource to learn some vbs as i see this would be a very powerful tool
            Here's code to check all files in a Path, if a file has been modified in the last 24 hrs it will be moved to the DestPath.

            Use .DateCreated for total file age. It is currently in terms of hours, use "d" for days or if in "h" multiply by 24 to get # of days.

            Code:
            Option Explicit 
            
            On Error Resume Next
            
            Dim fso, FileSet, Path, File, DDiff, Date1, Date2, DestPath
            
            Path = "C:\WINDOWS\system32\Logfiles\W3SVC1"
            DestPath = "N:\SQLLogArchive\" 
            'DestPath must end with \
            FileSet = GetDirContents(Path) 
            
            For each File in FileSet 
             Set File = fso.GetFile(Path & "\" & File)
             Date1 = File.DateLastModified 
             '.DateCreated if you want 24hrs of life, this example is 24hrs since last written
             Date2 = Now()
            
              DDiff = Abs(DateDiff("h", Date1, Date2))
            
                If DDiff >= 24 Then
                  If Not fso.FileExists(DestPath & File.Name) Then
                    File.Move DestPath
                    'wscript.echo File.Name
                  Else
                    wscript.echo "Unable to move file [" & File.Name & "].  A file by this name already exists in the target directory."
                  End If
                End If
            Next 
            
            Function GetDirContents(FolderPath) 
             Dim  FileCollection, aTmp(), i 
             Set fso = CreateObject("Scripting.FileSystemObject") 
             Set FileCollection = fso.GetFolder(FolderPath).Files 
            
              Redim aTmp(FileCollection.count - 1) 
              i = -1 
            
                For Each File in FileCollection 
                   i = i + 1 
                   aTmp(i) = File.Name 
                Next 
            
              GetDirContents = aTmp 
            End Function
            Last edited by ahinson; 15th January 2007, 17:20. Reason: Added the move file code.
            Andrew

            ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

            Comment


            • #7
              Re: VBS script to move files X days old to another folder

              Originally posted by stopufool View Post
              looks this this will do the trick, thanks, this can be actived by the task service correct?
              open www.google.com
              Type in: Robocopy
              Click Search.
              guess what you find?
              Read the documentation and place it into a batchfile...
              test the batchfile
              Shedule the batchfile

              how hard can it be?
              Marcel
              Technical Consultant
              Netherlands
              http://www.phetios.com
              http://blog.nessus.nl

              MCITP(EA, SA), MCSA/E 2003:Security, CCNA, SNAF, DCUCI, CCSA/E/E+ (R60), VCP4/5, NCDA, NCIE - SAN, NCIE - BR, EMCPE
              "No matter how secure, there is always the human factor."

              "Enjoy life today, tomorrow may never come."
              "If you're going through hell, keep going. ~Winston Churchill"

              Comment


              • #8
                Re: VBS script to move files X days old to another folder

                Originally posted by Dumber View Post
                open www.google.com
                Type in: Robocopy
                Click Search.
                guess what you find?
                Read the documentation and place it into a batchfile...
                test the batchfile
                Shedule the batchfile

                how hard can it be?

                OR you can use the VBScript I created... Up to you.
                Andrew

                ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

                Comment


                • #9
                  Re: VBS script to move files X days old to another folder

                  thanks for the help guys, appreciate the help, thanks

                  Comment


                  • #10
                    Re: VBS script to move files X days old to another folder

                    To make the vb script "days" instead of "hours", is this where I would make the change:

                    Code:
                    DDiff = Abs(DateDiff("d", Date1, Date2))
                    Replace the "h" with a "d"?

                    Comment


                    • #11
                      Re: VBS script to move files X days old to another folder

                      Originally posted by ekrengel View Post
                      To make the vb script "days" instead of "hours", is this where I would make the change:

                      Code:
                      DDiff = Abs(DateDiff("d", Date1, Date2))
                      Replace the "h" with a "d"?
                      This is correct.
                      Andrew

                      ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

                      Comment


                      • #12
                        Re: VBS script to move files X days old to another folder

                        Really good script, thank you. I have a quick question:

                        How would I go about adding a portion to the script that excludes certain files based on their extension, for example I would like to exclude .xml files.

                        Thank you.

                        Comment


                        • #13
                          Re: VBS script to move files X days old to another folder

                          Originally posted by mchuman View Post
                          Really good script, thank you. I have a quick question:

                          How would I go about adding a portion to the script that excludes certain files based on their extension, for example I would like to exclude .xml files.

                          Thank you.
                          Change this section of the script:

                          Code:
                          If Not fso.FileExists(DestPath & File.Name) Then
                            File.Move DestPath
                            'wscript.echo File.Name
                          Else
                          To...

                          Code:
                          If Not fso.FileExists(DestPath & File.Name) Then
                            If Not File.Type = "XML Document" Then
                              File.Move DestPath
                              'wscript.echo File.Name
                            End If
                          Else
                          Last edited by ahinson; 10th February 2007, 06:42.
                          Andrew

                          ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

                          Comment


                          • #14
                            Re: VBS script to move files X days old to another folder

                            Thank you very much. I am fairly new to scripting, I usually just hack up scripts I find on the web. Just curious why .Extension didn't work. Is it because you are using a different library or something? Thanks again, how do I give you points?

                            Comment


                            • #15
                              Re: VBS script to move files X days old to another folder

                              Originally posted by mchuman View Post
                              Thank you very much. I am fairly new to scripting, I usually just hack up scripts I find on the web. Just curious why .Extension didn't work. Is it because you are using a different library or something? Thanks again, how do I give you points?
                              To give reputation points click the YIN/YANG symbol at the top right of one of my posts.

                              It [file.extension] doesn't work because it's not supported by the filesystem object. Since it's using a file collection, which supports the type property it was what I decided to use.

                              More Info on the filesystem object type property:
                              http://msdn2.microsoft.com/en-us/library/y7k0wsxy.aspx

                              This doesn't give you the extension though - which can be a problem for unknown file types. Since the XML file type should always be recognized because it's used by the system it worked well. In the case of an unknown file type you'd need to get the actual extension. The simplest way... Split the filename on ".", then take the last array element - that should be the extension. This way it also won't matter how many "."'s appear in the filename.

                              file.name.with.too.many.periods.txt will still work.

                              Split is a function that splits a string into an array of strings based on some split delimiter.

                              More Info on the split function:
                              http://msdn2.microsoft.com/en-us/library/0764e5w5.aspx

                              i.e.

                              Dim astrings()

                              astrings = split("file.name.with.too.many.periods.txt", ".", -1, 1)

                              The resulting array will look like the following (arrays are index based - meaning they start at 0)

                              element 0: file
                              element 1: name
                              element 2: with
                              element 3: too
                              element 4: many
                              element 5: periods
                              element 6: txt

                              to access the last element you'd do astrings.count -1. Since count returns a count starting at 1, you must subtract 1 to account for the indicies of the array element you want.

                              A simple function can be used to return the extension.

                              Code:
                              Function GetExtension(filename)
                              
                                Dim astrings()
                              
                                astrings = split(filename, ".", -1, 1)
                              
                                GetExtension = astrings(astrings.count - 1)
                              End Function
                              You'd then evaluate the return value of the function...

                              i.e. with relation to the code from the other script...

                              Code:
                              If Not File.Exists ... Then
                                If Not GetExtenstion(file.name) = "xml" Then 
                                  File.Move ...
                                End If
                              ...
                              Hope that helps.
                              Last edited by ahinson; 11th February 2007, 00:32.
                              Andrew

                              ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

                              Comment

                              Working...
                              X