?
Solved

Input past end of file vbs script

Posted on 2016-08-27
9
Medium Priority
?
144 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 25

Expert Comment

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

Assisted Solution

by:Dan Craciun
Dan Craciun earned 2000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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
 

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 35

Accepted Solution

by:
Dan Craciun earned 2000 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 35

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 35

Expert Comment

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

Glad I could help!
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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Measuring Server's processing rate with a simple powershell command. The differences in processing rate also was recorded in different use-cases, when a server in free and busy states.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month17 days, 9 hours left to enroll

830 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