Search code examples
batch-filevariablescmd

CMD: FINDSTR: Cannot open Filename


I got this very wierd and unexplainable issue that boggles my mind for quite some time. Each time I run into this issue and I cannot figure out what is wrong since it is the same as it is for the other variables.

I know that there is probably some forum page wedged between the thousands of pages related to this where I could find my answer, however I cannot find that one in a thousand page.

So I am working on a script to run a filter for lines through and it is working fine except for this one stupid and stubbern variable that gives me the error message.

So when I run the following from my Bat-File called Test.bat (to keep names short) it gives me the error message following after.

Code:

@ECHO off
    set CNAME="RefilterMe"
    SET SPT="C:\ICT\RefilterMe.txt"
    SET SPT2="C:\ICT\%CNAME%.ReFiltered.txt"
    SET SPT3="C:\ICT\%CNAME%.Temp.txt"
    SET SPT4="C:\ICT\Refiltered"
    SET SPT5="C:\ICT\%CNAME%.Programms.txt"
    SET SPT6="C:\ICT\%CNAME%.Temp2Test.txt"
    SET SPT7="C:\ICT\%CNAME%.Filtered.Temp.txt"

TYPE %SPT% > %SPT5%

Error:

FINDSTR: Cannot open C:\ICT"RefilterMe.Programms.txt"

While writing my question I of course realized that it says 'C:\ICT"RefilterMe.Proper.txt"' instead of 'C:\ICT\RefilterMe.Programms.txt'. The part of 'TYPE %SPT% > %SPT5%' is placed directly behind the variable zone / area and it's so odd that it only happens to this one line.

Here some analysis info.

Test:

Copied SPT6 and named it SPT8. This one ran through no issue. But when I changed it to SPT5 and SPT5 to SPT8 it was also shifting with the issue.

@ECHO off
    set CNAME="RefilterMe"
    SET SPT="C:\ICT\RefilterMe.txt"
    SET SPT2="C:\ICT\%CNAME%.ReFiltered.txt"
    SET SPT3="C:\ICT\%CNAME%.Temp.txt"
    SET SPT4="C:\ICT\Refiltered"
    SET SPT8="C:\ICT\%CNAME%.Programs.txt"
    SET SPT6="C:\ICT\%CNAME%.Temp2Test.txt"
    SET SPT5="C:\ICT\%CNAME%.Programms.txt"
    SET SPT7="C:\ICT\%CNAME%.Filtered.Temp.txt"

TYPE %SPT% > %SPT5%
TYPE %SPT% > %SPT8%

The more I tested the wierder it got. Removing SPT5 completely only gives "Cannot Open" without adding what it cannot open. It is so odd that I am lost for words or reasons why. It works fine if i change SPT5 from %CNAME% to RefilterMe but I would love to know why this wierd issue occures and maybe someone on this forum got an idea.

Edit: As requested by Compo.

A bigger portion of the code to better help you understand the issue.

@ECHO off
    set CNAME="RefilterMe"
    SET SPT="C:\ICT\RefilterMe.txt"
    SET SPT2="C:\ICT\%CNAME%.ReFiltered.txt"
    SET SPT3="C:\ICT\%CNAME%.Temp.txt"
    SET SPT4="C:\ICT\Refiltered"
    SET "SPT5=C:\ICT\%CNAME%.Programms.txt"
    SET SPT6="C:\ICT\%CNAME%.Temp2Test.txt"
    SET SPT7="C:\ICT\%CNAME%.Filtered.Temp.txt"

TYPE %SPT% > %SPT5%
ECHO . > %SPT2%

ECHO Cleaning Collected Entries ...

ECHO Programms > %SPT7%


::First part with possible common programms

    (FindStr /IC:"Microsoft Edge" "%SPT5%" >Nul && (ECHO Edge))>> %SPT7%
    (FindStr /IC:"Mozilla Firefox" "%SPT5%" >Nul && (ECHO Firefox))>> %SPT7%

To add a bit more to confusion here is the variable setting from the original script I wrote which will contain the filtering part. With this there are no errors which is very wierd.

WMIC bios get serialnumber > C:\SN.txt
::Extracting only the serialnumber from the txt file
TYPE C:\SN.txt | findstr /v /c:"SerialNumber" > C:\SN1.txt
FOR /f "delims=" %%x in (C:\SN1.txt) DO SET SN1=%%x
::Defining the variable for the Filename
SET "SN=%SN1: =%"
::Deleting the created Files
DEL C:\SN.txt
DEL C:\SN1.txt

::Defining Save Paths (SPT) and the Move Path (MPT) of the Tool
SET MPT="\\<IP Address>\<Path>\ICT"
SET SPT="C:\%COMPUTERNAME%.%SN%.Complete.txt"
SET SPT2="C:\%COMPUTERNAME%.%SN%.Filtered.txt"
SET SPT3="C:\%COMPUTERNAME%.%SN%.Temp.txt"
SET SPT4="C:\%COMPUTERNAME%.%SN%.Screen.txt"
SET SPT5="C:\%COMPUTERNAME%.%SN%.Programs.txt"
SET SPT6="C:\%COMPUTERNAME%.%SN%.Screen.Temp.txt"
SET SPT7="C:\%COMPUTERNAME%.%SN%.Filtered.Temp.txt"

Hopefully this makes things clearer.


Solution

  • Here a simple batch from your use case illustrating why FINDSTR can't find the files which include %CNAME with your actual script:

    @ECHO off
        set CNAME="RefilterMe"
        SET SPT="C:\ICT\RefilterMe.txt"
        SET SPT2="C:\ICT\%CNAME%.ReFiltered.txt"
        SET SPT3="C:\ICT\%CNAME%.Temp.txt"
        SET SPT4="C:\ICT\Refiltered"
        SET SPT8="C:\ICT\%CNAME%.Programs.txt"
        SET SPT6="C:\ICT\%CNAME%.Temp2Test.txt"
        SET SPT5="C:\ICT\%CNAME%.Programms.txt"
        SET SPT7="C:\ICT\%CNAME%.Filtered.Temp.txt"
    
    call echo "more < %SPT4%"
    more < %SPT4%
    
    call echo "more < %SPT3%"
    more < %SPT3%
    
    call echo "more < %SPT5%"
    more < %SPT5%
    
    call echo "more < %SPT8%"
    more < %SPT8%
    
    for %%i in (%SPT3%) do echo %%i
    for %%i in (%SPT4%) do echo %%i
    for %%i in (%SPT5%) do echo %%i
    for %%i in (%SPT8%) do echo %%i
    
    call echo "FINDSTR x %SPT3%"
    FINDSTR x %SPT3%
    call echo "FINDSTR x %SPT4%"
    FINDSTR x %SPT4%
    call echo "FINDSTR x %SPT5%"
    FINDSTR x %SPT5%
    call echo "FINDSTR x %SPT8%"
    FINDSTR x %SPT8%
    

    Its output with files having 'aax', 'bxb', 'xee'_, 'xff' as content :

    "more < "C:\ICT\Refiltered""
    "xee"
    "more < "C:\ICT\"RefilterMe".Temp.txt""
    "xff"
    "more < "C:\ICT\"RefilterMe".Programms.txt""
    "aax"
    "more < "C:\ICT\"RefilterMe".Programs.txt""
    "bxb"
    "C:\ICT\"RefilterMe".Temp.txt"
    "C:\ICT\Refiltered"
    "C:\ICT\"RefilterMe".Programms.txt"
    "C:\ICT\"RefilterMe".Programs.txt"
    "FINDSTR x "C:\ICT\"RefilterMe".Temp.txt""
    FINDSTR : Can't open  C:\ICT"RefilterMe.Temp.txt
    "FINDSTR x "C:\ICT\Refiltered""
    "xee"
    "FINDSTR x "C:\ICT\"RefilterMe".Programms.txt""
    FINDSTR : Can't open  C:\ICT"RefilterMe.Programms.txt
    "FINDSTR x "C:\ICT\"RefilterMe".Programs.txt""
    FINDSTR : Can't open C:\ICT"RefilterMe.Programs.txt
    

    As you can see for %SPT3%, the FINDSTR command will be FINDSTR x "C:\ICT\"RefilterMe".Temp.txt". As it is not a path in a redirection ( < "path" ), their is no quotes removal. FINDSTR will then try to find the file "C:\ICT\"RefilterMe".Temp.txt" as is, the quotes are then trimed but as there is an escaped quote (\" from C:\ICT\"), this quote is kept as is. Thus, FINDSTR will look for C:\ICT"RefilterMe.Temp.txt. And indeed can't find it.

    If you change set CNAME="RefilterMe" with set "CNAME=RefilterMe", you 'll see that the script will behave as expected for the FINDSTR commands.

    I'll suggest you to avoid quotes in cmd batch variable values as most as possible when they are not a significant part of the value. You should fix all your SET this way.