Announcement

Collapse
No announcement yet.

change file name to date with script

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

  • change file name to date with script

    Hi,

    Every week I'm running the printmig.exe on the print server to back up all of the printers.
    The management requires us to save 4 valid backups.

    The problem is that the script i use only starts the printmig.exe and saves the CAB file to the same folder all the time.

    The file name is always printer.cab


    I'm looking for a script that will run in scheduled task and check a specific folder, if a file called printer.cab exists - it will change it's name to [currentdate].cab

    in that way I will run 2 scheduled tasks - one to create the printer.cab, and the second 1 hour later to get the printer.cab file and change it's name to that day's date.

    I looked around for a simple script, but came up with nothing that I understand (due to my lack of knowledge in scripting)

    Any help?


    10X

    Poly.

  • #2
    Re: change file name to date with script

    Code:
    Set objFSO=CreateObject("Scripting.FileSystemObject")
    For Each  myfile In objFSO.GetFolder("c:\your_folder").Files	
    	If myfile.Name = "printer.cab" Then
    		'Do your stuff
    	End If
    Next

    Comment


    • #3
      Re: change file name to date with script

      Originally posted by ghostdog74 View Post
      Code:
      Set objFSO=CreateObject("Scripting.FileSystemObject")
      For Each  myfile In objFSO.GetFolder("c:\your_folder").Files	
      	If myfile.Name = "printer.cab" Then
      		'Do your stuff
      	End If
      Next

      Thanks but...

      why there is a "for each" trigger? there is only one file called printer.cab in this folder...

      the "do your stuff" was the important stuff since I don't know how to rename it from printer.cab to [date].cab
      Last edited by poly; 3rd September 2007, 08:50.

      Comment


      • #4
        Re: change file name to date with script

        OK, a bit later here's what I got so far:

        Set objFSO=CreateObject("Scripting.FileSystemObject")
        For Each myfile In objFSO.GetFolder("\\blabla\HotPrintBKP").Files
        If myfile.Name = "printer.cab" Then
        objFSO.MoveFile "\\blabla\HotPrintBKP\printer.cab" , \\blabla\HotPrintBKP\%date%.cab"
        end if
        next


        the problem is that it changes the file name from printer.cab to %date%.cab
        and I want it to change to 03-09-07.cab

        what am I doing wrong ?

        Comment


        • #5
          Re: change file name to date with script

          Found it, here is it to anyone who wants...

          Set objFSO=CreateObject("Scripting.FileSystemObject")
          For Each myfile In objFSO.GetFolder("\\blabla").Files
          If myfile.Name = "printer.cab" Then
          objFSO.MoveFile "\\blabla\printer.cab" , \\blabla\"&replace(date,"/","-")&".cab"
          end if
          next

          Poly

          Comment


          • #6
            Re: change file name to date with script

            Originally posted by poly View Post
            there is only one file called printer.cab in this folder...
            oh..my bad. well in that case, just directly rename the file, as you have did in your last mail. No need for the loop.

            Comment


            • #7
              Re: change file name to date with script

              Originally posted by poly View Post
              Found it, here is it to anyone who wants...
              Code:
              Set objFSO=CreateObject("Scripting.FileSystemObject")
                   For Each  myfile In objFSO.GetFolder("\\blabla").Files
              	If myfile.Name = "printer.cab" Then
                         objFSO.MoveFile "\\blabla\printer.cab" ,  \\blabla\"&replace(date,"/","-")&".cab"
                      end if
               next
              -


              In the script before this working script, you did use a Sytem Environment notation. These are not reconized by a vbscript, you have to use the function ExpandEnvironmentString(). Like in: objShell.ExpandEnvironmentString(c:\blahbla\%Compu tername%-printers.cab)
              But.. Note that date() is not an environment variable! So %date% does not exist et all.

              In the script above you use the function date(). That wil not be sufficient if you use it like this:
              strNewFilename = date & ".cab"
              There are a few problems when using it like that.
              1) In English the date seperator is \ that sign is not allowed in file names (it is good to replace the separators like you did in the script)
              2) The order of appearance of the date parts depents on the OS language. That will make the date() un-sortable.
              3) Zeros are wipped away in the date notation, then both January 11th and November 1st will give 2007111.

              In the example below I use something that may look like an environment variable, ( %Today%) but in fact has nothing to do with it. It is just like that for easy replacement purpose. In the part 'adjust the new file name' the date is converted to a yyyymmdd-notation, for better sorting purpose and avoid conflicting names. Then I use the function Replace() to replace "%Today%" in the file name with the actual date.

              Code:
              'Rename Just One File.vbs
              
              'Variables
              strOldFile = "c:\scripts\test.txt"
              strNewFile = "c:\scripts\%today%-test.txt"
              
              '----------------------------------------------------------
              'adjust the new file name
              dt = date() : dt = Year(dt)*1e4 + Month(dt)*1e2 + Day(dt)
              strNewFile = Replace(Lcase(strNewFile), "%today%", dt)
              
              'using the 'Move method' here instead of the 'Rename Method' 
              Set objFSO=CreateObject("Scripting.FileSystemObject")
              If objFSO.FileExists(strOldFile) Then
                objFSO.MoveFile strOldFile, strNewFile
                    ' check
                    With objFSO.GetFile(strNewFile)
                      msgbox "File: """ & .name & """ " & vbNewLine _
                             & " - is now present in -" & vbNewLine _
                             & "Folder: """ & .ParentFolder & """"
                    End With
              Else
                wscript.echo "Not found:", strOldFile
              End If
              '----------------------------------------------------------
              
              Wscript.quit
              (!) The code colored in 'Grey' in this example is optional, it is just for confirmation purpose

              \Rems
              Last edited by Rems; 3rd September 2007, 15:46.

              This posting is provided "AS IS" with no warranties, and confers no rights.

              __________________

              ** Remember to give credit where credit's due **
              and leave Reputation Points for meaningful posts

              Comment

              Working...
              X