严格字符串匹配定位文件批处理 - 区分大小写

Strict string matching locate file batch - Case sensitive

我有一段代码遍历 find.txt 文件中的每一行并尝试找到它。如果它不存在,它将填充一个 output.txt 文件。问题是,如果一个文件被称为 "Egg.mp3" 并且在我的 find.txt 中有 "egg.mp3" 它认为它好像找到了它?现在正确..确实如此,但我需要一些严格的东西!区分大小写,即使 "Egg.mp3" 与 "egg.mp3" 不同,因此将 "egg.mp3" 放入我的 output.txt.

有人对此有解决方案吗?我四处搜索,没有发现任何可能有帮助的东西。

批号:

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

Windows 在处理文件或文件夹名称时不区分大小写。所以 "egg.mp3" 和 "Egg.mp3" 真的是等价的。

但如果您仍想包含仅大小写不同的文件名,则可以执行以下操作:

@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

以下会快很多(假设您真的不需要输出中的路径信息),但是 this nasty FINDSTR bug 会阻止以下正常工作 - 请勿使用!

@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"

如果您有 JREPL.BAT,那么您可以改为执行以下操作:

@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"

如果您确实需要输出中的路径信息,那么您可以执行以下操作:

@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"

根据 this solution 中的评论,这应该可以满足您的要求:

@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"
    )
)

基本命令示例:

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

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