Search code examples
batch-filecase-sensitivestrict

Strict string matching locate file batch - Case sensitive


I have a piece of code which runs through each line in a find.txt file and tries locate it. If it does not exist it will populate a output.txt file. Thing is, if a file is called "Egg.mp3" and in my find.txt has "egg.mp3" it counts that as if it found it? Now correct.. It did but i need something thats strict! Case sensitive even so that "Egg.mp3" is not the same as "egg.mp3" therefore to drop "egg.mp3" into my output.txt.

Does anyone have a solution to this? I searched around and found nothing that may help.

Batch code:

for /f "usebackq delims=" %%i in ("E:\find.txt") do IF EXIST "C:\Users\PC\Desktop\Lib\%%i" (echo "File Exists") ELSE (echo "C:\Users\PC\Desktop\Lib\%%i">> "C:\Users\PC\Desktop\output.txt")
pause

Solution

  • Windows does not differentiate case when dealing with file or folder names. So "egg.mp3" and "Egg.mp3" really are equivalent.

    But if you still want to include file names that differ only in case, then you can do the following:

    @echo off
    set "folder=C:\Users\PC\Desktop\Lib"
    set "output=C:\Users\PC\Desktop\output.txt"
    
    pushd "%folder%"
    >"%output%" (
      for /f "usebackq delims=" %%F in ("e:\find.txt") do dir /b /a-d "%%F" 2>nul | findstr /xc:"%%F" >&2 || echo %folder%\%%F
    )
    popd
    

    The following would be a lot faster (assuming you don't really need the path info in the output), but this nasty FINDSTR bug prevents the following from working properly - DO NOT USE!

    @echo off
    dir /b /a-d "C:\Users\PC\Desktop\Lib" >"e:\temp.txt"
    findstr /LXVG:"e:\temp.txt" "e:\find.txt" >"C:\Users\PC\Desktop\output.txt"
    del "e:\temp.txt"
    

    If you have JREPL.BAT, then you can do the following instead:

    @echo off
    dir /b /a-d "C:\Users\PC\Desktop\Lib" >"e:\temp.txt"
    call jrepl "e:\temp.txt" "" /b /e /r 0:FILE /f "e:\find.txt" /o "C:\Users\PC\Desktop\output.txt"
    del "e:\temp.txt"
    

    If you really need the path info in your output, then you can do the following:

    @echo off
    dir /b /a-d "C:\Users\PC\Desktop\Lib" >"e:\temp.txt"
    jrepl "e:\temp.txt" "" /b /e /r 0:FILE /f "e:\find.txt" | jrepl "^" "C:\Users\PC\Desktop\Lib\" /o "C:\Users\PC\Desktop\output.txt"
    del "e:\temp.txt"