如何testingpath是否是Windowsbatch file中的文件或目录?

我在这里search,发现有人使用这个

set is_dir=0 for %%i in ("%~1") do if exist "%%~si"\nul set is_dir=1 

但没有工作,当%1==c:\this is a file with spaces.csproj ,testing仍然成功,这意味着它仍然会被视为一个文件夹!

任何人都知道答案,我想这是一个非常普遍的问题,Windows已经存在很多年了,它应该有一个非常简单的解决scheme….

我知道if exist path\nul测试用于在MS-DOS上工作的文件夹。 我不知道是否因为长文件名的引入而中断。

我知道, if exist "long path\nul"不适用于Windows批处理。 直到今天我才意识到,只要路径是短8.3格式, if exist path\nul

原代码似乎在Vista上工作。 它似乎也应该在XP上工作,但我相信下面的XP错误正在成为: 批处理参数%〜S1给出不正确的8.3短名称 。

原来的代码不需要FOR循环,它可以简单地使用%~s1

以下是将路径完全分类为INVALID,FILE或FOLDER的变体。 它可以在Vista上运行,但是由于%~s1错误,不能在XP上运行。 我不知道它如何在MS-DOS上执行。
编辑2015-12-08:有一些Windows的情况下,这个失败

 @echo off if not exist "%~1" ( set "type=INVALID" ) else if exist %~s1\nul ( set "type=FOLDER" ) else ( set "type=FILE" ) @echo "%~1" = %type% 

我相信这个版本可以和几乎所有版本的Microsoft批处理一起使用,包括MS-DOS和XP。 (它显然不适用于不支持PUSHD的早期版本的DOS)

 @echo off if exist "%~1" (2>nul pushd "%~1" && (popd&set "type=FOLDER") || set "type=FILE" ) else set "type=INVALID" echo "%~1" = %type% 

更新2014-12-26

我敢肯定,以下将从XP以后的所有版本的Windows上工作,但我只在Win 7上测试。
编辑2015-12-08:这可能会在网络驱动器上失败,因为文件夹测试可能会错误地将文件报告为文件夹

 @echo off if exist %1\ ( echo %1 is a folder ) else if exist %1 ( echo %1 is a file ) else ( echo %1 does not exist ) 

更新2015-12-08

最后 – 真正应该在XP以后的任何Windows版本上运行的测试,包括网络驱动器和UNC路径

 for /f "tokens=1,2 delims=d" %%A in ("-%~a1") do if "%%B" neq "" ( echo %1 is a folder ) else if "%%A" neq "-" ( echo %1 is a file ) else ( echo %1 does not exist ) 

我只是试着这样。 希望这可以帮助。

 @ECHO OFF SET CURR_DIR=%CD% SET IS_DIR=0 CD %1% IF "%ERRORLEVEL%"=="0" SET IS_DIR=1 CD %CURR_DIR% ECHO IS DIRECTORY %IS_DIR% 

输出:

 D:\Work\Stand alone Java classes>test.bat D:\Work\Training IS DIRECTORY 1 D:\Work\Stand alone Java classes>test.bat D:\Work\Training\SRT.txt The directory name is invalid. IS DIRECTORY 0 

“dir”命令的/ ad选项列出文件夹,/ b选项为裸设备。 假设你已经检查了文件的存在,使用:

 dir /ad /b ChangeThisToYourFilename 1> NUL 2> NUL if %ERRORLEVEL% EQU 0 ( echo is a file ) else ( echo is NOT a file ) 

对于1班轮:

 dir /a:d /b C:\Windows 2>&1 | findstr /i /n /c:"File Not Found">nul && (@echo. Im a file) || (@echo. Im a folder) 

例如将C:\Windows更改为C:\Windows\Notepad.exe

-Shery Arun,dbenham,没有读你的! 与…一样..

以前,我使用"\nul"方法,但是现在很长一段时间,我用"\*"来测试一个现有的filespec是一个文件夹还是一个文件。 据我所知,它适用于所有版本的Windows,从Windows 95(也许是早期版本)到所有当前的Windows版本。

所以,与其他方法一样,首先测试文件是否存在。 然后,要查看它是否为“文件夹”,请使用以下命令进行测试: if exist "%fspec%\*"

 if not exist "%fspec%" goto :NotExistOrInvalid rem "%fspec%" is "Valid" and is either a "Folder", or a "File". if exist "%fspec%\*" goto :IsValidAndIsAFolder rem "%fspec%" is a "File" (a "Regular File" or a Shortcut/Link). goto :IsValidAndIsAFile 

例如:

 set "fspec=XYZ:%perlpath%" if not exist "%fspec%" echo "%fspec%": Invalid or not found && rem Invalid, goto :NotExistOrInvalid set "fspec=%perlpath%" if not exist "%fspec%" echo "%fspec%": Invalid or not found && rem goto :NotExistOrInvalid rem "%fspec%" Is a "Valid" filespec and is either a "Folder", or a "File". if exist "%fspec%\*" (echo "%fspec%" is a "Folder".) else echo "%fspec%" is a "File". set "fspec=%perlpath%\perl.exe" if not exist "%fspec%" echo "%fspec%": Invalid or not found && rem Invalid, goto :NotExistOrInvalid rem "%fspec%" Is a "Valid" filespec and is either a "Folder", or a "File". if exist "%fspec%\*" (echo "%fspec%" is a "Folder".) else echo "%fspec%" is a "File". 

这个输出是:

 "XYZ:F:\usr\perl\bin": Invalid or not found "F:\usr\perl\bin" is a "Folder". "F:\usr\perl\bin\perl.exe" is a "File".