Solved

Input past end of file vbs script

Posted on 2016-08-27
9
68 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

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 …
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

760 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

23 Experts available now in Live!

Get 1:1 Help Now