如果存在,则批量检查区分大小写

时间:2013-06-24 09:57:43

标签: windows batch-file

我需要检查给定文件是否存在,区分大小写,out.txt(所有都是小写字母)文件存在于我运行脚本的位置。

代码:

Case1:
filename=out.txt
 if exist %filename% (
    echo file exist...
) else echo File doesn't exist...

Case2:
filename=OUT.TXT
if exist %filename% (
    echo file exist...
) else echo File doesn't exist...

对于这两种情况,它显示文件存在...输出。但我需要检查区分大小写。它应该显示OUT.TXT

的“文件不存在”消息

提前致谢

2 个答案:

答案 0 :(得分:2)

从参数中获取文件名:

@echo off 
dir /b /a-d "%~1"|find "%~1" >nul
if %errorlevel% == 0 (echo found) else (echo fail)

dir实际上不区分大小写 - 但find是......

答案 1 :(得分:1)

添加了文件列表解析:

@echo off
for /f "delims=" %%z in ('type "namelist.txt" ') do (
if not exist "%%~z" echo "%%~z" not found
if     exist "%%~z" for %%a in ("%%~z") do if "%%a"=="%%~z" (echo "%%~z" is the right case) else (echo "%%~z" is the wrong case "%%a found")
)
pause