Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Searching a folder to see if a file exsists

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

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Searching a folder to see if a file exsists
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1] 2   next >   >>
Login
Message << Older Topic   Newer Topic >>
 Searching a folder to see if a file exsists - 10/13/2005 6:11:45 AM   
  mnman66

 

Posts: 49
Score: 0
Joined: 3/16/2005
From:
Status: offline
Hello guys! Long time no post. Something like 6 months I think. Well, I'm back again for a little bit of help. I'm trying to create a process that searches a specific folder for a number of specific files. Once one of those files are found, I'm running another script to move them with 3rd party software. Here is what I have at this point:

Set fso = CreateObject("Scripting.FileSystemObject")
srcDir = "C:\Temp\outgoing"
On Error Resume Next
If fso.FolderExists(srcDir) Then
For Each file In fso.GetFolder(srcDir).Files
If UCase(Left(fso.GetBaseName(file),4)) = "AGBN" Then
Set ws = WScript.CreateObject("WScript.Shell")
ws.Run("C:\Temp\AGBN\agbnvs.vbs",0,"True")
ElseIf UCase(Left(fso.GetBaseName(file),4)) = "AGTT" Then
ws.Run("C:\Temp\AGBN\agttvs.vbs",0,"True")
ElseIf UCase(Left(fso.GetBaseName(file),4)) = "AGUS" Then
ws.Run("C:\Temp\AGUS\agusvs.vbs",0,"True")
ElseIf UCase(Left(fso.GetBaseName(file),4)) = "AGWF" Then
ws.Run("C:\Temp\AGWF\agwfvs.vbs",0,"True")
ElseIf UCase(Left(fso.GetBaseName(file),4)) = "BLRJ" Then
ws.Run("C:\Temp\blrjvs.vbs",0,"True")
ElseIf UCase(Left(fso.GetBaseName(file),4)) = "CCRJ" Then
ws.Run("C:\Temp\CCRJ\ccrjvs.vbs",0,"True")

End If
 
 
Post #: 1
 
 RE: Searching a folder to see if a file exsists - 10/13/2005 8:18:42 AM   
  Fredledingue


Posts: 383
Score: 0
Joined: 5/9/2005
From:
Status: offline
Remove On Error Resume Next.
Good scripts don't need that and it will help you find the errors.

_____________________________

Fred

(in reply to mnman66)
 
 
Post #: 2
 
 RE: Searching a folder to see if a file exsists - 10/13/2005 9:29:35 AM   
  Cybex


Posts: 412
Score: 0
Joined: 9/14/2005
From: Florida
Status: offline
Perhaps this is not all of your code, it appears you are missing an "End If" and a "Next".


      


Cybex

(in reply to mnman66)
 
 
Post #: 3
 
 RE: Searching a folder to see if a file exsists - 10/13/2005 9:44:59 AM   
  Cybex


Posts: 412
Score: 0
Joined: 9/14/2005
From: Florida
Status: offline
This is totally untested as I am not 100% sure what you are trying to do nor am I sure I could even replectate it if I did.

But this may be another approach to look at...

UNTESTED

      

It seems to be a less repetitious approach.

Cybex

(in reply to mnman66)
 
 
Post #: 4
 
 RE: Searching a folder to see if a file exsists - 10/13/2005 7:02:27 PM   
  ginolard


Posts: 1068
Score: 21
Joined: 8/10/2005
Status: offline
As a general rule, if you need to use more than 2 If/Then statements you're better of using Select/Case

(in reply to Cybex)
 
 
Post #: 5
 
 RE: Searching a folder to see if a file exsists - 10/13/2005 9:15:45 PM   
  Snipah


Posts: 1343
Score: 6
Joined: 11/1/2004
From: Netherlands
Status: offline
Ginolard is right, it makes it neater aswell.

I do like Cybex' approach, only, if the VBS file is not in the respective folder...then you have a misser...

See below for the Select approach:


      

_____________________________

For more information, please see the "Read me First" topic.

http://www.visualbasicscript.com

(in reply to ginolard)
 
 
Post #: 6
 
 RE: Searching a folder to see if a file exsists - 10/14/2005 1:27:16 AM   
  Cybex


Posts: 412
Score: 0
Joined: 9/14/2005
From: Florida
Status: offline
Snipah.

Thank you and I see your point but I would think that the location of the scripts is the one thing that he would have the most control over and they would always be where the coder placed them.  I am not sure how one would include handling for such an instance without recursively searching all directories for  gfName & "vs.vbs".  I did put in a way to handle a situation where a gfName did not have a corresponding script located in the script directory.



Mnman66
I think that when using "ElseIf" your last condition is made using "Else".


Cybex

(in reply to Snipah)
 
 
Post #: 7
 
 RE: Searching a folder to see if a file exsists - 10/14/2005 1:32:33 AM   
  mnman66

 

Posts: 49
Score: 0
Joined: 3/16/2005
From:
Status: offline
Thanks for all of the help! I'll be trying those out today. Here is the actual scenario: Users will be putting files to a shared folder represented in the scrDir(C:\Temp\outgoing). They have certain criteria that they have to use, mainly a certain number of naming conventions to use. When they create a file, they have to use one of the names I've given:
AGBN.txt
AGTT.txt
AGUS.txt
AGWF.txt
BLRJ.txt
CCRJ.txt
Each of these represent a certain process on another server. With THIS process I need help with, I need to search the "outgoing" folder for each of these files. Once a file is there, it's to be sent to another server via a 3rd party software, hence the other .vbs scripts that need to be run. I would put it in a loop as well, but that's going to take up too much of the systems resources from other processes already running. It will most likely be set to run every 10 or 15 minutes. I'll let everyone know how the previous examples work out. Thanks again!

(in reply to Snipah)
 
 
Post #: 8
 
 RE: Searching a folder to see if a file exsists - 10/14/2005 1:41:03 AM   
  Cybex


Posts: 412
Score: 0
Joined: 9/14/2005
From: Florida
Status: offline
What is this 3rd party software and why do you have to use it to send or copy the file to another server?

Cybex

(in reply to mnman66)
 
 
Post #: 9
 
 RE: Searching a folder to see if a file exsists - 10/14/2005 1:45:02 AM   
  mnman66

 

Posts: 49
Score: 0
Joined: 3/16/2005
From:
Status: offline
The 3rd party software is called SecureFX by VanDyke software. We have to use it's SFTP functions to send the files via SSH2. The other VBS entails the function to send the files using the SecureFX, then moves it to an archive directory and adding a date stamp to the end of the file name. I've just been trying to come up with a way to search for the files given in the folder given. I'm getting the hang of it, but still come up against a wall on some functions. Glad you guys are here, and this site is active!


(in reply to Cybex)
 
 
Post #: 10
 
 RE: Searching a folder to see if a file exsists - 10/14/2005 4:50:11 AM   
  mnman66

 

Posts: 49
Score: 0
Joined: 3/16/2005
From:
Status: offline
Well, after trying Snipah's bit of code, I'm getting a "Cannot use parentheses when calling a Sub" error starting at line 10/character 57.  I was getting that with my first code, but didn't know how to get around it. Like I said, still learning.

(in reply to mnman66)
 
 
Post #: 11
 
 RE: Searching a folder to see if a file exsists - 10/14/2005 4:58:27 AM   
  Cybex


Posts: 412
Score: 0
Joined: 9/14/2005
From: Florida
Status: offline
Did the code I suggested work?

Also here is another approch along the same lines as before.
This one stays running and watches the directory every 60 seconds for new files.  After it detects a new file it runs the second script with the correct syntax passed.  It does this for every new file created until you end the script.  To end the script and stop it watching the directory you can go to "Task Manager" and stop the "cscript.exe" process that is running.


      

*****EDIT*****

You could add in the ability to do the same actions after a file has been modified by adding a Case "__InstanceModificationEvent".

*****EDIT2*****
Why don't you input the 2nd code into this one?  That would get rid if the WS.Run problem.

Cybex

< Message edited by Cybex -- 10/14/2005 5:05:47 AM >

(in reply to mnman66)
 
 
Post #: 12
 
 RE: Searching a folder to see if a file exsists - 10/14/2005 5:09:21 AM   
  Snipah


Posts: 1343
Score: 6
Joined: 11/1/2004
From: Netherlands
Status: offline
Mnman66,

try this....:

ws.Run "C:\Temp\AGBN\agbnvs.vbs", 0, True


-lol-

_____________________________

For more information, please see the "Read me First" topic.

http://www.visualbasicscript.com

(in reply to mnman66)
 
 
Post #: 13
 
 RE: Searching a folder to see if a file exsists - 10/19/2005 2:29:57 AM   
  mnman66

 

Posts: 49
Score: 0
Joined: 3/16/2005
From:
Status: offline
Sorry for not getting back sooner. Cybex, I tried the code that you last posted, and it did run and create a service, but didn't move any of the files. No errors either. Next, all of the previous posts gave me basically the same error: "Cannot use parentheses when calling a Sub" . Even when I take them out...lol...with what Snipah recommended. It then failed to recognize the folder where the scripts are. I'm going to be trying a few different things, such as putting all of the other scripts into 1 folder as opposed to each individual folder.  Or maybe If I delare all of the folder names and locations of the scripts? Not sure, but I'll be trying. I'll post more when I find more out. Again...thanks for the help all!

K

(in reply to Snipah)
 
 
Post #: 14
 
 RE: Searching a folder to see if a file exsists - 10/19/2005 5:16:50 AM   
  Snipah


Posts: 1343
Score: 6
Joined: 11/1/2004
From: Netherlands
Status: offline
can you post everything you have so far? This'll help us to get a better understanding of the whole...

S

_____________________________

For more information, please see the "Read me First" topic.

http://www.visualbasicscript.com

(in reply to mnman66)
 
 
Post #: 15
 
 RE: Searching a folder to see if a file exsists - 10/19/2005 6:33:37 AM   
  Cybex


Posts: 412
Score: 0
Joined: 9/14/2005
From: Florida
Status: offline
I agree with Snipah.  Please post everything you have so far so we can see what is going on.

Cybex

(in reply to Snipah)
 
 
Post #: 16
 
 RE: Searching a folder to see if a file exsists - 10/19/2005 6:35:57 AM   
  mnman66

 

Posts: 49
Score: 0
Joined: 3/16/2005
From:
Status: offline
Okay, here's what I've done so far:

Set fso = CreateObject("Scripting.FileSystemObject")
srcDir = "C:\RCCTS\data\outgoing"
scrAgbn = "C:\Temp\AGBN\agbnvs.vbs"
scrAgtt = "C:\Temp\AGTT\agttvs.vbs"
scrAgus = "C:\Temp\AGUS\agusvs.vbs"
scrAgwf = "C:\Temp\AGWF\agwfvs.vbs"
scrBlrj = "C:\Temp\BLRJ\blrjvs.vbs"
scrCcrj = "C:\Temp\CCRJ\ccrjvs.vbs"

If fso.FolderExists(srcDir) Then
    For Each file In fso.GetFolder(srcDir).Files
       
If UCase(Left(fso.GetBaseName(file),4)) = "AGBN" Then
Set ws = WScript.CreateObject("WScript.Shell")
            ws.Run(scrAgbn)
       
ElseIf UCase(Left(fso.GetBaseName(file),4)) = "AGTT" Then
            ws.Run(scrAgtt)
       
ElseIf UCase(Left(fso.GetBaseName(file),4)) = "AGUS" Then
            ws.Run(scrAgus)
       
ElseIf UCase(Left(fso.GetBaseName(file),4)) = "AGWF" Then
            ws.Run(scrAgwf)
       
ElseIf UCase(Left(fso.GetBaseName(file),4)) = "BLRJ" Then
            ws.Run(scrBlrj)
       
ElseIf UCase(Left(fso.GetBaseName(file),4)) = "CCRJ" Then
            ws.Run(scrCcrj)
       
End If
    Next
End If

I've put a file in the source folder named AGUS.txt, so if it all works, the agusvs.vbs should kick off and send that file to it's destination. However when I run this I get the following error:
Code: 800A01A8
Error: Object required: 'ws'
Did I not Set something correctely?
I'll google and see if I can find anything as well.
Thanks again for the help.

(in reply to Snipah)
 
 
Post #: 17
 
 RE: Searching a folder to see if a file exsists - 10/19/2005 7:32:10 AM   
  Snipah


Posts: 1343
Score: 6
Joined: 11/1/2004
From: Netherlands
Status: offline
Hiya MnMan66,

Look here:
[..]
scrCcrj = "C:\Temp\CCRJ\ccrjvs.vbs"

'very first If
If fso.FolderExists(srcDir) Then
For Each file In fso.GetFolder(srcDir).Files

'first If
If UCase(Left(fso.GetBaseName(file),4)) = "AGBN" Then
Set ws = WScript.CreateObject("WScript.Shell")
         ws.Run(scrAgbn)
    
ElseIf UCase(Left(fso.GetBaseName(file),4)) = "AGTT" Then
         ws.Run(scrAgtt)
[..]


If the first If was False Then it would bounce to the next ElseIf, and that is where you didn't put your "Set ws..." hence the error...

Put the "Set ws..." before the very first If


< Message edited by Snipah -- 10/19/2005 7:37:12 AM >


_____________________________

For more information, please see the "Read me First" topic.

http://www.visualbasicscript.com

(in reply to mnman66)
 
 
Post #: 18
 
 RE: Searching a folder to see if a file exsists - 10/19/2005 11:13:05 AM   
  Cybex


Posts: 412
Score: 0
Joined: 9/14/2005
From: Florida
Status: offline
mnman66,

Try this one I missed some of your logical conditions last time and needed to call a few things differently.  I tested it locally and it worked fine, I only created one test vbs "C:\temp\AGBN\agbnvs.vbs" but it triggered even when I moved it to the second array position.

Seeing you code made things clearer.

Tested and works here!
EDITED*** Remembered a better (Shorter) way to declare the array.  Got rid of six lines of code. Tested, still works.

      

Let me know the status.

Cybex

< Message edited by Cybex -- 10/19/2005 1:22:11 PM >

(in reply to mnman66)
 
 
Post #: 19
 
 RE: Searching a folder to see if a file exsists - 10/27/2005 6:24:48 AM   
  mnman66

 

Posts: 49
Score: 0
Joined: 3/16/2005
From:
Status: offline
Okay, I've gotten around to trying this latest post from Cybex. Unfortunately, I'm still getting an error, and from what I've found, it's still in the syntax. The actual WSH error is the following:

Script: C:\Temp\AGUS\AGUSvs.vbs
Line:31
Char:1
Error: File not found
Code: 800A0035
Source: Microsoft VBScript runtime error

As far as I can tell, it's getting stuck when trying to call the other vbs script to transfer the file. I'm going to include the actual transfer command within your script and see how it goes. I'll include it on my next post.
Thanks everyone! I'll keep you posted!

(in reply to Cybex)
 
 
Post #: 20
 
 
Page:   [1] 2   next >   >>
 
  

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 >> Searching a folder to see if a file exsists Page: [1] 2   next >   >>
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