Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Getting files from a folder

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

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Getting files from a folder
  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 >>
 Getting files from a folder - 5/7/2008 3:58:37 AM   
  trkghost

 

Posts: 20
Score: 0
Joined: 3/18/2008
Status: offline
Hello,
I have used the search and looked into the Frequenly asked questions to see if i can find the answer to this but I need help on what im missing...I am trying to create a script that loops through the files in a set folder.  I found 1 post that was close to what I am asking about but not fully.  I cannot figure out how to get it to return which filename it is looking at at the moment and how to do the loop.  I have this so far...


      

I am just lost on how to begin the loop to go through each file and return which file it is on.  All I am looking for is a little direction to get this finished.  So any help would be great.

Thank you.
 
 
Post #: 1
 
 RE: Getting files from a folder - 5/7/2008 4:13:16 AM   
  Country73


Posts: 732
Score: 10
Joined: 8/25/2004
From: USA
Status: offline
Change:
   SET objFolder = objFSO.GetFolder("C:\Test\")
To just
   SET objFolder = objFSO.GetFolder("C:\Test")

Then just add this to the end of what you have shown:

FOR EACH objFiles1 IN objFiles
   wscript.echo objFiles1.Name
NEXT

(in reply to trkghost)
 
 
Post #: 2
 
 RE: Getting files from a folder - 5/7/2008 4:18:19 AM   
  trkghost

 

Posts: 20
Score: 0
Joined: 3/18/2008
Status: offline
Thanks Country,
That works great.  And gives me a good direction on where to go from there for this.

(in reply to Country73)
 
 
Post #: 3
 
 RE: Getting files from a folder - 5/7/2008 4:43:07 AM   
  trkghost

 

Posts: 20
Score: 0
Joined: 3/18/2008
Status: offline
Country,
I am getting an error when I added it to my code.


      

The error I am getting is: Unexpected 'Next"...Where am I going wrong in this?

(in reply to Country73)
 
 
Post #: 4
 
 RE: Getting files from a folder - 5/7/2008 6:04:50 AM   
  ebgreen


Posts: 4972
Score: 31
Joined: 7/12/2005
Status: offline
Change Else If to ElseIf.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to trkghost)
 
 
Post #: 5
 
 RE: Getting files from a folder - 5/7/2008 6:12:21 AM   
  trkghost

 

Posts: 20
Score: 0
Joined: 3/18/2008
Status: offline
ebGreen,
Wow i feel stupid now.  Small oversite.  Thanks for the help!

(in reply to ebgreen)
 
 
Post #: 6
 
 RE: Getting files from a folder - 5/7/2008 7:29:07 AM   
  trkghost

 

Posts: 20
Score: 0
Joined: 3/18/2008
Status: offline
EB,
can you tell me why this is erroring?  I know that there is prob an easier way of doing this but here is my code...

      

Kind of have it doing things with 4 seperate files.  But the error I am getting is Object Required: " and its at reference Line: 213 Char: 4

I dont see anything wrong there.  Any help would be great.  Thanks

(in reply to ebgreen)
 
 
Post #: 7
 
 RE: Getting files from a folder - 5/7/2008 7:57:25 AM   
  ebgreen


Posts: 4972
Score: 31
Joined: 7/12/2005
Status: offline
Here is my guess. Look at the code where you make nFile:

  If PlcHlder = "Test1" Then
      FlOpt = "1"
      Set nFile = nFSO.CreateTextFile("C:\test\Summary " & PlcHlder & Ext, True)
  ElseIf PlcHlder = "Test2" Then
      FlOpt = "2"
      Set nFile = nFSO.CreateTextFile("C:\test\Summary " & PlcHlder & Ext, True)
  ElseIf PlcHlder = "Test3" Then
      FlOpt = "3"
      Set nFile = nFSO.CreateTextFile("C:\test\Summary " & PlcHlder & Ext, True)
  ElseIf PlcHlder = "Test4" Then   
      FlOpt = "4"
      Set nFile = nFSO.CreateTextFile("C:\test\Summary " & PlcHlder & Ext, True)
  End If


What would happen if PlcHolder did not meet any of these requirements? What would nFile be? If that were the case, what would happen when execution hits this line?

nFile.WriteLine("This file is correct")

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to trkghost)
 
 
Post #: 8
 
 RE: Getting files from a folder - 5/7/2008 8:06:43 AM   
  trkghost

 

Posts: 20
Score: 0
Joined: 3/18/2008
Status: offline
Found the answer...added this to the code...

      
This fixed the issue.  Thanks for all the help guys.

< Message edited by trkghost -- 5/7/2008 8:12:20 AM >

(in reply to ebgreen)
 
 
Post #: 9
 
 RE: Getting files from a folder - 5/7/2008 8:26:57 AM   
  ebgreen


Posts: 4972
Score: 31
Joined: 7/12/2005
Status: offline
Ok, so I'm going to go through and just sort of stream of consciousness make some comments about what you have so far:

You only need one FileSystemObject, not 3

Use the .Name property of the file object explicitly, so this:
   PlcHlder = Mid(objFiles1, 9, 5
becomes this:
   PlcHlder = Mid(objFiles1.Name, 9, 5

The naming of nFile has nothing to do with the If-Then, so take it out. This:
  If PlcHlder = "Test1" Then
      FlOpt = "1"
      Set nFile = nFSO.CreateTextFile("C:\test\Summary " & PlcHlder & Ext, True)
  ElseIf PlcHlder = "Test2" Then
      FlOpt = "2"
      Set nFile = nFSO.CreateTextFile("C:\test\Summary " & PlcHlder & Ext, True)
  ElseIf PlcHlder = "Test3" Then
      FlOpt = "3"
      Set nFile = nFSO.CreateTextFile("C:\test\Summary " & PlcHlder & Ext, True)
  ElseIf PlcHlder = "Test4" Then   
      FlOpt = "4"
      Set nFile = nFSO.CreateTextFile("C:\test\Summary " & PlcHlder & Ext, True)
  End If
becomes this:
  If PlcHlder = "Test1" Then
      FlOpt = "1"
  ElseIf PlcHlder = "Test2" Then
      FlOpt = "2"
  ElseIf PlcHlder = "Test3" Then
      FlOpt = "3"
  ElseIf PlcHlder = "Test4" Then   
      FlOpt = "4"
  End If
  Set nFile = nFSO.CreateTextFile("C:\test\Summary " & PlcHlder & Ext, True)

Now you can see that an if then is really kind of silly for this logic and a Select-Case would be better. So it becomes:
   Select Case PlcHlder
       Case "Test1"
           FlOpt = "1"
       Case "Test2"
           FlOpt = "2"
       Case "Test3"
           FlOpt = "3"
       Case "Test4"
           FlOpt = "4"
   End Select

But wait..If you look closely, there is an easily defined pattern here. FlOpt is always the same as the last character of PlcHlder. So this would work:
   FlOpt = Right(PlcHlder, 1)
So we can replace all of this:
  If PlcHlder = "Test1" Then
      FlOpt = "1"
      Set nFile = nFSO.CreateTextFile("C:\test\Summary " & PlcHlder & Ext, True)
  ElseIf PlcHlder = "Test2" Then
      FlOpt = "2"
      Set nFile = nFSO.CreateTextFile("C:\test\Summary " & PlcHlder & Ext, True)
  ElseIf PlcHlder = "Test3" Then
      FlOpt = "3"
      Set nFile = nFSO.CreateTextFile("C:\test\Summary " & PlcHlder & Ext, True)
  ElseIf PlcHlder = "Test4" Then   
      FlOpt = "4"
      Set nFile = nFSO.CreateTextFile("C:\test\Summary " & PlcHlder & Ext, True)
  End If
with this:
   FlOpt = Right(PlcHlder, 1)




That's all I see right now. I don't think I addressed your specific question, but I'm not sure I understand it anyway. Could you explain in more detail what problem you are currently having?

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to trkghost)
 
 
Post #: 10
 
 RE: Getting files from a folder - 5/7/2008 8:47:08 AM   
  trkghost

 

Posts: 20
Score: 0
Joined: 3/18/2008
Status: offline
Eb,
Sweet, I appriciate the critque of the Script.  This is only the 2nd one I have built so suggestions and critques are great.  I had fixed the issue I was having and edited the post to show what I did to fix it.  I placed Set nFile = nFSO.CreateTextFile("C:\test\Summary " & PlcHlder & Ext, True) in an If statement due to the fact that other files that this script is not doing anything with wont cause a problem.  If i take it out of the If or case statement would it cause a problem when it hits a file i am not really doing anything with?

(in reply to ebgreen)
 
 
Post #: 11
 
 RE: Getting files from a folder - 5/7/2008 11:52:50 PM   
  ebgreen


Posts: 4972
Score: 31
Joined: 7/12/2005
Status: offline
Ehhh...I think you would just wind up making a blank file so I'd put the creation into an If-Then.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to trkghost)
 
 
Post #: 12
 
 
 
  

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 >> Getting files from a folder 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