如何测试一个路径是Windows批处理文件一个文件或目录? [英] How to test if a path is a file or directory in Windows batch file?

查看:267
本文介绍了如何测试一个路径是Windows批处理文件一个文件或目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里搜查,发现有人使用这种

I searched here, found someone using this

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

但没有工作,当%1 == C:\\这是spaces.csproj文件,测试仍然成功,这意味着它仍然是当作一个文件夹!

but didn't work, when %1==c:\this is a file with spaces.csproj, the test still success, which means it will still be treated as a folder!!!

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

anyone knows the answer, i guess this is a very common problem and Windows has existed for many many years, it should have a very simple solution....

推荐答案

我知道是否存在路径\\ NUL 测试用于在MS-DOS工作的文件夹。我不知道这是否与引进长文件名的损坏。

I know the if exist path\nul test for a folder used to work on MS-DOS. I don't know if it was broken with the introduction of long file names.

我知道是否存在长路径\\ NUL不能在Windows批处理工作。我不知道,直到今天是否存在路径\\ NUL 适用于Vista和只要路径是在短期形式8.3超越。

I knew that if exist "long path\nul" does not work on Windows batch. I did not realize until today that if exist path\nul works on Vista and beyond as long as path is in the short 8.3 form.

原来的code似乎在Vista上工作。现在看来似乎应该在XP工作为好,但我相信下面的错误XP的凑了过来:批处理参数%〜S1给人8.3不正确的短名称

The original code appears to work on Vista. It seems like it should work on XP as well, but I believe the following XP bug is getting in the way: Batch parameter %~s1 gives incorrect 8.3 short name.

原来的code不需要FOR循环,它可以简单地使用%〜S1

The original code does not need the FOR loop, it could simply use %~s1

下面是一个完全归类为无效,文件或文件夹的路径的变化。它的工作原理在Vista上,但因为%〜S1 的bug在XP不起作用。我不知道它如何执行在MS-DOS。结果
编辑2015年12月8日:有许多的Windows情况下失败

Here is a variation that fully classifies a path as INVALID, FILE or FOLDER. It works on Vista, but does NOT work on XP because of the %~s1 bug. I'm not sure how it performs on MS-DOS.
EDIT 2015-12-08: There are a number of Windows situations where this fails

@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的工作。 (它显然不会对DOS的早期版本的工作不支持PUSHD)

I believe this variation will work with nearly all versions of Microsoft batch, including MS-DOS and XP. (it obviously won't work on early versions of DOS that don't support PUSHD)

@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

我是pretty确保下面会从XP以后所有版本的Windows工作,但我只在Win 7测试结果
编辑2015年12月8日:这可以在网络驱动器失败,因为该文件夹的测试可能误报的文件作为文件夹

I'm pretty sure the following will work on all versions of Windows from XP onward, but I have only tested on Win 7.
Edit 2015-12-08: This can fail on network drives because the folder test can falsely report a file as a folder

@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月8日

最后 - 一个测试,真正应该从XP的任何Windows版本以后的工作,包括网络驱动器和UNC路径

Finally - a test that truly should work on any Windows version from XP onward, including with network drives and UNC paths

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
)

这篇关于如何测试一个路径是Windows批处理文件一个文件或目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆