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

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

问题描述

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

I searched here, found someone using this

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

但是没有用,当%1==c: his is a file with space.csproj时,测试还是成功,说明还是会被当作文件夹处理!!!

but didn't work, when %1==c: his 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....

推荐答案

我知道用于在 MS-DOS 上工作的文件夹的 if exists path ul 测试.不知道是不是被长文件名的引入破坏了.

I know the if exist path ul 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.

我知道 如果存在长路径 ul" 在 Windows 批处理上不起作用.直到今天我才意识到 if existing path ul 可以在 Vista 及更高版本上运行,只要 path 是 8.3 的简短形式.

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

原始代码似乎适用于 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.

原代码不需要FOR循环,直接使用%~s1

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

这是一个将路径完全分类为无效、文件或文件夹的变体.它适用于 Vista,但由于 %~s1 错误而不适用于 XP.我不确定它在 MS-DOS 上的表现如何.
EDIT 2015-12-08:有许多 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
ul ( set "type=FOLDER" ) else ( set "type=FILE" )
@echo "%~1" = %type%

我相信这种变体适用于几乎所有版本的 Microsoft 批处理,包括 MS-DOS 和 XP.(它显然不适用于不支持 PUSHD 的早期版本的 DOS)

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

我很确定以下内容适用于 XP 以后的所有 Windows 版本,但我只在 Win 7 上进行了测试.
编辑 2015-12-08:这可能会在网络驱动器上失败,因为文件夹测试可能会将文件错误地报告为文件夹

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-08

最后 - 一个真正适用于 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
)

注意 - 此技术旨在用于没有任何通配符的路径(单个特定文件或文件夹).如果提供的路径包含一个或多个通配符,则它提供文件系统遇到的第一个文件或文件夹的结果.相同的目录结构可能会根据底层文件系统(FAT32、NTFS 等)给出不同的排序结果

Note - This technique is intended to be used for a path without any wildcards (a single specific file or folder). If the provided path includes one or more wildcards, then it provides the result for the first file or folder that the file system encounters. Identical directory structures may give different sort order results depending on the underlying file system (FAT32, NTFS, etc.)

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

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