How do I move files to appropriate folder based on Month and Year? - vbscript

I have this very complicated requirement.
We have a bunch of zipped files downloaded from an ftp server into a folder in our local directory.
Then we use the code below to unzip the files.
Set objZip = CreateObject("XStandard.Zip")
Set FSO = CreateObject("Scripting.FileSystemObject")
Set fldr = FSO.GetFolder("C:\MUSK\FTP\MainFolder\")
For Each fil In fldr.Files
If LCase( Right( fil.Name, 4 ) ) = ".zip" Then
zipFilePath = fil.Path
objZip.UnPack zipFilePath, ("C:\MUSK\FTP\Current\")
End If
Next
So far so good.
Here is where problems come in.
These downloaded files have the following naming convention:
filename_month-day-year.zip
Example: Assuming today is May 16, 2012, the filename looks like this:
myFile_5-16-2012.zip
Our requirement is to grab the downloaded zipped files and place them in their correct folder.
For instance, we have folders named according month and year.
Example: We have JAN2012, FEB2012, etc
So taking myFIle_5-16-2012.zip as an example, the myFile_5-16-2012.zip is for MAY2012.
We would like to use the script above to grab the myFile_5-16-2012.zip and place it in the appropriate folder. In this example, the appropriate folder would be MAY2012 and then unzip it.
Basically, the MonthYear folder will replace this:
objZip.UnPack zipFilePath, ("C:\MUSK\FTP\Current\")
In other words, instead of the Current folder, it will be MAY2012 or whatever MonthYear combination.
Is this possible?
I would be more than happy to clarify. Sorry if I confused anyone.

This is pretty straight forward. I would:
Create a function which converts file name to appropriate MMMYYYY format
Use the FileSystemObject to determine if the folder name created in step 1 exists, and create if needed
Pass the full directory to your XStandard.Zip object
Check out the supported methods of FileSystemObject here:
http://msdn.microsoft.com/en-us/library/z9ty6h50(v=vs.85).aspx
You'll need .FolderExists and .CreateFolder, at least.
A quick VBScript I whipped up, could probably use some error checking and whatnot. Enjoy
' parse date, assumes file name is in foo_M-D-YYYY.ext format
Function parseDate(s)
dim dt
dt = CDate(split(split(s, "_")(1), ".")(0))
parseDate = Monthname(Month(dt)) & Year(dt)
End Function

Related

UFT File system test

Im a rookie on HP UFT testing and work on a data migration project I would like to automate.
Every day, we get a set of folder and files syncronize from a vendor with a following syncronization report(.csv file).
I would really like to test if the actual .csv file containing a list of files updated in the filesystem exists.
I get the .csv file on a network share, I open it and see a list of files with
data paths, which should be used to (loop)search though the filesystem and check if the files is actually on the location. How do I do that with UFT??
sample script to get all csv content and looping through content and verifying whether files exists or not.
filename = "C:\path\list.csv"
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(filename)
Do Until f.AtEndOfStream
filepath=f.ReadLine 'assuming every line as file full path
if FSO.fileexists(filepath) then
print filepath & " file is avaialble"
' do your checks here
else
print filepath & " file is not avaialble"
End if
Loop
f.Close

FlashAir Execute on Write Event Properties

Using the dropbox sample code I am working on a Lua script that will run each time a new file is added (a photo in this case) to a specific folder. I see examples to iterate through a folders files to upload them each in turn. What I need is the properties that are passed to the file that will execute each time a new file is written. I'm hoping it will be passed the filename for the last file created so I can use that to upload the file directly to Dropbox or FTP site.
HI someone in japan solved your doubt as the following.
last_filepath = ""
max_mod = 0
fpath = "/DCIM/100__TSB"
for filename in lfs.dir(fpath) do
filepath = fpath .. "/" .. filename
mod = lfs.attributes( filepath, "modification" )
if mod > max_mod then
max_mod = mod
last_filepath = filepath
end
end
basically it set a directory to search for the latest file with newest modification date/time.
details r here
http://dotnsf.blog.jp/archives/1040120555.html

Compare two files in two different folders and replace it by the newer

With this VBScript code I was able to copy files. If the file exists it does nothing, if not it will copy the files needed.
Dim Photo
SourceFolder = "C:\Photo1"
DistinationFolder = "C:\Photo2"
Set ObjPhoto = CreateObject("Scripting.FileSystemObject")
For Each Photo In ObjPhoto.GetFolder( SourceFolder).Files
If Not ObjPhoto.FileExists(ObjPhoto.BuildPath(DistinationFolder, Replace(Photo.Name, ".jpg", ".bmp"))) Then
photo.Copy ObjPhoto.BuildPath(DistinationFolder, Photo.Name), True
End If
Next
I want to compare the files if the source files also exists in the destination folder and replace it by the newer.
If you want to overwrite based on the last modified date, then the File object has the property you want: DateLastModified. (You can check all properties of the File object here.)
You already have access to the source file objects (your code's Photo variable) so you just need to get the target's file object.
Something like this should work:
Dim Photo
Dim targetFile, bmpTargetFilename, jpgTargetFilename
SourceFolder = "C:\Photo1"
DistinationFolder = "C:\Photo2"
Set ObjPhoto = CreateObject("Scripting.FileSystemObject")
For Each Photo In ObjPhoto.GetFolder(SourceFolder).Files
bmpTargetFilename = ObjPhoto.BuildPath(DistinationFolder, Replace(Photo.Name, ".jpg", ".bmp"))
jpgTargetFilename = ObjPhoto.BuildPath(DistinationFolder, Photo.Name)
If ObjPhoto.FileExists(bmpTargetFilename) Then
' Get the target file object
Set targetFile = ObjPhoto.GetFile(jpgTargetFilename)
' Now compare the last modified dates of both files
If Photo.DateLastModified > targetFile.DateLastModified Then
Photo.Copy jpgTargetFilename, True
End If
Else
Photo.Copy jpgTargetFilename, True
End If
Next
A couple of notes:
It seems you are checking for the existence of a .BMP file yet copying a .JPG file, so I made it explicit by using two variables.
I am also assuming you want to compare the JPG files, since those are the ones being copied.

How to read the contents of a .zip file with VBScript without actually extracting the files?

I have a .zip file that starts with a parent directory. I need to read that dir from the file then search my HD to see if that dir name already exists. If it exists, I then delete it and replace it the contents of the .zip file.
All of this I can do, except read the .zip without actually unzipping the file.
The .zip file can be upwards of 2G in size so I want to avoid unzipping, then reading the dir, then copying.
The reason I don't just unzip directly to the location and force an overwrite is that for some reason when using the CopyHere method to unzip, it ignores the switches that would normally force the overwrite and still prompts the user if they want to overwrite.
Code to unzip files:
Set objSA = CreateObject("Shell.Application")
Set objSource = objSA.NameSpace(pathToZipFile).Items ()
Set objTarget = objSA.NameSpace(extractTo)
objTarget.CopyHere objSource,4
Here is a similar question on SO.
How to list the contents of a .zip folder in c#?
I've used this library myself. It works well, http://dotnetzip.codeplex.com/, there is even a treeview example that appears to read the zip without extraction.
You will need the DLLs on the server, but I wouldn't say you have to install them. ;)
You can use For Each on your objSource object, for example:
Dim objSA, objSource, item
Set objSA = CreateObject("Shell.Application")
Set objSource = objSA.NameSpace(pathToZipFile).Items ()
For Each item in objSource
WScript.Echo item
Next
Assuming that you can use an external application, try downloading 7Zip and then have your script execute it with the -l switch. This should give you some output that you should be able to parse in some way.
Sample from the help file: 7z l archive.zip
I'm not sure if it is possible to read the contents of a zip without extracting it.
If you are just trying to avoid a time consuming copy operation on the data you could try unzipping to a temp directory and then using a "move" function. Move is usually less time consuming than copy as it doesn't actually re-write the data on the disk. It just updates the file system to point at where the data is.

Getting file list in a folder

I have created a tool which picks up a file from a specific location, copies it, zips it and then puts it at another location. The user has to select the required folders from the location.
Is there any way through which I can create an option in the tool so that the user can see the list of available folders at that location, or some way to direct the user directly to that location? I only need the folder names.
I tried it with cmd but since the location is not on my computer (it's on another computer with shared property) I dunno how to access that location. Any help, any hint is very much appreciated. My tool is in VBScript and ASP.
You can use a FileSystemObject to get the contents of a directory.
set fso = CreateObject( "FileSystemObject" )
set my_folder = fso.getFolder( "C:\Example" )
Then, use the Folder object to get its contents.
set sub_folders = my_folder.subFolders
for each f in sub_folders
wscript.echo( f.name & VBNEWLINE )
next

Resources