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

问题描述 投票:1回答:2

我有一段代码贯穿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
batch-file case-sensitive strict
2个回答
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阻止以下工作正常 - 请勿使用!

@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
投票

根据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
© www.soinside.com 2019 - 2024. All rights reserved.