Archive Files Using Automation
Let me discuss on a new
requirement that was in my project. The requirement is as follows.
1. A particular directory
would have a number of text files with different names but they all have a
similar prefixes.
2. The number of files are
too many which would make manual work tedious and time consuming.
3. The files must be
archived to the Archive file and they should be having a time stamp appended in
the file name.
4. The appended time stamp
should not be the archived date but the file created date. So that this helps
in checking the date which the file was created.
5. Once the files are
archived they would be deleted form the source directory.
The requirement is very
simple and a VB script would be doing the entire process with few lines in it.
Consider the folder
structure E:\Test\ and E:\Test\Archive.
E:\Test\ folder would have
10 files namely
Text_1.txt, Text_2.txt, Text_3.txt, Text_4.txt, Text_5.txt, Text_5.txt, Text_7.txt, Text_8.txt, Text_9.txt, Text_10.txt.
All these 10 files in
folder E:\Test\ to be archived to E:\Test\Archive.
Here is a sample VB Script
which would do satisfy the above requirement.
strComputer =
"." '
Comuter Name
strDrive = "e:" ' Drive Letter
strPath = "Test" 'Directory Name
desName = strDrive & "\" & strPath &
"\Archive\" 'Complete
path of Target Directory
Set objWMIService = GetObject("winmgmts:\\"
& strComputer)
Set objF =
CreateObject("Scripting.FileSystemObject")
Set colFiles = objWMIService.ExecQuery("Select * from
CIM_DataFile where Drive = '" & _
strDrive & "' AND Path = '\\" & strPath
& "\\' AND FileName Like 'Test%' AND Extension = 'txt'") ' this would fetch a cursor
like in PLSQL with all the matches
For Each objFile In colFiles
objF.CopyFile objFile.Name, (desName &
objFile.Filename & "_" & _
Left(objFile.CreationDate, Len(objFile.CreationDate) - 11)
& "." & objFile.Extension) 'Move
each file and rename it to archived directory
Next
Save the above code snippet in a file by name Archive.vbs and then call the vbs file from bat file. I would call my bat file Archive.bat. This bat file would have following line
@echo off
Start Archive.vbs
The bat file would call the vbs and archive all the files as required.
How ever the above code would be specific only to a specific structure. We wuld be discusing how this can be generalized to any folder structure.
Stay Tuned.......
No comments:
Post a Comment