Archive Files Using
Automation - by Passing Arguments
In the previous post we
had a scenario where we had archived files form one folder to Archive folder
but the structure of the folder was fixed as we had hard coded the values in
the script. Now let us see how we can write a code that can work on any folder
structure.
The best part of any VB Script is it can accept
arguments from command line. As the script can accept the arguments from
command line we can have a bat file that can call the VB.
The VB script below would accept four parameters and
if four parameters not passed it would throw a message as "Not enough
Arguments". The four arguments w.r.t to the position of the arguments
is
Arg(0):{DriveName, E:},
Arg(1):{PathName, Test_E},
Arg(2):{ArchivePath, E:\Test_E\Archive\},
Arg(3):{Prefix, Test_E}
These arguments are for the requirement if the source
is E:\Test_D and Archive path is E:\Test_D\Archive and Files with prefix as
Test_E*.txt would be archived.
strComputer = "."
If WScript.Arguments.Count = 4
Then
Arg_1 =
cstr(WScript.Arguments.Item(0))
Arg_2 = cstr(WScript.Arguments.Item(1))
Arg_3 =
cstr(WScript.Arguments.Item(2))
Arg_4 =
cstr(WScript.Arguments.Item(3))
strName = strDrive &
"\" & "Test\"
Else
Wscript.Echo "Not
enough Argumets"
Wscript.Quit
End If
strDrive = Arg_1
StrPath = Arg_2
desPath = Arg_3
PrefixfName = Arg_4 &
"%"
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 '"&
prefixfName &"' AND Extension = 'txt'")
For Each objFile In colFiles
objF.CopyFile objFile.Name,
(desPath & objFile.Filename & "_" & _
Left(objFile.CreationDate,
Len(objFile.CreationDate) - 11) & "." & objFile.Extension)
Next
The above script is saved as ArchiveAny.vbs
The requirement as below
There are two folders in two different
drives
E:\Test_E and E:\Test_E\Archive\ and files have prefix
as Test_E
E:\Test_D and D:\Test_D\Archive\ and files have prefix
as Test_D
The bat file has following lines
@echo off
Cscript ArchiveAny.vbs
"e:" Test_E "e:\Test_E\Archive\" "E_Test"
Cscript ArchiveAny.vbs
"d:" Test_D "d:\Test_D\Archive\" "D_Test"
This is really a good way to automate the process of
automation if scheduled. We shall discuss on how such a process can be
scheduled.
Stay Tuned....
No comments:
Post a Comment