我在这里搜索了一下,发现有人使用了这个方法
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
时,测试仍然成功,这意味着它仍将被视为文件夹!!!
有人知道答案,我猜这是一个非常普遍的问题,Windows存在已经很多年了,应该有一个非常简单的解决方案....
我在这里搜索了一下,发现有人使用了这个方法
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
时,测试仍然成功,这意味着它仍将被视为文件夹!!!
有人知道答案,我猜这是一个非常普遍的问题,Windows存在已经很多年了,应该有一个非常简单的解决方案....
我知道在MS-DOS中,if exist path\nul
用于测试文件夹是否存在。但我不确定在长文件名出现后是否还有效。
我知道在Windows批处理中,if exist "long path\nul"
无法工作。直到今天,我才意识到只要路径以短的8.3形式呈现,if exist path\nul
就可以在Vista及以上版本上工作。
原始代码似乎在Vista上可以正常工作。它看起来也应该在XP上工作,但我相信以下XP bug会导致问题:Batch parameter %~s1 gives incorrect 8.3 short name。
原始代码不需要使用FOR循环,它可以简单地使用%~s1
。
这里是一个变体,可以完全将路径分类为INVALID(无效)、FILE(文件)或FOLDER(文件夹)。它适用于Vista,但由于%~s1
的bug,不适用于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
终于,这个测试可以在任何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
)
注意 - 此技术旨在用于没有通配符(单个特定文件或文件夹)的路径。 如果提供的路径包含一个或多个通配符,则它会为文件系统遇到的第一个文件或文件夹提供结果。 根据底层文件系统(FAT32、NTFS 等),相同的目录结构可能会给出不同的排序结果。
%1\
和%1
部分放在引号中不是个好主意吗? - zb226\
是无害的。"c:\path\"
和"c:\path"\
是等效的。如果您确实想显式添加自己的引号,则必须使用"%~1"
和"%~1\"
。 - dbenham%1
…我现在正在处理另一种情况,即测试文件名在一个常规变量中,那么当然,引号很重要。无论如何,感谢您的澄清! - zb226@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
PUSHD %1 2>NUL && SET IS_DIR=1 || SET IS_DIR=0 & POPD
- aaron“dir” 命令的 /ad 选项会列出文件夹,“/b” 选项则为裸格式。假设您已经检查了文件是否存在,请使用以下命令:
dir /ad /b ChangeThisToYourFilename 1> NUL 2> NUL
if %ERRORLEVEL% EQU 0 (
echo is a folder
) else (
echo is NOT a folder
)
dir /ad /b "somePath" 1>nul 2>nul && (echo 是一个文件夹) || (echo 不是一个文件夹)
- dbenhamdir /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
-抱歉 Arun,dbenham,我没有看到你们的!和你们一样。
dir /a:d /b C:\Windows 2>nul >nul && (@echo. 我是一个文件) || (@echo. 我是一个文件夹)
- Radon8472之前,我使用了 "\nul"
方法,但很长一段时间以来,我已经开始使用 "\*"
来测试现有的文件名是否是文件夹或文件。据我所知,它适用于所有 Windows 版本,从 Windows 95(也许更早的版本)到当前所有的 Windows 版本。
因此,与其他方法一样,首先检查文件是否存在。然后,要查看它是否是 "Folder",请使用以下命令进行测试: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".
这个解决方案结合了文件属性参数扩展 (%~a1
) 和 变量子字符串提取 (%variable:~0,1%
):
@ECHO OFF
CALL :is_directory C:\Windows
CALL :is_directory C:\MinGW\share\doc\mingw-get\README
CALL :is_directory C:\$Recycle.Bin
CALL :is_directory "C:\Documents and Settings"
CALL :is_directory "%LOGONSERVER%\C$\Users\All Users"
GOTO :EOF
:is_directory
SETLOCAL
SET file_attribute=%~a1
IF "%file_attribute:~0,1%"=="d" (
ECHO %file_attribute% %1 is a directory
) ELSE (
ECHO %file_attribute% %1 is NOT a directory
)
ENDLOCAL
GOTO :EOF
输出:
d-------- C:\Windows is a directory
--a------ C:\MinGW\share\doc\mingw-get\README is NOT a directory
d--hs---- C:\$Recycle.Bin is a directory
d--hs---l "C:\Documents and Settings" is a directory
d--hs---l "\\MYCOMPUTER\C$\Users\All Users" is a directory
@echo off
IF EXIST %OneDriveCommercial%\desktop\ (
copy "%appdata%\Microsoft\Windows\Start Menu\Programs\Work Resources (RADC)\*.lnk" "%OneDriveCommercial%\desktop\"
echo Copied RDAC to %OneDriveCommercial%\desktop
) else (
copy "%appdata%\Microsoft\Windows\Start Menu\Programs\Work Resources (RADC)\*.lnk" "%userprofile%\desktop"
echo Copied RDAC to %userprofile%\desktop
)