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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/138981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:25:11  来源:igfitidea点击:

How to test if a file is a directory in a batch script?

windowsbatch-filecommand-line

提问by Vhaerun

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" }

采纳答案by Dave Webb

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 %%~siconverts %%ito an 8.3 filename. To see all the other tricks you can perform with FORvariables enter HELP FORat a command prompt.

%%~si转换%%i到一个8.3文件名。要查看您可以使用FOR变量HELP FOR在命令提示符下输入的所有其他技巧。

(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.)

(注-上面给出的例子是在一个批处理文件格式,以工作来获取它的命令行上工作,则更换。%%%在两个地方。)

回答by Gerard

This works:

这有效:

if exist %1\* echo Directory

Works with directory names that contains spaces:

适用于包含空格的目录名称:

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

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

This is safe to try at home, kids!

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

回答by batchman61

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

最近以与上述不同的方法失败。很确定他们过去工作过,可能与这里的 dfs 有关。现在使用文件属性并剪切第一个字符

@echo off
SETLOCAL ENABLEEXTENSIONS
set ATTR=%~a1
set DIRATTR=%ATTR:~0,1%
if /I "%DIRATTR%"=="d" echo %1 is a folder
:EOF

回答by Gerard

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 周围不需要引号,因为调用者会提供它们。这比我一年前的回答节省了一个完整的击键 ;-)

回答by indiv

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.

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

@echo off
if [%1]==[] goto usage

for /f "delims=" %%i in ("%~1") do set MYPATH="%%~fi"
pushd %MYPATH% 2>nul
if errorlevel 1 goto notdir
goto isdir

:notdir
echo not a directory
goto exit

:isdir
popd
echo is a directory
goto exit

:usage
echo Usage:  %0 DIRECTORY_TO_TEST

:exit

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

上面保存为“isdir.bat”的示例输出:

C:\>isdir c:\Windows\system32
is a directory

C:\>isdir c:\Windows\system32\wow32.dll
not a directory

C:\>isdir c:\notadir
not a directory

C:\>isdir "C:\Documents and Settings"
is a directory

C:\>isdir \
is a directory

C:\>isdir \ninja\SharedDocs\cpu-z
is a directory

C:\>isdir \ninja\SharedDocs\cpu-z\cpuz.ini
not a directory

回答by Aziz

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 中。

回答by Explorer09

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

@batchman61 方法的一种变体(检查 Directory 属性)。

This time I use an external 'find' command.

这次我使用外部“查找”命令。

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

(哦,注意&&技巧。这是为了避免冗长乏味的IF ERRORLEVEL语法。)

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

Outputs yes on:

输出是:

  • Directories.
  • Directory symbolic links or junctions.
  • Brokendirectory 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:\System Volume Information”)

回答by Samuel

This works and also handles paths with spaces in them:

这有效并且还处理其中包含空格的路径:

dir "%DIR%" > NUL 2>&1

if 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.

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

回答by pestophagous

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

NUL 技术似乎只适用于 8.3 兼容的文件名。

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

(换句话说,`D:\Documents and Settings` 是“坏的”而`D:\DOCUME~1` 是“好的”)



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

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

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

我正在使用 Windows XP service pack 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:\Documents and Settings>if exist "D:\Documents and Settings\NUL" echo yes

This DID NOT work:

这不起作用:

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

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

This DOES work (for me):

这确实有效(对我而言):

D:\Documents and Settings>cd ..

D:\Documents and Settings>cd ..

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

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

D:\>dir /x

D:\>dir /x

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

Volume in drive D has no label.Volume Serial Number is 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

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

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

[[看这里!!!!]]
09/25/2008 08:34 AM <DIR> DOCUME~1 Documents and Settings

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

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

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

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

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

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:\Documents and Settings>if exist .\NUL echo yes

回答by Kimae

I use this:

我用这个:

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