1

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
William
  • 1,009
  • 15
  • 41
  • Use a `FOR` command to iterate over the files in the directory. Use the `FINDSTR` command to match against your list of files using the `/G` option. FINDSTR is case sensitive by default. – Squashman Dec 18 '17 at 16:38
  • 1
    Windows does not differentiate case when dealing with file/folder names. So "egg.mp3" really is the same as "Egg.mp3" – dbenham Dec 18 '17 at 16:43
  • @Squashman Was reading into FINDSTR, cant seem to figure out the correct way to go about this. Could you kindly post an answer that could be a working solution? Thanks! – William Dec 18 '17 at 16:43
  • @dbenham yeah i know that, but sadly i need some way that can accomplish this! – William Dec 18 '17 at 16:43
  • 1
    Possible duplicate of [if exist check with case sensitive in batch](https://stackoverflow.com/questions/17272693/if-exist-check-with-case-sensitive-in-batch) –  Dec 18 '17 at 16:53

2 Answers2

3

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"
dbenham
  • 127,446
  • 28
  • 251
  • 390
  • 1
    Dave, I think they want to output the source directory to the output file as well. – Squashman Dec 18 '17 at 16:58
  • The first method works a charm! From my tests that is anyway! But i am getting two line output which i dont want, just an output with the location of the non found track. I receive `test.mp3 followed by c://blabla/test.mp3` How can i change this to just output something like `c://blabla/test.mp3`? I dont want `test.mp3` in the output just the `c://blabla/test.mp3` `c://blabla/test2.mp3` etc – William Dec 19 '17 at 10:34
  • @irishwill200 - Oops, I forgot to redirect the output of the FINDSTR command so that that the found files do not appear in the result file. I edited my code to redirect the output to stderr so that the names of the existing files appear on the screen, and the missing files appear in the output.txt file. If you prefer to not see the existing files listed, then change `&2` to `nul`. – dbenham Dec 19 '17 at 13:10
  • Perfect! Thanks a bunch! Much appreciated :) – William Dec 19 '17 at 15:47
  • Actually i noticed something weird. If i put hello.mp3 in my find.txt and have a pause at the end of the script. I return file not found (Correct!) but that hello.mp3 does not go into the output.txt? – William Dec 20 '17 at 09:40
  • Also, trying to use one of the solutions using JREPL.BAT i receive the error: `JScript runtime error loading /R Search File: Out of memory` – William Dec 20 '17 at 09:49
  • Thinking about it, it does kinda do what i want i guess but i need it to output to that file ALSO if the file does not exist. – William Dec 20 '17 at 10:31
  • @irishwill200 - Yes, I had the wrong logic, but it should be good now. I fixed the problem by removing the `/V` option and using `||` instead of `&&`. I also had put the redirection `>&2` in the wrong place. Regarding JREPL, I'll have to investigate size limitations of `/R n:FILE`. Thanks for the feedback. – dbenham Dec 20 '17 at 13:40
1

Based off of a comment in this solution, this should do what you want:

@echo off
for /f "usebackq delims=" %%i in ("find.txt") do (
    echo Checking for %%i...
    dir /b /a-d "%%i"|find "%%i" >nul
    if %errorlevel% == 0 (
        echo "File Exists"
    ) ELSE (
        echo "Not found"
    )
)

Example of the base command in action:

D:\batch>dir /b /a-d "egg.mp3"|find "egg.mp3"

D:\batch>dir /b /a-d "Egg.mp3"|find "Egg.mp3"
Egg.mp3
  • 1
    Actually, my deleted comment was wrong. The `dir /b /a-d "%%i"` eliminates the risk of matching a substring. So this should work after all. (I suppose there may be an edge case where a short file name could lead to a problem, but I think that would be exceptionally rare) – dbenham Dec 18 '17 at 18:15
  • This method still shows file exists if i have a "egg.mp3" in my find.txt but i have "Egg.mp3" in the lib. – William Dec 19 '17 at 10:20