如何测试文件是否是批处理脚本中的目录?

时间:2020-11-26 02:23:36

Is there any way to find out if a file is a directory?

有没有什么方法可以查出一个文件是否是一个目录?

I have the file name in a variable. In Perl I can do this:

我在变量中有文件名。在Perl中,我可以这样做:

if(-d $var) { print "it's a directory\n" }

21 个解决方案

#1


43  

You can do it like so:

你可以这样做:

IF EXIST %VAR%\NUL ECHO It's a directory

However, this only works for directories without spaces in their names. When you add quotes round the variable to handle the spaces it will stop working. To handle directories with spaces, convert the filename to short 8.3 format as follows:

但是,这只适用于名称中没有空格的目录。当您在变量周围添加引号来处理空格时,它将停止工作。要处理带有空格的目录,将文件名转换为短8.3格式如下:

FOR %%i IN (%VAR%) DO IF EXIST %%~si\NUL ECHO It's a directory

The %%~si converts %%i to an 8.3 filename. To see all the other tricks you can perform with FOR variables enter HELP FOR at a command prompt.

%%~si将%%i转换为8.3文件名。要查看所有其他可以为变量执行的技巧,请在命令提示符处输入帮助。

(Note - the example given above is in the format to work in a batch file. To get it work on the command line, replace the %% with % in both places.)

(注意——上面给出的示例是在批处理文件中工作的格式。要使它在命令行上工作,请在两个地方用%替换%%。

#2


75  

This works:

如此:

if exist %1\* echo Directory

Works with directory names that contains spaces:

使用包含空格的目录名:

C:\>if exist "c:\Program Files\*" echo DirectoryDirectory

Note that the quotes are necessary if the directory contains spaces:

注意,如果目录包含空格,则必须使用引号:

C:\>if exist c:\Program Files\* echo Directory

Can also be expressed as:

也可表示为:

C:\>SET D="C:\Program Files"C:\>if exist %D%\* echo DirectoryDirectory

This is safe to try at home, kids!

这在家里是安全的,孩子们!

#3


42  

Recently failed with different approaches from the above. Quite sure they worked in the past, maybe related to dfs here. Now using the files attributes and cut first char

最近由于以上方法的不同而失败。很确定他们以前工作过,可能和dfs有关。现在使用文件属性和第一个char。

@echo offSETLOCAL ENABLEEXTENSIONSset ATTR=%~a1set DIRATTR=%ATTR:~0,1%if /I "%DIRATTR%"=="d" echo %1 is a folder:EOF

#4


11  

Further to my previous offering, I find this also works:

继我之前的出价之后,我发现这也适用:

if exist %1\ echo Directory

No quotes around %1 are needed because the caller will supply them.This saves one entire keystroke over my answer of a year ago ;-)

不需要在%1附近的引号,因为调用者将提供它们。这比我一年前的回答节省了整整一个按键;

#5


9  

Here's a script that uses FOR to build a fully qualified path, and then pushd to test whether the path is a directory. Notice how it works for paths with spaces, as well as network paths.

这里有一个脚本,用于构建完全限定的路径,然后pushd测试路径是否是目录。注意它是如何处理带空间的路径以及网络路径的路径的。

@echo offif [%1]==[] goto usagefor /f "delims=" %%i in ("%~1") do set MYPATH="%%~fi"pushd %MYPATH% 2>nulif errorlevel 1 goto notdirgoto isdir:notdirecho not a directorygoto exit:isdirpopdecho is a directorygoto exit:usageecho Usage:  %0 DIRECTORY_TO_TEST:exit

Sample output with the above saved as "isdir.bat":

上述样本输出以“is .bat”形式保存:

C:\>isdir c:\Windows\system32is a directoryC:\>isdir c:\Windows\system32\wow32.dllnot a directoryC:\>isdir c:\notadirnot a directoryC:\>isdir "C:\Documents and Settings"is a directoryC:\>isdir \is a directoryC:\>isdir \\ninja\SharedDocs\cpu-zis a directoryC:\>isdir \\ninja\SharedDocs\cpu-z\cpuz.ininot a directory

#6


6  

This works perfectly

这是完美的

if exist "%~1\" echo Directory

we need to use %~1 to remove quotes from %1, and add a backslash at end. Then put thw whole into qutes again.

我们需要使用%~1从%1中删除引号,并在末尾添加反斜杠。然后再把它放进qutes中。

#7


3  

The NUL technique seems to only work on 8.3 compliant file names.

(In other words, `D:\Documents and Settings` is "bad" and `D:\DOCUME~1` is "good")


I think there is some difficulty using the "NUL" tecnique when there are SPACES in the directory name, such as "Documents and Settings."

我认为当目录名中有空格时,使用“NUL”tecnique会有一些困难,比如“文档和设置”。

I am using Windows XP service pack 2 and launching the cmd prompt from %SystemRoot%\system32\cmd.exe

我正在使用Windows XP服务包2,并从%SystemRoot%\system32\cmd.exe启动cmd提示

Here are some examples of what DID NOT work and what DOES WORK for me:

以下是一些对我不起作用的和对我起作用的例子:

(These are all demonstrations done "live" at an interactive prompt. I figure that you should get things to work there before trying to debug them in a script.)

(这些都是在交互提示符下“实时”完成的演示。我认为,在尝试在脚本中调试之前,应该先让它们在那里工作。

This DID NOT work:

这并不工作:

D:\Documents and Settings>if exist "D:\Documents and Settings\NUL" echo yes

D:\文档和设置如果存在>“D:\文档和设置”echo yes

This DID NOT work:

这并不工作:

D:\Documents and Settings>if exist D:\Documents and Settings\NUL echo yes

D:\ document and Settings>if exist D:\Documents and Settings\NUL echo yes

This DOES work (for me):

这对我很有用:

D:\Documents and Settings>cd ..

D:\文档和设置> cd . .

D:\>REM get the short 8.3 name for the file

快速获取文件的8.3个简短名称

D:\>dir /x

D:\ > dir / x

Volume in drive D has no label.Volume Serial Number is 34BE-F9C9

驱动器D中的卷没有标签。卷序列号是34BE-F9C9

Directory of D:\
09/25/2008 05:09 PM <DIR> 2008
09/25/2008 05:14 PM <DIR> 200809~1.25 2008.09.25
09/23/2008 03:44 PM <DIR> BOOST_~3 boost_repo_working_copy
09/02/2008 02:13 PM 486,128 CHROME~1.EXE ChromeSetup.exe
02/14/2008 12:32 PM <DIR> cygwin

目录D:\ 09/25/2008 05:09 PM <目录> 2008 09/25/2008 05:14 PM <目录> 200809~1.25 2008.25 /2008 03:09 /23/2008 03:44 PM <目录> boost_3 boost_repo_working_copy 09/02/2008 02:02 /2008 02:13 PM 486,128 CHROME~1。EXE ChromeSetup。exe 02/14/2008 12:32 PM

cygwin。

[[Look right here !!!! ]]
09/25/2008 08:34 AM <DIR> DOCUME~1 Documents and Settings

[[看这里! ! ! ![英语背诵文选[hide] 1文档和设置

09/11/2008 01:57 PM 0 EMPTY_~1.TXT empty_testcopy_file.txt
01/21/2008 06:58 PM <DIR> NATION~1 National Instruments Downloads
10/12/2007 11:25 AM <DIR> NVIDIA
05/13/2008 09:42 AM <DIR> Office10
09/19/2008 11:08 AM <DIR> PROGRA~1 Program Files
12/02/1999 02:54 PM 24,576 setx.exe
09/15/2008 11:19 AM <DIR> TEMP
02/14/2008 12:26 PM <DIR> tmp
01/21/2008 07:05 PM <DIR> VXIPNP
09/23/2008 12:15 PM <DIR> WINDOWS
02/21/2008 03:49 PM <DIR> wx28
02/29/2008 01:47 PM <DIR> WXWIDG~2 wxWidgets
3 File(s) 510,704 bytes
20 Dir(s) 238,250,901,504 bytes free

09/11/2008 01:57 0空档~1。TXT empty_testcopy_file。 <目录> 国家~1国家仪器下载10/12/2007 11:25 AM <目录> NVIDIA 05/13/2008 09:42 AM <目录> Office10 09/19/2008 11:08 AM <目录> 程序文件12/02/1999 02:54 PM 24576 setx。exe 09/15/2008 11:19 AM

TEMP 02/14/2008 12:26 PM tmp 01/21/2008 07:05 PM VXIPNP 09/23/2008 12:15 PM WINDOWS 02/21/2008 PM > wx28 / 295 bytes

D:\>REM now use the \NUL test with the 8.3 name

现在使用8.3名称的\NUL测试

D:\>if exist d:\docume~1\NUL echo yes

D:\>如果存在D: docume~1\NUL echo yes

yes

是的

This works, but it's sort of silly, because the dot already implies i am in a directory:

这行得通,但有点傻,因为这个点已经暗示我在一个目录中:

D:\Documents and Settings>if exist .\NUL echo yes

D:如果存在的话,文件和设置>

#8


3  

This works and also handles paths with spaces in them:

它可以工作,也可以处理带有空格的路径:

dir "%DIR%" > NUL 2>&1if not errorlevel 1 (    echo Directory exists.) else (    echo Directory does not exist.)

Probably not the most efficient but easier to read than the other solutions in my opinion.

在我看来,这可能不是最有效但更容易阅读的解决方案。

#9


3  

A variation of @batchman61's approach (checking the Directory attribute).

@batchman61方法的变体(检查目录属性)。

This time I use an external 'find' command.

这次我使用了一个外部的“查找”命令。

(Oh, and note the && trick. This is to avoid the long boring IF ERRORLEVEL syntax.)

(哦,注意&&诀窍。这是为了避免冗长而无趣的错误级别语法。

@ECHO OFFSETLOCAL EnableExtentionsECHO.%~a1 | find "d" >NUL 2>NUL && (    ECHO %1 is a directory)

Outputs yes on:

输出是:

  • Directories.
  • 目录。
  • Directory symbolic links or junctions.
  • 目录符号链接或结点。
  • Broken directory symbolic links or junctions. (Doesn't try to resolve links.)
  • 损坏的目录符号链接或结点。(不要尝试解析链接。)
  • Directories which you have no read permission on (e.g. "C:\System Volume Information")
  • 没有读权限的目录(例如。“C:\系统体积信息”)

#10


2  

I use this:

我用这个:

if not [%1] == [] (  pushd %~dpn1 2> nul  if errorlevel == 1 pushd %~dp1)

#11


2  

A very simple way is to check if the child exists.

一个非常简单的方法是检查子元素是否存在。

If a child does not have any child, the exist command will return false.

如果一个孩子没有任何孩子,存在的命令将返回false。

IF EXIST %1\. (  echo %1 is a folder) else (  echo %1 is a file)

You may have some false negative if you don't have sufficient access right (I have not tested it).

如果您没有足够的访问权限(我还没有测试过),您可能会有一些假阴性。

#12


1  

Based on this article titled "How can a batch file test existence of a directory" it's "not entirely reliable".

基于这篇题为“批处理文件如何测试目录的存在”的文章,它是“不完全可靠的”。

BUT I just tested this:

但我只是测试了一下

@echo offIF EXIST %1\NUL goto printECHO not dirpauseexit:printECHO It's a directorypause

and it seems to work

这似乎行得通

#13


1  

Here's my solution:

这是我的解决方案:

REM make sure ERRORLEVEL is 0TYPE NULREM try to PUSHD into the path (store current dir and switch to another one)PUSHD "insert path here..." >NUL 2>&1REM if ERRORLEVEL is still 0, it's most definitely a directoryIF %ERRORLEVEL% EQU 0 command...REM if needed/wanted, go back to previous directoryPOPD

#14


1  

If you can cd into it, it's a directory:

如果你可以cd到它,它是一个目录:

set cwd=%cd%cd /D "%1" 2> nul@IF %errorlevel%==0 GOTO endcd /D "%~dp1"@echo This is a file.@goto end2:end@echo This is a directory:end2@REM restore prior directory@cd %cwd%

#15


1  

I would like to post my own function script about this subject hope to be useful for someone one day.

我想把我自己的关于这个主题的函数脚本发布出来,希望有一天能对某人有用。

@pushd %~dp1@if not exist "%~nx1" (        popd        exit /b 0) else (        if exist "%~nx1\*" (                popd                exit /b 1        ) else (                popd                exit /b 3        ))

This batch script checks if file/folder is exist and if it is a file or a folder.

此批处理脚本检查文件/文件夹是否存在,以及它是文件还是文件夹。

Usage:

用法:

script.bat "PATH"

脚本。蝙蝠“路径”

Exit code(s):

退出代码(s):

0: file/folder doesn't exist.

0:文件/文件夹不存在。

1: exists, and it is a folder.

1:存在,它是一个文件夹。

3: exists, and it is a file.

3:存在,它是一个文件。

#16


0  

Under Windows 7 and XP, I can't get it to tell files vs. dirs on mapped drives. The following script:

在Windows 7和XP系统下,我无法让它识别映射驱动器上的文件和dirs。下面的脚本:

@echo offif exist c:\temp\data.csv echo data.csv is a fileif exist c:\temp\data.csv\ echo data.csv is a directoryif exist c:\temp\data.csv\nul echo data.csv is a directoryif exist k:\temp\nonexistent.txt echo nonexistent.txt is a fileif exist k:\temp\something.txt echo something.txt is a fileif exist k:\temp\something.txt\ echo something.txt is a directoryif exist k:\temp\something.txt\nul echo something.txt is a directory

produces:

生产:

data.csv is a filesomething.txt is a filesomething.txt is a directorysomething.txt is a directory

So beware if your script might be fed a mapped or UNC path. The pushd solution below seems to be the most foolproof.

因此,如果您的脚本可能被输入一个映射路径或UNC路径,请小心。下面的推式解决方案似乎是最简单的。

#17


0  

This is the code that I use in my BATCH files

这是我在批处理文件中使用的代码

```@echo offset param=%~1set tempfile=__temp__.txtdir /b/ad > %tempfile%set isfolder=falsefor /f "delims=" %%i in (temp.txt) do if /i  "%%i"=="%param%" set isfolder=truedel %tempfile%echo %isfolder%if %isfolder%==true echo %param% is a directory

```

' ' '

#18


0  

Ok... the 'nul' trick doesn't work if you use quotes in names, which accounts for most files with long file names or spaces.

好吧……如果您在名称中使用引号,那么“nul”技巧就不起作用,因为大多数文件的文件名或空格都很长。

For example,

例如,

if exist "C:\nul" echo Directory

does nothing, but

什么也不做,但

if exist C:\nul echo Directory

works.

的工作原理。

I finally came up with this, which seemed to work for all cases:

我终于想到了这个方法,它似乎适用于所有情况:

for /f %%i in ('DIR /A:D /B %~dp1 ^| findstr /X /c:"%~nx1"') do echo Directory

or if you can assure you meet all of the requirements for 'nul' a solution is:

或者,如果你能保证满足“nul”解决方案的所有要求,那就是:

if exist %~sf1\nul echo Directory

Put these into a batch file like 'test.bat' and do 'test.bat MyFile'.

将这些文件放入批处理文件中,如“test”。蝙蝠和做测试。蝙蝠MyFile”。

#19


0  

Here is my solution after many tests with if exist, pushd, dir /AD, etc...

这是我的解决方案,经过多次测试,如if存在、pushd、dir /AD等…

@echo offcd /d C:\for /f "delims=" %%I in ('dir /a /ogn /b') do (    call :isdir "%%I"    if errorlevel 1 (echo F: %%~fI) else echo D: %%~fI)cmd/k:isdirecho.%~a1 | findstr /b "d" >nulexit /b %errorlevel%:: Errorlevel:: 0 = folder:: 1 = file or item not found
  • It works with files that have no extension
  • 它可以处理没有扩展名的文件
  • It works with folders named folder.ext
  • 它与名为folder.ext的文件夹一起工作。
  • It works with UNC path
  • 它与UNC路径一起工作。
  • It works with double-quoted full path or with just the dirname or filename only.
  • 它使用双引号的完整路径,或者只使用dirname或文件名。
  • It works even if you don't have read permissions
  • 即使您没有读权限,它也可以工作
  • It works with Directory Links (Junctions).
  • 它与目录链接(结点)一起工作。
  • It works with files whose path contains a Directory Link.
  • 它与路径包含目录链接的文件一起工作。

#20


0  

One issue with using %%~si\NUL method is that there is the chance that it guesses wrong. Its possible to have a filename shorten to the wrong file. I don't think %%~si resolves the 8.3 filename, but guesses it, but using string manipulation to shorten the filepath. I believe if you have similar file paths it may not work.

使用%%~si\NUL方法的一个问题是,它有可能猜错了。文件名缩短为错误的文件是可能的。我不认为%%~si解析了8.3文件名,但是可以猜测一下,但是使用字符串操作来缩短文件路径。我相信如果你有相似的文件路径,它可能不会工作。

An alternative method:

另一种方法:

dir /AD %F% 2>&1 | findstr /C:"Not Found">NUL:&&(goto IsFile)||(goto IsDir):IsFile  echo %F% is a file  goto done:IsDir  echo %F% is a directory  goto done:done

You can replace (goto IsFile)||(goto IsDir) with other batch commands:
(echo Is a File)||(echo is a Directory)

您可以用其他批处理命令替换(goto IsFile)||(goto IsDir):(echo是一个文件)||(echo是一个目录)

#21


-2  

Can't we just test with this :

我们不能用这个来测试吗?

IF [%~x1] == [] ECHO Directory

It seems to work for me.

对我来说似乎行得通。

#1


43  

You can do it like so:

你可以这样做:

IF EXIST %VAR%\NUL ECHO It's a directory

However, this only works for directories without spaces in their names. When you add quotes round the variable to handle the spaces it will stop working. To handle directories with spaces, convert the filename to short 8.3 format as follows:

但是,这只适用于名称中没有空格的目录。当您在变量周围添加引号来处理空格时,它将停止工作。要处理带有空格的目录,将文件名转换为短8.3格式如下:

FOR %%i IN (%VAR%) DO IF EXIST %%~si\NUL ECHO It's a directory

The %%~si converts %%i to an 8.3 filename. To see all the other tricks you can perform with FOR variables enter HELP FOR at a command prompt.

%%~si将%%i转换为8.3文件名。要查看所有其他可以为变量执行的技巧,请在命令提示符处输入帮助。

(Note - the example given above is in the format to work in a batch file. To get it work on the command line, replace the %% with % in both places.)

(注意——上面给出的示例是在批处理文件中工作的格式。要使它在命令行上工作,请在两个地方用%替换%%。

#2


75  

This works:

如此:

if exist %1\* echo Directory

Works with directory names that contains spaces:

使用包含空格的目录名:

C:\>if exist "c:\Program Files\*" echo DirectoryDirectory

Note that the quotes are necessary if the directory contains spaces:

注意,如果目录包含空格,则必须使用引号:

C:\>if exist c:\Program Files\* echo Directory

Can also be expressed as:

也可表示为:

C:\>SET D="C:\Program Files"C:\>if exist %D%\* echo DirectoryDirectory

This is safe to try at home, kids!

这在家里是安全的,孩子们!

#3


42  

Recently failed with different approaches from the above. Quite sure they worked in the past, maybe related to dfs here. Now using the files attributes and cut first char

最近由于以上方法的不同而失败。很确定他们以前工作过,可能和dfs有关。现在使用文件属性和第一个char。

@echo offSETLOCAL ENABLEEXTENSIONSset ATTR=%~a1set DIRATTR=%ATTR:~0,1%if /I "%DIRATTR%"=="d" echo %1 is a folder:EOF

#4


11  

Further to my previous offering, I find this also works:

继我之前的出价之后,我发现这也适用:

if exist %1\ echo Directory

No quotes around %1 are needed because the caller will supply them.This saves one entire keystroke over my answer of a year ago ;-)

不需要在%1附近的引号,因为调用者将提供它们。这比我一年前的回答节省了整整一个按键;

#5


9  

Here's a script that uses FOR to build a fully qualified path, and then pushd to test whether the path is a directory. Notice how it works for paths with spaces, as well as network paths.

这里有一个脚本,用于构建完全限定的路径,然后pushd测试路径是否是目录。注意它是如何处理带空间的路径以及网络路径的路径的。

@echo offif [%1]==[] goto usagefor /f "delims=" %%i in ("%~1") do set MYPATH="%%~fi"pushd %MYPATH% 2>nulif errorlevel 1 goto notdirgoto isdir:notdirecho not a directorygoto exit:isdirpopdecho is a directorygoto exit:usageecho Usage:  %0 DIRECTORY_TO_TEST:exit

Sample output with the above saved as "isdir.bat":

上述样本输出以“is .bat”形式保存:

C:\>isdir c:\Windows\system32is a directoryC:\>isdir c:\Windows\system32\wow32.dllnot a directoryC:\>isdir c:\notadirnot a directoryC:\>isdir "C:\Documents and Settings"is a directoryC:\>isdir \is a directoryC:\>isdir \\ninja\SharedDocs\cpu-zis a directoryC:\>isdir \\ninja\SharedDocs\cpu-z\cpuz.ininot a directory

#6


6  

This works perfectly

这是完美的

if exist "%~1\" echo Directory

we need to use %~1 to remove quotes from %1, and add a backslash at end. Then put thw whole into qutes again.

我们需要使用%~1从%1中删除引号,并在末尾添加反斜杠。然后再把它放进qutes中。

#7


3  

The NUL technique seems to only work on 8.3 compliant file names.

(In other words, `D:\Documents and Settings` is "bad" and `D:\DOCUME~1` is "good")


I think there is some difficulty using the "NUL" tecnique when there are SPACES in the directory name, such as "Documents and Settings."

我认为当目录名中有空格时,使用“NUL”tecnique会有一些困难,比如“文档和设置”。

I am using Windows XP service pack 2 and launching the cmd prompt from %SystemRoot%\system32\cmd.exe

我正在使用Windows XP服务包2,并从%SystemRoot%\system32\cmd.exe启动cmd提示

Here are some examples of what DID NOT work and what DOES WORK for me:

以下是一些对我不起作用的和对我起作用的例子:

(These are all demonstrations done "live" at an interactive prompt. I figure that you should get things to work there before trying to debug them in a script.)

(这些都是在交互提示符下“实时”完成的演示。我认为,在尝试在脚本中调试之前,应该先让它们在那里工作。

This DID NOT work:

这并不工作:

D:\Documents and Settings>if exist "D:\Documents and Settings\NUL" echo yes

D:\文档和设置如果存在>“D:\文档和设置”echo yes

This DID NOT work:

这并不工作:

D:\Documents and Settings>if exist D:\Documents and Settings\NUL echo yes

D:\ document and Settings>if exist D:\Documents and Settings\NUL echo yes

This DOES work (for me):

这对我很有用:

D:\Documents and Settings>cd ..

D:\文档和设置> cd . .

D:\>REM get the short 8.3 name for the file

快速获取文件的8.3个简短名称

D:\>dir /x

D:\ > dir / x

Volume in drive D has no label.Volume Serial Number is 34BE-F9C9

驱动器D中的卷没有标签。卷序列号是34BE-F9C9

Directory of D:\
09/25/2008 05:09 PM <DIR> 2008
09/25/2008 05:14 PM <DIR> 200809~1.25 2008.09.25
09/23/2008 03:44 PM <DIR> BOOST_~3 boost_repo_working_copy
09/02/2008 02:13 PM 486,128 CHROME~1.EXE ChromeSetup.exe
02/14/2008 12:32 PM <DIR> cygwin

目录D:\ 09/25/2008 05:09 PM <目录> 2008 09/25/2008 05:14 PM <目录> 200809~1.25 2008.25 /2008 03:09 /23/2008 03:44 PM <目录> boost_3 boost_repo_working_copy 09/02/2008 02:02 /2008 02:13 PM 486,128 CHROME~1。EXE ChromeSetup。exe 02/14/2008 12:32 PM

cygwin。

[[Look right here !!!! ]]
09/25/2008 08:34 AM <DIR> DOCUME~1 Documents and Settings

[[看这里! ! ! ![英语背诵文选[hide] 1文档和设置

09/11/2008 01:57 PM 0 EMPTY_~1.TXT empty_testcopy_file.txt
01/21/2008 06:58 PM <DIR> NATION~1 National Instruments Downloads
10/12/2007 11:25 AM <DIR> NVIDIA
05/13/2008 09:42 AM <DIR> Office10
09/19/2008 11:08 AM <DIR> PROGRA~1 Program Files
12/02/1999 02:54 PM 24,576 setx.exe
09/15/2008 11:19 AM <DIR> TEMP
02/14/2008 12:26 PM <DIR> tmp
01/21/2008 07:05 PM <DIR> VXIPNP
09/23/2008 12:15 PM <DIR> WINDOWS
02/21/2008 03:49 PM <DIR> wx28
02/29/2008 01:47 PM <DIR> WXWIDG~2 wxWidgets
3 File(s) 510,704 bytes
20 Dir(s) 238,250,901,504 bytes free

09/11/2008 01:57 0空档~1。TXT empty_testcopy_file。 <目录> 国家~1国家仪器下载10/12/2007 11:25 AM <目录> NVIDIA 05/13/2008 09:42 AM <目录> Office10 09/19/2008 11:08 AM <目录> 程序文件12/02/1999 02:54 PM 24576 setx。exe 09/15/2008 11:19 AM

TEMP 02/14/2008 12:26 PM tmp 01/21/2008 07:05 PM VXIPNP 09/23/2008 12:15 PM WINDOWS 02/21/2008 PM > wx28 / 295 bytes

D:\>REM now use the \NUL test with the 8.3 name

现在使用8.3名称的\NUL测试

D:\>if exist d:\docume~1\NUL echo yes

D:\>如果存在D: docume~1\NUL echo yes

yes

是的

This works, but it's sort of silly, because the dot already implies i am in a directory:

这行得通,但有点傻,因为这个点已经暗示我在一个目录中:

D:\Documents and Settings>if exist .\NUL echo yes

D:如果存在的话,文件和设置>

#8


3  

This works and also handles paths with spaces in them:

它可以工作,也可以处理带有空格的路径:

dir "%DIR%" > NUL 2>&1if not errorlevel 1 (    echo Directory exists.) else (    echo Directory does not exist.)

Probably not the most efficient but easier to read than the other solutions in my opinion.

在我看来,这可能不是最有效但更容易阅读的解决方案。

#9


3  

A variation of @batchman61's approach (checking the Directory attribute).

@batchman61方法的变体(检查目录属性)。

This time I use an external 'find' command.

这次我使用了一个外部的“查找”命令。

(Oh, and note the && trick. This is to avoid the long boring IF ERRORLEVEL syntax.)

(哦,注意&&诀窍。这是为了避免冗长而无趣的错误级别语法。

@ECHO OFFSETLOCAL EnableExtentionsECHO.%~a1 | find "d" >NUL 2>NUL && (    ECHO %1 is a directory)

Outputs yes on:

输出是:

  • Directories.
  • 目录。
  • Directory symbolic links or junctions.
  • 目录符号链接或结点。
  • Broken directory symbolic links or junctions. (Doesn't try to resolve links.)
  • 损坏的目录符号链接或结点。(不要尝试解析链接。)
  • Directories which you have no read permission on (e.g. "C:\System Volume Information")
  • 没有读权限的目录(例如。“C:\系统体积信息”)

#10


2  

I use this:

我用这个:

if not [%1] == [] (  pushd %~dpn1 2> nul  if errorlevel == 1 pushd %~dp1)

#11


2  

A very simple way is to check if the child exists.

一个非常简单的方法是检查子元素是否存在。

If a child does not have any child, the exist command will return false.

如果一个孩子没有任何孩子,存在的命令将返回false。

IF EXIST %1\. (  echo %1 is a folder) else (  echo %1 is a file)

You may have some false negative if you don't have sufficient access right (I have not tested it).

如果您没有足够的访问权限(我还没有测试过),您可能会有一些假阴性。

#12


1  

Based on this article titled "How can a batch file test existence of a directory" it's "not entirely reliable".

基于这篇题为“批处理文件如何测试目录的存在”的文章,它是“不完全可靠的”。

BUT I just tested this:

但我只是测试了一下

@echo offIF EXIST %1\NUL goto printECHO not dirpauseexit:printECHO It's a directorypause

and it seems to work

这似乎行得通

#13


1  

Here's my solution:

这是我的解决方案:

REM make sure ERRORLEVEL is 0TYPE NULREM try to PUSHD into the path (store current dir and switch to another one)PUSHD "insert path here..." >NUL 2>&1REM if ERRORLEVEL is still 0, it's most definitely a directoryIF %ERRORLEVEL% EQU 0 command...REM if needed/wanted, go back to previous directoryPOPD

#14


1  

If you can cd into it, it's a directory:

如果你可以cd到它,它是一个目录:

set cwd=%cd%cd /D "%1" 2> nul@IF %errorlevel%==0 GOTO endcd /D "%~dp1"@echo This is a file.@goto end2:end@echo This is a directory:end2@REM restore prior directory@cd %cwd%

#15


1  

I would like to post my own function script about this subject hope to be useful for someone one day.

我想把我自己的关于这个主题的函数脚本发布出来,希望有一天能对某人有用。

@pushd %~dp1@if not exist "%~nx1" (        popd        exit /b 0) else (        if exist "%~nx1\*" (                popd                exit /b 1        ) else (                popd                exit /b 3        ))

This batch script checks if file/folder is exist and if it is a file or a folder.

此批处理脚本检查文件/文件夹是否存在,以及它是文件还是文件夹。

Usage:

用法:

script.bat "PATH"

脚本。蝙蝠“路径”

Exit code(s):

退出代码(s):

0: file/folder doesn't exist.

0:文件/文件夹不存在。

1: exists, and it is a folder.

1:存在,它是一个文件夹。

3: exists, and it is a file.

3:存在,它是一个文件。

#16


0  

Under Windows 7 and XP, I can't get it to tell files vs. dirs on mapped drives. The following script:

在Windows 7和XP系统下,我无法让它识别映射驱动器上的文件和dirs。下面的脚本:

@echo offif exist c:\temp\data.csv echo data.csv is a fileif exist c:\temp\data.csv\ echo data.csv is a directoryif exist c:\temp\data.csv\nul echo data.csv is a directoryif exist k:\temp\nonexistent.txt echo nonexistent.txt is a fileif exist k:\temp\something.txt echo something.txt is a fileif exist k:\temp\something.txt\ echo something.txt is a directoryif exist k:\temp\something.txt\nul echo something.txt is a directory

produces:

生产:

data.csv is a filesomething.txt is a filesomething.txt is a directorysomething.txt is a directory

So beware if your script might be fed a mapped or UNC path. The pushd solution below seems to be the most foolproof.

因此,如果您的脚本可能被输入一个映射路径或UNC路径,请小心。下面的推式解决方案似乎是最简单的。

#17


0  

This is the code that I use in my BATCH files

这是我在批处理文件中使用的代码

```@echo offset param=%~1set tempfile=__temp__.txtdir /b/ad > %tempfile%set isfolder=falsefor /f "delims=" %%i in (temp.txt) do if /i  "%%i"=="%param%" set isfolder=truedel %tempfile%echo %isfolder%if %isfolder%==true echo %param% is a directory

```

' ' '

#18


0  

Ok... the 'nul' trick doesn't work if you use quotes in names, which accounts for most files with long file names or spaces.

好吧……如果您在名称中使用引号,那么“nul”技巧就不起作用,因为大多数文件的文件名或空格都很长。

For example,

例如,

if exist "C:\nul" echo Directory

does nothing, but

什么也不做,但

if exist C:\nul echo Directory

works.

的工作原理。

I finally came up with this, which seemed to work for all cases:

我终于想到了这个方法,它似乎适用于所有情况:

for /f %%i in ('DIR /A:D /B %~dp1 ^| findstr /X /c:"%~nx1"') do echo Directory

or if you can assure you meet all of the requirements for 'nul' a solution is:

或者,如果你能保证满足“nul”解决方案的所有要求,那就是:

if exist %~sf1\nul echo Directory

Put these into a batch file like 'test.bat' and do 'test.bat MyFile'.

将这些文件放入批处理文件中,如“test”。蝙蝠和做测试。蝙蝠MyFile”。

#19


0  

Here is my solution after many tests with if exist, pushd, dir /AD, etc...

这是我的解决方案,经过多次测试,如if存在、pushd、dir /AD等…

@echo offcd /d C:\for /f "delims=" %%I in ('dir /a /ogn /b') do (    call :isdir "%%I"    if errorlevel 1 (echo F: %%~fI) else echo D: %%~fI)cmd/k:isdirecho.%~a1 | findstr /b "d" >nulexit /b %errorlevel%:: Errorlevel:: 0 = folder:: 1 = file or item not found
  • It works with files that have no extension
  • 它可以处理没有扩展名的文件
  • It works with folders named folder.ext
  • 它与名为folder.ext的文件夹一起工作。
  • It works with UNC path
  • 它与UNC路径一起工作。
  • It works with double-quoted full path or with just the dirname or filename only.
  • 它使用双引号的完整路径,或者只使用dirname或文件名。
  • It works even if you don't have read permissions
  • 即使您没有读权限,它也可以工作
  • It works with Directory Links (Junctions).
  • 它与目录链接(结点)一起工作。
  • It works with files whose path contains a Directory Link.
  • 它与路径包含目录链接的文件一起工作。

#20


0  

One issue with using %%~si\NUL method is that there is the chance that it guesses wrong. Its possible to have a filename shorten to the wrong file. I don't think %%~si resolves the 8.3 filename, but guesses it, but using string manipulation to shorten the filepath. I believe if you have similar file paths it may not work.

使用%%~si\NUL方法的一个问题是,它有可能猜错了。文件名缩短为错误的文件是可能的。我不认为%%~si解析了8.3文件名,但是可以猜测一下,但是使用字符串操作来缩短文件路径。我相信如果你有相似的文件路径,它可能不会工作。

An alternative method:

另一种方法:

dir /AD %F% 2>&1 | findstr /C:"Not Found">NUL:&&(goto IsFile)||(goto IsDir):IsFile  echo %F% is a file  goto done:IsDir  echo %F% is a directory  goto done:done

You can replace (goto IsFile)||(goto IsDir) with other batch commands:
(echo Is a File)||(echo is a Directory)

您可以用其他批处理命令替换(goto IsFile)||(goto IsDir):(echo是一个文件)||(echo是一个目录)

#21


-2  

Can't we just test with this :

我们不能用这个来测试吗?

IF [%~x1] == [] ECHO Directory

It seems to work for me.

对我来说似乎行得通。