Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


FileExists with Wildcards

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,3056
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> FileExists with Wildcards
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 FileExists with Wildcards - 5/19/2005 1:50:54 AM   
  KevinAvalon

 

Posts: 3
Score: 0
Joined: 5/19/2005
From: USA
Status: offline
Greetings. I am new to the forum, but have found it quite useful so far.

Currently I am attempting to write a script to verify the existance of a file and then copy it. The copy part works, but the FileExists does not.

The variables are declared and the code works if not in the IF-Then loop.

Here is the code excerpt:

ArchiveFile = "\\epsilon\archives\" & User & "\*.pst"
If (FSO.FileExists(ArchiveFile)) Then
SourcePath = ArchiveFile
Target = "C:\Outlook Archive\"
FSO.CopyFile SourcePath, Target, True
MsgBox "The archive file was copied."
End If

_____________________________

Thanks.

Kevin
 
 
Post #: 1
 
 Re: FileExists with Wildcards - 5/19/2005 6:14:54 AM   
  marcusrp

 

Posts: 145
Score: 0
Joined: 4/19/2005
From:
Status: offline
some more clarification would help...you say the copy works, but not the fileexists. But then you say the code works if its not in the if-then loop...these two statements contradict each other somewhat. are you saying that both the copy and fileexists parts work when they are not in the loop statement, but only the copy part works inside the loop?

(in reply to KevinAvalon)
 
 
Post #: 2
 
 Re: FileExists with Wildcards - 5/19/2005 6:16:19 AM   
  marcusrp

 

Posts: 145
Score: 0
Joined: 4/19/2005
From:
Status: offline
also, how could the copy work if the fileexist doesn't? you cant copy a file that cant be found...

(in reply to KevinAvalon)
 
 
Post #: 3
 
 Re: FileExists with Wildcards - 5/19/2005 6:42:30 AM   
  KevinAvalon

 

Posts: 3
Score: 0
Joined: 5/19/2005
From: USA
Status: offline
Clarification of problem:

The code functions fine if left as follows:

ArchiveFile = "\\epsilon\archives\" & User & "\*.pst"
SourcePath = ArchiveFile
Target = "C:\Outlook Archive\"
FSO.CopyFile SourcePath, Target, True
MsgBox "The archive file was copied."

The file or files are copied just fine.

However, when inserted into the If-then with the .fileexists check, there is no indication that they function. No files are copied and no msgbox is displayed.

My guess is that the .fileexists method will not function correctly with the wildcard as a part of the path.

My goal is to check to see if there are .pst files in the user's directory and if they are present to copy them. If there are none, do nothing. I don't want to copy all files in the directory, only those with a .pst file extension. The msgbox is just there for my debugging and will be removed in the final product.

I hope this helps.

Kevin

(in reply to KevinAvalon)
 
 
Post #: 4
 
 Re: FileExists with Wildcards - 5/19/2005 7:24:18 AM   
  marcusrp

 

Posts: 145
Score: 0
Joined: 4/19/2005
From:
Status: offline
right, yeah the fileexist method is looking for a file called "*.pst" (string literal). fileexists is usually used for a single file spec. thats a good question, I figure you have to have a loop that checks each file in turn and ends when the folder is searched. give me a few (at work now), otherwise someone else may give you a tip.

(in reply to KevinAvalon)
 
 
Post #: 5
 
 Re: FileExists with Wildcards - 5/19/2005 8:18:41 AM   
  marcusrp

 

Posts: 145
Score: 0
Joined: 4/19/2005
From:
Status: offline
Try This:

Dim objFSO
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")

'Get the folder object associated with the directory
Dim objFolder
Set objFolder = objFSO.GetFolder("C:\InetPub\wwwroot")

Response.Write "The files found in " & objFolder.Name & ":<br>"

'Loop through the Files collection
Dim objFile
For Each objFile in objFolder.Files
if right(len(objFile),3) = "pst" then
'run your copy function'
end if
Next

'Clean up!
Set objFolder = Nothing
Set objFile = Nothing
Set objFSO = Nothing

this should return the number of characters in the filename, grab the last three chars (the extension), and if it ends in "pst" it will run your copy code. You may have to make extensions visible on your test system. I threw this together just now, so don't know if it will work as advertised! but it should get you going in the right direction. (make sure you substitute your copy code, I was being lazy.)

(in reply to KevinAvalon)
 
 
Post #: 6
 
 Re: FileExists with Wildcards - 5/19/2005 8:48:14 AM   
  KevinAvalon

 

Posts: 3
Score: 0
Joined: 5/19/2005
From: USA
Status: offline
I tried this code, but had the same results....that is to say, nothing was copied.
File extensions are visible on the source directory.

Is there an API call that will do this?

Kevin


quote:
Originally posted by marcusrp

Try This:

Dim objFSO
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")

'Get the folder object associated with the directory
Dim objFolder
Set objFolder = objFSO.GetFolder("C:\InetPub\wwwroot")

Response.Write "The files found in " & objFolder.Name & ":<br>"

'Loop through the Files collection
Dim objFile
For Each objFile in objFolder.Files
if right(len(objFile),3) = "pst" then
'run your copy function'
end if
Next

'Clean up!
Set objFolder = Nothing
Set objFile = Nothing
Set objFSO = Nothing

this should return the number of characters in the filename, grab the last three chars (the extension), and if it ends in "pst" it will run your copy code. You may have to make extensions visible on your test system. I threw this together just now, so don't know if it will work as advertised! but it should get you going in the right direction. (make sure you substitute your copy code, I was being lazy.)


(in reply to KevinAvalon)
 
 
Post #: 7
 
 Re: FileExists with Wildcards - 5/19/2005 8:55:22 AM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
Generally, vbs doesn't use the traiditional "API" that is associated with VB. VBS and VB are completely different things, one is interpreted and the other is compiled. The FILEEXISTS method of the FSO object doesn't support wildcards; therefore, you will need numerate though a list of files found with the directory and check the extension of those files found in it individually.

(in reply to KevinAvalon)
 
 
Post #: 8
 
 Re: FileExists with Wildcards - 5/19/2005 10:28:54 AM   
  marcusrp

 

Posts: 145
Score: 0
Joined: 4/19/2005
From:
Status: offline
since the code snippet I gave you wasnt totally complete, why don't you post exactly what you did use? there may be a syntax error or something

(in reply to KevinAvalon)
 
 
Post #: 9
 
 Re: FileExists with Wildcards - 5/20/2005 1:07:26 AM   
  Country73


Posts: 733
Score: 10
Joined: 8/25/2004
From: USA
Status: offline
This should help you out:

ArchiveFolder = "\\epsilon\archives\" & User
If FSO.FolderExists(ArchiveFolder)Then
Set g = FSO.GetFolder(ArchiveFolder)
Set gg = g.Files
For Each g1 in gg
If Right(g1,3) = "pst" Then
'Copy File if found
wscript.echo g1
End If
Next
End If

(in reply to KevinAvalon)
 
 
Post #: 10
 
 Re: FileExists with Wildcards - 5/20/2005 10:11:15 PM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
I found it od for someone who wanted to use windows API to do something and yet couldn't see the simple fact that fileexists method doesn't support wildcards :)

(in reply to KevinAvalon)
 
 
Post #: 11
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> FileExists with Wildcards Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts