Solved

VB Script to echo a list of file names to a text file is not working

Posted on 2014-04-03
10
672 Views
Last Modified: 2014-04-17
VB Script to echo a list of file names to a text file is not working. Running it results in a VBScript runtime error (screen shot attached). I need help making this work.

Here is my code:

cscript FileList.vbs > "\\Domain-01.com\DFS$\care-one\groups\CORPORATE\Regional Accounting\Monthly Reports\Region Accounting\2014\03 March 2014\NJ\AP\FileList.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "\\Domain-01.com\DFS$\care-one\groups\CORPORATE\Regional Accounting\Monthly Reports\Region Accounting\2014\03 March 2014\NJ\AP"

Set objFolder = objFSO.GetFolder(objStartFolder)

Set colFiles = objFolder.Files

Set objFile = objFSO.CreateTextFile(outFile,True)
For Each objFile in colFiles
Wscript.Echo objFile.Name
Next
VBS-error.png
0
Comment
Question by:janthonyn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 39976819
Any reason why you can't do it all within the script?

Set objFSO = CreateObject("Scripting.FileSystemObject")
outfile="filelist.txt"
objStartFolder = "\\Domain-01.com\DFS$\care-one\groups\CORPORATE\Regional Accounting\Monthly Reports\Region Accounting\2014\03 March 2014\NJ\AP"

Set objFolder = objFSO.GetFolder(objStartFolder)

Set colFiles = objFolder.Files

Set objFile = objFSO.CreateTextFile(outFile,True)
For Each objFile in colFiles
objFile.WriteLine objFile.Name
Next

Open in new window

0
 

Author Comment

by:janthonyn
ID: 39976998
sirbounty,

Thanks, but that didn't work. I ran the script and it returned another error for Line 11 Char 1. The error reads: Object doesn't support this property or method:'objFile.WriteLine' Code: 800A01B6 MSVBS runtime error

Any idea what could be substituted for the code in Line 11?
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 39977029
I think it's the duplicate usage of objFile....


Set objFile = objFSO.CreateTextFile(outFile,True)
For Each oFile in colFiles '<<<change this objFile to oFile
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:janthonyn
ID: 39977066
I made the change and got the same error. My code became the following:

Set objFSO = CreateObject("Scripting.FileSystemObject")
outfile="filelist.txt"
objStartFolder = "\\Domain-01.com\DFS$\care-one\groups\CORPORATE\Regional Accounting\Monthly Reports\Region Accounting\2014\03 March 2014\NJ\AP"

Set objFolder = objFSO.GetFolder(objStartFolder)

Set colFiles = objFolder.Files

Set objFile = objFSO.CreateTextFile(outFile,True)
For Each oFile in colFiles
objFile.WriteLine objFile.Name
Next
VBS-Error.png
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 39977737
Same situation, and my lack of pointing it out.
Your code is still referencing the wrong element, objFile.Name should be oFile.Name:

Set objFile = objFSO.CreateTextFile(outFile,True)
For Each oFile in colFiles
  objFile.WriteLine oFile.Name
Next

Open in new window

0
 

Author Comment

by:janthonyn
ID: 39991336
This is not working. I'm still getting an error. <<Object required: 'objFile' Code: 800A01AB, runtime error.

I'm testing it on my local drive now, outside of the workplace network. I've attached the code.
FileList.vbs
0
 

Author Comment

by:janthonyn
ID: 39994623
Maybe this is better done with a batch file?
0
 
LVL 67

Accepted Solution

by:
sirbounty earned 500 total points
ID: 39994900
You took objfile back out though?  That's why the error...

Set objFSO = CreateObject("Scripting.FileSystemObject")
outfile="filelist.txt"
objStartFolder = "C:\Users\Nels Anthony\Documents\Files & Folders\Desktop Folder\VBScript"

Set objFolder = objFSO.GetFolder(objStartFolder)
Set colFiles = objFolder.Files
Set objFile = objFSO.CreateTextFile(outFile,True)  '<<< objFile is the output file

For Each oFile in colFiles  '<<< oFile is the object reference for each iteration of a file in colFiles
objFile.WriteLine oFile.Name '<<<Need both here - you're writing output to objFile, but the value of what you're writing is a property (.Name) of oFile
Next
0
 

Author Closing Comment

by:janthonyn
ID: 40006304
sirbounty

You have restored my faith in experts-exchange. Thank you very much!
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 40006462
I know it can be frustrating, but it's a back-and-forth exchange.  Were we seated together at a PC, this could have been sorted in about 5 minutes.  But since I can't see exactly what's happening on your end, it takes a bit longer.
So glad it's sorted out now though.
Thanks for the grade! :^)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Problem to line 23 74
collection output issue 9 64
how to read the text file and convert it to csv. 5 36
Problem to copy file 14 51
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question