Solved

Input past end of file vbs script

Posted on 2016-08-27
9
82 Views
Last Modified: 2016-08-28
Hi Everyone,
I am new to vbscript
Trying for hours to fix the script.
Basically what I am trying to do is
1) To get the filenames from the folder specified
2) Use the filenames from the folder and compare with the names listed in a .txt file and write the missing filename that is not present to the log file.(output.txt)
This is what i have done.

Set objFSO = CreateObject("Scripting.FileSystemObject") 
Const forReading = 1 
logName = "Output.txt"
Set logFile = objFSO.CreateTextFile(logName, True)

    
inputFile = "a.txt"                                                    
                                                    
                         
strFolder1 = InputBox("Enter Source Folder Path :")
Set fileList = objFSO.OpenTextFile(inputFile, forReading)                  
Set objFolder1 = objFSO.GetFolder(strFolder1)

Set files = objFolder1.Files
  

For each objFile in files
 
fileName = objFile.Name

             fileSpec = fileList.ReadLine()  
                          
    If not (fileName = fileSpec) Then                                            
	logFile.writeline (fileName)                                          
    End If 
      
      next
fileList.close                                                               
logFile.close 

Open in new window

Appreciate your help.
Thanks
0
Comment
Question by:Adrian Raj
  • 4
  • 4
9 Comments
 
LVL 23

Expert Comment

by:NVIT
ID: 41773483
Is vbs required? If not, I can give you a bat/cmd solution instead.
1
 
LVL 34

Assisted Solution

by:Dan Craciun
Dan Craciun earned 500 total points
ID: 41773498
You're assuming the order of the file listing will be exactly as that from your file list. This means your script will work only while those are identical.

Read the entire text file (not just a line) into a string, then test if 'filename' is present in that string.

HTH,
Dan
1
 

Author Comment

by:Adrian Raj
ID: 41773626
@ nvit
Thanka for replying.Yes vbs is required because I have already done the vice versa of the script where file listing is compared against the filename in the folder and return the missing filename from the file list, so m trying to complete this script and combine both. Sort of a vlookup via script. Thanks for the help
0
 

Author Comment

by:Adrian Raj
ID: 41773629
@dan
Thanks for replying. Ok I will try to google how to pass the file into a string. Thanks for the suggestion. Will let you know if it works
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:Adrian Raj
ID: 41773643
I have pass the file into a string and below is my code, however I am only getting the last filename in the folder specified that does not match the filelist. I know I am close, just need a little bit tweaking. Please help.

Set objFSO = CreateObject("Scripting.FileSystemObject")
Const forReading = 1
logName = "Output.txt"
Set logFile = objFSO.CreateTextFile(logName, True)

   
inputFile = "a.txt"                                                    
                                                   
                         
strFolder1 = InputBox("Enter Source Folder Path :")
Set fileList = objFSO.OpenTextFile(inputFile, forReading)
strText = fileList.ReadAll  
fileList.close
arrFileList = Split(strText, vbCrLf)
             
Set objFolder1 = objFSO.GetFolder(strFolder1)

Set files = objFolder1.Files
 

For each objFile in files
      

      fileName = objFile.Name
 

      
Next
      For each strFile in arrFileList
                   
                If not (fileName = strFile) Then                                        
            logFile.writeline (fileName)                                          
                End If
     

Next
                                                             
logFile.close
0
 
LVL 34

Accepted Solution

by:
Dan Craciun earned 500 total points
ID: 41773900
Change this:
For each objFile in files
      

      fileName = objFile.Name
  

      
Next
      For each strFile in arrFileList
                   
                If not (fileName = strFile) Then                                         
            logFile.writeline (fileName)                                          
                End If 
      

Next

Open in new window

to this
For each objFile in files
      fileName = objFile.Name
     If (InStr(strText, fileName) = 0) Then
        logFile.writeline (fileName) 
     EndIf
Next

Open in new window

0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 41773915
It helps if you write what you want in plain English before writing any code.

The problem would be something like this:
For each of the files found in the folder, test if it's present in the txt file. If not, append it to the log.

That will stop you from writing the second for loop...
1
 

Author Comment

by:Adrian Raj
ID: 41773938
@dan
Works like a charm. Thanks for the help and the tip.will do
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 41773957
You're welcome.

Glad I could help!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
The viewer will learn how to count occurrences of each item in an array.

867 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now