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

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.

Related

Replace a text file which may or may not contain a space in file name

I want to replace a file named with different name using VBScript. The input file name may or may not contain blank spaces.
Set objFSO = CreateObject("Scripting.FileSystemObject")
' First parameter: original location\file
' Second parameter: new location\file
objFSO.CopyFile "D:\Development\abc def.txt", "D:\Development\xyz.txt"
Perhaps surprisingly, CopyFile creates a copy of the source file. To rename a file you could use MoveFile, but the usual approach is to simply change the name of the file:
Set fso = CreateObject("Scripting.FileSystemobject")
fso.GetFile("D:\Development\abc def.txt").Name = "xyz.txt"
Edit: If you actually mean to replace one file with another file, you can do so with CopyFile by setting the third parameter (overwrite) to True, as #Lankymart pointed out in the comments.
fso.CopyFile "D:\Development\abc def.txt", "D:\Development\xyz.txt", True
If you don't want to keep the source file you need to delete it after the copy operation (VBScript doesn't allow moving a file over an existing file). Alternatively you can delete the destination file first and then move or rename the source file.

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

Visual basic extract folder names and compare them with active directory users

im a newbie in vb script writing and I want to extract folder names and compare them with active directory users I started with the following code:
Set fs = CreateObject("Scripting.FileSystemObject")
'Log file name
Set logFile = fs.OpenTextFile("fileNameLogs.txt", 2, True)
'Directory you want listed
Set folder = fs.GetFolder("\\server\Data\Users")
Set files = folder.files
For Each file in files
logFile.writeline(file.name)
Next
logFile.close
This script only extracts the file names not the folder names.
Can any one help me continue and extract the folder names instead of the file names so I can compare them active directory.
You are on the right track just use the .SubFolders object collection to iterate through the Folder objects in the current Folder object similar to the approach you have used for the File objects.
Option Explicit
Dim fs, logFile
Dim folder, subFolders, subFolder
Dim files, file
Set fs = CreateObject("Scripting.FileSystemObject")
'Log file name
Set logFile = fs.OpenTextFile("fileNameLogs.txt", 2, True)
'Directory you want listed
Set folder = fs.GetFolder("\\server\Data\Users")
'Write out sub folders
Set subFolders = folder.SubFolders
For Each subFolder In subFolders
logFile.Writeline(subFolder.Name)
Next
Set subFolders = Nothing
'Write out files
Set files = folder.files
For Each file In files
logFile.Writeline(file.Name)
Next
Set files = Nothing
logFile.Close
'Clean-up and reclaim memory
Set logFile = Nothing
Set folder = Nothing
Set fs = Nothing

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

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

creation of a text file in a specified location and remove the old one

I need to create a text file "setup.txt" in the location C:\Documents and Settings\All Users\Application Data\xerox\setapp in VB script
the location C:\Documents and Settings\All Users\Application Data is common apllication data folder here we can
use word "CSIDL_COMMON_APPDATA" or &H23& for that.Here the other which i need to take care is if any setup.txt file are
already there in that location i need to remove that old one i need to insert the new "setup.txt" which contatin blank
values means new one
I am new to this vbscript ,and i wanted an optimized code to acheive that functionality
You could open the file for writing and write a blank line, this will create the new text file and overwrite any previous versions.
strFilename = " C:\Documents and Settings\All Users\Application Data\xerox\setapp.txt"
Set objFile = CreateObject("Scripting.FileSystemObject").OpenTextFile(strFilename,2,true)
'Write stuff to the file here
objFile.Close
Set objFile = nothing
It's not clear if your script is creating the log file, if not and you want to copy the file and overwrite any previous ones you could do this (setting the last argument in the CopyFile method to true will overwrite older versions).
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CopyFile Source, Destination, true
Set fso = Nothing
If you just want to delete the previous file if it exists you could do this (here setting the last argument in the DeleteFile method to true will force deletion of the file).
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(file) Then
fso.DeleteFile file, true
End If
Set fso = Nothing
Wow, VBScript, it was a long time ago ... But I'll give it a shot.
What you need, if I understand you correctly, is to use the FileSystemObject it contains methods for deleting, creating and copying text files.
I hope this at least gives you some pointers to get you started.

Resources