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

时间:2017-12-18 16:25:14

标签: batch-file case-sensitive strict

我有一段代码贯穿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

2 个答案:

答案 0 :(得分:3)

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会阻止以下内容正常工作 - < em>不要使用!

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

答案 1 :(得分:1)

根据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