Home » Uncategorized

Rename, Zip, Move all in VBScript

28 January 2006 3,337 views Comments

Running a dedicated web server has taught me alot, especially about backup and redundancy.

In the process of working through the best backup routines I had to develop a way to conserve some disk space and automate the movement and renaming of files around the server.  Having had a VB background I expected that VBScript would be the best option.

So below I have outlined the process (and scripts) I get my server to run every day to make manage these backup files.

Out of interest, I am using the Win 2003 backup service to do the backups, which basically puts a single file with all the incremental changes into a single location on the disk.  My objective was to rename the file, zip it and then move it to where I actually wanted it stored, ready for download in another automated FTP process each night.

Here’s the script:

‘This script will:
‘ – Rename the latest backup file to format BYYYYMMDD.BAK
‘ – ZIP the above file to BYYYYMMDD.zip
‘ – Copy the file to /save
‘ – Delete the file from the current directory

dim strDate
dim fso
dim objShell
dim strCommand

strDate = Year(Date()) & month(date()) & Day(Date())

set fso = CreateObject(”SCRIPTING.FileSystemObject”)

fso.MoveFile “c:\backup\backup.bkf”, “c:\backup\B” & strDate & “.bkf”

set objShell = WScript.CreateObject(”Wscript.Shell”)
strCommand = “”"C:\program files\WinZip\wzzip.exe”" c:\backup\B” & strDate & “.ZIP c:\backup\B” & strDate & “.bkf”
objShell.run strCommand ,0 ,true

fso.MoveFile “c:\backup\B” & strDate & “.zip”, “c:\backup\save\B” & strdate & “.zip”
fso.DeleteFile “c:\backup\B” & strdate & “.bkf”

set fso = Nothing
set objShell = Nothing

Basically it does the following:

  1. Initiates some variables
  2. Puts today’s date into a formatted string (which becomes part of the file name)
  3. Using a File System Object (FSO) it renames the backup’d file to a file with the date in it
  4. It then executes a command line to zip the renamed file, and specifically waits until execution of this part is complete.
  5. Finally it moves the ZIP file to the new location (save directory) and deleted the original backup file

Looking at this again now (I wrote this about 14 months ago) there are more efficient ways to do this, however it does the job and has never once failed on me.

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...
  • Joby
    Moving Files


    Moving a Set of Files
    Demonstration script that uses the FileSystemObject to move all the .txt files in a folder to a new location. Script must be run on the local computer.

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    objFSO.MoveFile "C:\FSO\*.txt" , "D:\Archive\"
  • Joby
    Thanks Jay, I was able to use this code. It came up on google as I searched for script to move files.
  • Praveen
    Rick ,

    can you post what you have done in yours? I'm also looking for a
    simillar work but couldent get it working.

    Pra
  • Rick Spurlin
    Jay,

    Thanks for posting this! I had been working at trying to get some similar code to do a variation of this job but for whatever reason, it kept bombing. Using what you had here, I was able to adapt mine and get it working.

    Thanks again for your post!

    Rick
  • jay
    Hi

    Have had a look at this (finally). The key is to include the * in the file to be zipped. Putting in * is equivalent to using *.*.

    So to zip a folder you could specific c:\files\*.* and everything in that folder will be zipped. Change the command line accordingly.

    Hope this helps.

    Jay
  • Gary Armstrong
    Jay,
    I would also like to be able to zip up all the files in a folder and move it to a network drive with a given name and the date appended to it. Any help would be greatly appreciated.
    Thanks
    Gary
  • jay
    I'll see what i can do, travelling at the moment, will be a day or two maybe.
  • Alexandre Fernandes
    Hi, my name is Alexandre.
    You made this script for only one file.
    I try to make one script that zip two files, move this zip file to another directory and then delete the original files but it doesn´t works.
    Do you have any idea to help me ?
    thanks.
blog comments powered by Disqus