Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
MsgBox not showing
#1
Solved: 11 Years, 6 Months ago
I am trying to copy a folder using FSO in vb using below code :
Code:
Dim FSO
Set FSO = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
FSO.CopyFolder "C:\Program Files\Test", "E:\Test"

If err.number <> 0 Then
    FSO.CreateFolder "E:\Test"
    FSO.CopyFolder "C:\Program Files\Test", "E:\Test"
    MsgBox "Folder Copied"
End If

With this code MsgBox doesnt show up, but the folder is copied successfully. But if I move the MsgBox statement outside the If segment, then it shows correctly.

Code:
Dim FSO
Set FSO = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
FSO.CopyFolder "C:\Program Files\Test", "E:\Test"

If err.number <> 0 Then
    FSO.CreateFolder "E:\Test"
    FSO.CopyFolder "C:\Program Files\Test", "E:\Test"    
End If
MsgBox "Folder Copied"

Someone please explain this behavior.
Reply
#2
Solved: 11 Years, 6 Months ago
Thats because there is no error encountered and hence the IF loop is not satisfied. Only on an error would the err.number will bring up an error number and traverse into the loop. When there was no error encountered it skipped the If loop and went to the next line. However the copy has already happened outside of the loop and hence you see that behaviour.
You have to use an Else to skip the If condition and put up your msgbox there.
Code:
Dim FSO
Set FSO = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
FSO.CopyFolder "C:\Program Files\Test", "E:\Test"

If err.number <> 0 Then
    FSO.CreateFolder "E:\Test"
    FSO.CopyFolder "C:\Program Files\Test", "E:\Test"    
Else
   MsgBox "Folder Copied"
End If

you could see that the folder is not copied if you try this code below,
Code:
Dim FSO
Set FSO = CreateObject("Scripting.FileSystemObject")
On Error Resume Next

If err.number <> 0 Then
    FSO.CreateFolder "E:\Test"
    FSO.CopyFolder "C:\Program Files\Test", "E:\Test"    
         msgbox "Files Copied"
End If
Basanth
Give a fish to a man and you feed him for a day..Teach a man how to fish and you feed him for life.
Reply
#3
Solved: 11 Years, 6 Months ago
@Basanth

I dont have "E:\Test" in my E Drive, as i have explicitly deleted it myself.
So I suppose the flow goes in the If Condition, but still MsgBox doesnt show up.
Reply
#4
Solved: 11 Years, 6 Months ago
I just ran the below code and it never created the directory. Try this,
Code:
Dim FSO
Set FSO = CreateObject("Scripting.FileSystemObject")
On Error Resume Next

If err.number <> 0 Then
    FSO.CreateFolder "E:\Arebest"
    FSO.CopyFolder "C:\Program Files\Arebest", "E:\Arebest"    
         msgbox "Files Copied"
End If

Do you see a directory called Arebest on E:\ ?? If yes, your machine is haunted Smile
Basanth
Give a fish to a man and you feed him for a day..Teach a man how to fish and you feed him for life.
Reply
#5
Solved: 11 Years, 6 Months ago
Code:
Working with Files using FSO
FSO:
Filesystemobject is an object model which is used to handle the drives, folders, and files of a system or server.

♦  If an user needs to work on Driver, Folder, Files properties,methods or events then the first step he need to setup is
    filesystemobject

♦  File system object is an interface between QTP and the local system. using FSO we can create/delete folder,create/delete/read
    from/write to text files

♦ The FileSystemObject (FSO) object model allows you to use the familiar object.method syntax with a rich set of properties,
    methods, and events to process folders and files


Object/Collection Description:


FileSystemObject:

File system object is a Main object. Contains methods and properties that allow you to create, delete, gain information about, and generally manipulate drives, folders, and files. Many of the methods associated with this object duplicate those in other FSO objects; they are provided for convenience.

Drive:

Drive is a Object. Contains methods and properties that allow you to gather information about a drive attached to the system, such as its share name and how much room is available. Note that a "drive" isn't necessarily a hard disk, but can be a CD-ROM drive, a RAM disk, and so forth. A drive doesn't need to be physically attached to the system; it can be also be logically connected through a network.

Drives:

Drives are Collection. Provides a list of the drives attached to the system, either physically or logically. The Drives collection includes all drives, regardless of type. Removable-media drives need not have media inserted for them to appear in this collection.

File:

File is a Object. Contains methods and properties that allow you to create, delete, or move a file. Also allows you to query the system for a file name, path, and various other properties.

Files:

Files are Collection. Provides a list of all files contained within a folder.

Folder:

Folder is a Object. Contains methods and properties that allow you to create, delete, or move folders. Also allows you to query the system for folder names, paths, and various other properties.

Folders:

Folders are Collection. Provides a list of all the folders within a Folder.

TextStream:

TextStream is a Object. Allows you to read and write text files.

⇒Creating a FileSystemObject Object:

First, create a FileSystemObject object by using the CreateObject method.

The following code displays how to create an instance of the FileSystemObject:

Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")


Using the Appropriate Method
Second, use the appropriate method of the FileSystemObject object. For example, to create a new object, use either CreateTextFile

⇒Method: CreateTextFile

Description: Creates a specified file name and returns a TextStream object that can be used to read from or write to the file

Syntax: Set objfile = fso.CreateTextFile(filename[, overwrite[, Unicode]])

Arguments:
fso:     (Required) The name of a FileSystemObject object previously instantiated
filename:     (Required) A string expression that identifies the file to create
overwrite:     (Optional) Boolean value that indicates whether you can overwrite an existing file. The value is True if the file
can be overwritten, False if it can't be overwritten. If omitted, existing files are not overwritten (default False).
unicode:     (Optional) Boolean value that indicates whether the file is created as a Unicode or ASCII file. If the value is True, the file is created as a Unicode file. If the value is False, the file is created as an ASCII file. If omitted, an ASCII file is assumed.

Example:  

'Create a filesystemObject
Set fso=createobject("Scripting.FileSystemObject")
'Create a non existing file "qtptest.txt "  with overwrite option as True
Set qfile1=fso.CreateTextFile("C:\qtptest.txt",True,False)
'Output --> New File "qtptest.txt " is created

'Close the files
qfile1.Close
'Release the allocated objects
Set qfile1=nothing

Example:  

'Create a filesystemObject
Set fso=createobject("Scripting.FileSystemObject")
'Create a   file "qtptest.txt "  in C Drive .
'Then  run the below statement with overwrite option as False
'Output --> Error message "Fie already exists" is displayed
Set qfile2=fso.CreateTextFile("C:\qtpexist.txt",False,False)
Set fso=nothing

⇒Method: CopyFile

Description: Copies one or more files from one location to a new location

Syntax: fso.CopyFile (source, destination[, overwrite])

Arguments:
fso:     (Required) The name of a FileSystemObject object previously instantiated.
source:     (Required) A character string file specification, which can include wildcard characters, for one or more files to be copied.
destination:     (Required) Character string destination where the file or files from source are to be copied.Wildcard characters are not allowed in the destination string.
overwrite:     (Optional) Boolean value that indicates if existing files are to be overwritten. If True, files are overwritten; if False, they are not. The default is True. Note that CopyFile will fail if destination has the read-only attribute set, regardless of the value of overwrite.

Example:

Set fso=createobject("Scripting.FileSystemObject")

'File to be copied Sourcefile="C:\copy.txt"'Dest folder where the file has to be copied
Destination="D:\final1\"

'If the destination doesnot exist then create the destination folder

If fso.FolderExists(Destination) = false Then  
fso.CreateFolder (Destination)
End If  

'Copy the file

fso.CopyFile Sourcefile,Destination,True
Set fso=nothing

⇒Method: DeleteFile

Description: Deletes a specified file

Syntax: fso.DeleteFile (filename[, force])

Arguments:
fso:      (Required) The name of a FileSystemObject object previously instantiated
filename:     (Required) The name of the file to delete. The filename can contain wildcard characters in the last path component.
force:     (Optional) Boolean value that is True of files with the read-only attribute set are to be deleted;False if they are not. False is the default.

Example:  

Set fso=createobject("Scripting.FileSystemObject")
'File to be  deleted.  Sourcefile="C:\copy.txt"  'Delete the file
fso.DeleteFile Sourcefile

Set fso=nothing


⇒Method: CreateFolder

Description: Creates a new folder in the specified location

Syntax: fso.CreateFolder(foldername)

Arguments:
fso:     (Required) The name of a FileSystemObject object previously instantiated.
foldername:     (Required) A character string expression that identifies the folder to create.

Example:    

Set fso=createobject("Scripting.FileSystemObject")

'Folder to be created Foldername="D:\Folder_create"  
'If the folder doenot exst then create the folder
If fso.FolderExists(Foldername) = false Then

fso.CreateFolder (Foldername)

End If

Set fso=nothing

⇒Method: CopyFolder

Description: Copies a folder to a new location

Syntax: fso.CopyFolder (source, destination[, overwrite])

Arguments:
fso:     (Required) The name of a FileSystemObject object previously instantiated.
source:     (Required) A character string folder specification, which can include wildcard characters, for one or more folders to be copied. Wildcard characters can only be used in the last path component of the source argument.
destination:     (Required) Character string destination where the folder and subfolders from source are to be copied. Wildcard characters are not allowed in the destination string.
overwrite:     (Optional) Boolean value that indicates if existing folders are to be overwritten. If True, files are overwritten; if False, they are not. The default is True.

Example:  

Set fso=createobject("Scripting.FileSystemObject")
'Folder to be created SourcePath="D:\Folder_create" DestinationPath="D:\Destination\"  
'If the folder doesnot exst then create the folder

If fso.FolderExists(DestinationPath) = false Then

fso.CreateFolder (DestinationPath)
End If

fso.CopyFolder  SourcePath,DestinationPath,True
Set fso=nothing

⇒Method: MoveFolder

Description: Moves one or more folders from one location to another.

Syntax: fso.MoveFolder (source, destination)

Arguments:
fso:     (Required) The name of a FileSystemObject object previously instantiated.
source:     (Required) The path to the folder or folders to be moved. The source argument string can contain wildcard charactersin the last path component only.
destination:     (Required) The path where the folder or folders are to be moved. The destination argument can't contain wildcard characters.

Example:    

Set fso=createobject("Scripting.FileSystemObject")

'Folder to be created SourcePath="D:\Folder_move" DestinationPath="D:\Destination\"
'If the folder doesnot exst then create the folder

If fso.FolderExists(DestinationPath) = false Then

fso.CreateFolder (DestinationPath)
End If

fso.MoveFolder  SourcePath,DestinationPath

Set fso=nothing


⇒Method: DeleteFolder

Description: Deletes the specified folder and its contents

Syntax: fso.DeleteFolder (folderspec[, force])

Arguments:
fso:     (Required) The name of a FileSystemObject object previously instantiated
folderspec:     (Required) The name of the folder to delete. The folderspec can contain wildcard characters in the last path component.
force:     (Optional) Boolean value that is True of folders with the read-only attribute set are to be deleted;False if they are not. False is the default.

Example:  

Set fso=createobject("Scripting.FileSystemObject")
'Folder to be  deleted.  FolderDel="D:\final1"  'Delete the folder
fso.DeleteFolder(FolderDel)

Set fso=nothing

⇒Method: DriveExists

Description: Determines whether or not a specified drive exists

Syntax: fso.DriveExists (drivespec)

Arguments:
fso:     (Required) The name of a FileSystemObject object previously instantiated.
drivespec:     (Required) A drive letter or a complete path specification.

Example:  

Set fso=createobject("Scripting.FileSystemObject")
'The drive to check the existence
drivepath="D:\"

If fso.DriveExists(drivepath) then
msgbox  "Drive Exists"  Else    Msgbox "Drive doesnot Exist"

End If
Set fso=nothing

⇒Method: FileExists

Description: Determines whether or not a specified file exists

Syntax: fso.FileExists (filespec)

Arguments:
fso:     (Required) The name of a FileSystemObject object previously instantiated.
filespec:     (Required) The name of the file whose existence is to be determined. A complete path specification (either absolute or relative) must be provided if the file isn't expected to exist in the current folder.

Example:  

Set fso=createobject("Scripting.FileSystemObject")
'The file to check the existence
filepath="D:\qtptest.txt"

If fso.FileExists(filepath) then
msgbox  "File Exists"
Else
Msgbox "File doesnot Exist"
End If

Set fso=nothing

⇒Method: FolderExists

Description: Determines whether or not a specified folder exists

Syntax: fso.FolderExists (folderspec)

Arguments:
fso:     (Required) The name of a FileSystemObject object previously instantiated.
folderspec:     (Required) The name of the folder whose existence is to be determined. A complete path specification (either absolute or relative) must be provided if the folder isn't expected to exist in the current folder.

Example:  

Set fso=createobject("Scripting.FileSystemObject")
'The Folder to check the existence
folderpath="D:\qtp"

If fso.FolderExists(folderpath)  then
msgbox  "Folder Exists"
Else
Msgbox "Folder doesnot Exist"
End If

Set fso=nothing


⇒Method: GetDrive

Description: Returns a Drive object corresponding to the drive for a specified path

Syntax: objDrv = fso.GetDrive(drivespec)

Arguments:
fso:     (Required) The name of a FileSystemObject object previously instantiated.
drivespec:     (Required) The drivespec argument can be a drive letter (c), a drive letter with a colon appended (c:), a drive letter with a colon and path separator appended (c:\), or any network share specification (\\computer2\share1).

Example:

Set fso=createobject("Scripting.FileSystemObject")

'Drive for  getting details  Sourcefile="C:\"  Set get_drv=fso.GetDrive(Sourcefile)
'Some of the following details can be retrieved from a drive
msgbox  get_drv.AvailableSpace
Msgbox  get_drv.DriveLetter
msgbox  get_drv.DriveType
msgbox  get_drv.FileSystem
msgbox  get_drv.FreeSpace
msgbox  get_drv.Path
Msgbox  get_drv.RootFolder
Msgbox  get_drv.SerialNumber
Msgbox  get_drv.TotalSize

Set fso=nothing


⇒Method: GetFolder

Description: Returns a Folder object corresponding to the folder in a specified path

Syntax: objFolder = fso.GetFolder(folderSpec)

Arguments:
fso:     (Required) The name of a FileSystemObject object previously instantiated.
folderSpec:     (Required) The folderspec is the path (absolute or relative) to a specific folder.

Example:    

Set fso=createobject("Scripting.FileSystemObject")

'Folder  for  getting details  
Sourcefolder="D:\QTP"  
Set get_folder=fso.GetFolder(Sourcefolder)

'get the subfolders count in a folder
Set  get_subfolder=get_folder.SubFolders

For  each  sfile  in get_subfolder
'Get the name of each folder
Msgbox  sfile.name

Next

Set fso=nothing


⇒Method: GetFile

Description: Returns a File object corresponding to the file in the specified path. The file object methods and properties can be accessed. See File Object for the file object’s methods and properties.

Syntax: objFile = fso.GetFile(fileSpec)

Arguments:
fso:     (Required) The name of a FileSystemObject object previously instantiated.
fileSpec:     (Required) The filespec is the path (absolute or relative) to a specific file.


Example:    

Set fso=createobject("Scripting.FileSystemObject")

'File for  getting details  Sourcefile="D:\qtptest.txt"
Set get_file=fso.GetFile(Sourcefile)

'Some of  the following details can be retrieved from a file
msgbox  get_file.DateCreated
msgbox  get_file.DateLastAccessed
msgbox  get_file.DateLastModified
msgbox  get_file.ParentFolder
msgbox  get_file.Path

Set fso=nothing
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Disabled Webelement is showing up as Visible =True in GUI Spy Shwethareddy 3 3,902 11-07-2012, 02:45 PM
Last Post: Ankesh
  get all text showing on web page daniel_QTP 3 14,319 09-25-2012, 12:44 PM
Last Post: jesonvan

Forum Jump:


Users browsing this thread: 3 Guest(s)