Set Archive Bit Based On Creation Date in WSH
Properly constructed incremental backup schemes use a file’s archive bit to determine what files to include in each backup increment. Currently indexed files should have the archive bit set, while modified or newly created files should have it unset to mark them for inclusion in the next backup. The following code sample can be used to set the archive bit based on a file’s creation date in WSH.
If you’ve installed the Windows Backup program in Windows XP, it will set archive bits based on age. The default will set an archive bit on any file older than 90 days so this example will do the same.
The code sample we will be creating will look into a provided folder and enumerate all files in that folder. It will then move through each file, check for the existence of an archive bit, and set it if the file is older than the set number of days.
The first step in creating this code sample will be to establish our constants. This includes the number of days that represents an archived file’s age (90 days) and the folder where the script should operate. This is done quite simply by assigning a couple of variables.
numDays = 90 strPath = "C:WindowsTemp"
Next, we’ll instantiate the FileSystemObject and check to see that our folder path really exists.
Set objfso = CreateObject("Scripting.FileSystemObject") If objfso.FolderExists(strPath) Then 'Make sure folder exists
If is does in fact exist, we should instantiate the folder object using the FileSystemObject’s GetFolder method and enumerate its file collection with the Folder object’s Files property. Keep in mind that this should all be done inside of the IF statement to prevent any errors that would occur if the path did not exist.
Set objFolder = objfso.GetFolder(strPath) Set colFiles = objFolder.Files 'Enumerate files
Once we’ve created the collection of files, we should move through each of them with a For Each loop and call a subroutine that will do the work of checking and setting the archive bit. We’ll be creating this function later.
Again, to prevent errors, we should nest the For Each loop inside of an IF statement that verifies that the file collection is not empty. We can do this simply by checking that the collection’s count property does not return 0.
If colFiles.Count > 0 Then For Each objFile In colFiles Call SetArchiveFlag(objFile, numDays) 'Call subroutine for each file found. Next Else 'colFiles collection was empty WScript.Echo "No files in folder", objFolder.Path End If
In this example, I’ve used the Else branch of our If statement to display a message box if there are no files in the file collection. This should be replaced with logging code or any other appropriate error-handling code.
The same is true as we complete our first IF statement. This Else branch will execute if the supplied folder path was not valid.
Else 'Folder path could not be found WScript.Echo "The specified folder", strPath, "does not exist" End If
That completes the basic code construct. This could be incorporated into a class or function in a larger script. Now we need to create the subroutine that will actually check and set the archive bit on each file.
Please use the trackback link when linking to this post.

































August 5th, 2007 at 9:35 am
After posting this article, I realized one other problem spot that you may wish to check for errors. If you are running this on a large directory, or perhaps a shared one, you may want to include an IF statement that checks to make sure that a file still exists before calling the SetArchiveBit subroutine. This would prevent errors if a file were to be moved or deleted AFTER being added to the File collection but BEFORE being processed by the subroutine.
August 5th, 2007 at 10:10 am
Excellent article - rather simple code, and very useful.
Two suggestions though: add some sort of message when the code finish successfully, and also what about recursive code that will set archive bit on all files in folder plus all files in all sub folders? this can be pretty cool addition to the code.
August 5th, 2007 at 10:16 am
Thanks for the comments, Shadow.
Let me take your suggestions in order. First, I didn’t feel it necessary to include a message when the code completes, because it isn’t actually intended to run as a stand-alone script. It’s designed to be incorporated into a full backup scheme. In that case, no notification would be necessary.
As for your second, suggestion, the original code sample for this post actually does contain recursion. I left it out of this sample for sake of brevity. However, I’ll do a follow up post to show how this can be included so stayed tuned for my next blog!
August 5th, 2007 at 1:11 pm
Fantastic entry, Nilpo. Thank you.
The code is efficient and the way you approached the situation is rather amazing.
Keep up posting proactively and all the best.
Regards,
Tony
August 5th, 2007 at 1:19 pm
@ Tony: Thank you very much. I’ll keep posting as long as readers keep sending questions!
@ Shadow Wizard: Would you like to see archive bits on folders as well? I tend to avoid using them because I feel as though I’m forced to assume that the folder contents are all set as well….and if I don’t want to assume, I check, and that defeats the purpose of using an archive bit on a folder anyway. It also seems to me that most backup routines rely on the file level bits anyway. What’s your take?