locked
vbscript search for string within string not working if filename contains a dash / hyphen RRS feed

  • Question

  • Hello, 

    I am hoping this is just something simple I missed but everything tried so far doesnt work.

    I have a folder containing a list of files.  What I need to do is read a text based file that contains a list of file names and search the folder for any file that contains the entry from the text file.

    So for example my text file contains the following entry

    file1

     

    My test folder is C:\test    This folder contains the following files

    Testfile1.exe

    Testfile2.exe

    The goal is a script that says search the folder c:\test for any file that contains "file1" and if its found then delete that file from the folder.

    So from the example above the result should be that Testfile1.exe contains the word "file1" so it gets deleted.  The folder should only contain Testfile2.exe after the script runs.

    Here is where I need help.   I can get the above to work correctly several different ways .  My problem is that the actual file names I will be using contain a dash,      5-file1.exe

    No matter what I do I cannot get the same script to work if the filename contains a dash.  I think it has to do with the script engine reading it as a math operator or some other critera but it certainly does not like the dash.

    I did not include my sample code because I am open to any suggestions.  Read the text file line by line,  read into an array or dictionary,   anything that works is fine by me.

    If anyone can figure this out I would really appreciate it.

    Monday, September 29, 2014 6:59 PM

Answers

  • thanks for the reply jrv.   I think I have it working now.  I used InStr to do the search and it seems to be doing what I want now.

    Since we have no idea what your code looked like we cannot know what the problem was.  I was pointing out that there is nothing special about a dash character in a name.  If it was a Unicode special character that looks like a dash then you will have issues with VBScript.

    ¯\_(ツ)_/¯

    • Marked as answer by jkeadle2 Tuesday, September 30, 2014 3:07 PM
    Tuesday, September 30, 2014 11:22 AM

All replies

  • It's hard for us to tell what's wrong with your code because you didn't post it.


    -- Bill Stewart [Bill_Stewart]

    Monday, September 29, 2014 7:04 PM
  • Works fine for me:

    PS C:\scripts> dir c:\temp2\* -include '*test-1*'
    
    
        Directory: C:\temp2
    
    
    Mode                LastWriteTime     Length Name
    ----                -------------     ------ ----
    -a---         9/29/2014   4:44 PM          0 test-1.txt
    


    ¯\_(ツ)_/¯

    Monday, September 29, 2014 8:44 PM
  • thanks for the reply jrv.   I think I have it working now.  I used InStr to do the search and it seems to be doing what I want now.
    Tuesday, September 30, 2014 11:10 AM
  • Hi Bill,

    Thanks for the reply.  Sorry about that,  I should have posted what I had.

    After beating my head against the wall I took a different look at it and it looks like I may have figured it out.  I may have had the syntax wrong for how I was using the InStr function.

    This seems to do what I want.  Basically the filelist.txt has an entry such as "1-1file1"

    The folder directory contains a file called 01-1-1file.exe.   If the actual file name contains the entry in the text file then it writes the actual file name to a an exclusion file to be marked for deletion.

    so If "1-1file1"  is found in "01-1-1file1.exe" then write the filename to the log.

    If you see a better way to do this I welcome any suggestions.

    Thanks, 

    Option Explicit                                                               
    Const forReading = 1                                                          
    Const forWriting = 2                                                         

    Dim inputFile, outputFile, fso, fileList, logFile, fileSpec, file           

    inputFile = "c:\test\filelist.txt"                                                    
    outputFile = "excluded.log"                                                    

    Set fso = CreateObject("Scripting.FileSystemObject")                    
    Set fileList = fso.OpenTextFile(inputFile, forReading)                        
    If Not (fso.FileExists(outputFile)) Then fso.CreateTextFile(outputfile)       
    Set logFile = fso.OpenTextFile(outputFile, forWriting)                        

    Do while not fileList.AtEndOfStream                                           
        fileSpec = fileList.ReadLine() 
    For Each file in fso.GetFolder("C:\test").Files
    if Instr(1, file.Name, fileSpec, 1) Then         '/this line is what made it work
    logFile.writeline (file.Name)
         End If 
    Next
    Loop

    fileList.close                                                                
    logFile.close 

    Tuesday, September 30, 2014 11:20 AM
  • thanks for the reply jrv.   I think I have it working now.  I used InStr to do the search and it seems to be doing what I want now.

    Since we have no idea what your code looked like we cannot know what the problem was.  I was pointing out that there is nothing special about a dash character in a name.  If it was a Unicode special character that looks like a dash then you will have issues with VBScript.

    ¯\_(ツ)_/¯

    • Marked as answer by jkeadle2 Tuesday, September 30, 2014 3:07 PM
    Tuesday, September 30, 2014 11:22 AM
  • thanks for confirming that.  I wasnt completely sure because of the results I was getting.  

    I did find my problem was actually with the source file that contains the file name listing.  It is a text based file but has no extension (or I should say you can read the file with a text editor.

    Anyway I created a new text file with the same entries and the script works like a champ.   I made the mistake of assuming that because the actual source file was text readable that it was viewed and treated like any other text file which doesnt appear to be the case.   Ill have to dig a little more on this .

    Here is the code I was using. For testing I have it setup so if a match is found it writes that filename to a exclusion log file.

    Option Explicit                                                               
    Const forReading = 1                                                          
    Const forWriting = 2                                                         

    Dim inputFile, outputFile, fso, fileList, logFile, fileSpec, file           

    inputFile = "c:\test\filelist.txt"                                                    
    outputFile = "excluded.log"                                                    

    Set fso = CreateObject("Scripting.FileSystemObject")                    
    Set fileList = fso.OpenTextFile(inputFile, forReading)                        
    If Not (fso.FileExists(outputFile)) Then fso.CreateTextFile(outputfile)       
    Set logFile = fso.OpenTextFile(outputFile, forWriting)                        

    Do while not fileList.AtEndOfStream                                           
        fileSpec = fileList.ReadLine() 
    For Each file in fso.GetFolder("C:\test").Files
    if Instr(1, file.Name, fileSpec, 1) Then
    logFile.writeline (file.Name)
    End If 
    Next
    Loop

    fileList.close                                                                
    logFile.close  

    Tuesday, September 30, 2014 2:09 PM