?
Solved

Input past end of file vbs script

Posted on 2016-08-27
9
Medium Priority
?
125 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
[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
  • 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
Cloud Training Guides

FREE GUIDES: In-depth and hand-crafted Linux, AWS, OpenStack, DevOps, Azure, and Cloud training guides created by Linux Academy instructors and the community.

 

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

762 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