是否有任何办法找出如果文件是一个目录?

我有该文件的名字在一个变量。在Perl我可以这样做:

if(-d $var) { print "it's a directory\n" }
有帮助吗?

解决方案

你可以这样做:

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

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

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

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

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

其他提示

这有效:

if exist %1\* echo Directory

使用包含空格的目录名:

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

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

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

也可以表示为:

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

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

最近因上述不同方法而失败。相当肯定他们过去工作过,也许与dfs相关。现在使用文件属性并剪切第一个字符

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

继我之前的发布之后,我发现这也有效:

if exist %1\ echo Directory

%1周围不需要引号,因为调用者将提供它们。 这比一年前的答案节省了一整个击键次数; - )

这是一个使用FOR构建完全限定路径的脚本,然后按下来测试路径是否是目录。请注意它如何适用于包含空格的路径以及网络路径。

@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

示例输出,上面保存为<!>“; 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

这完美无缺

if exist "%~1\" echo Directory

我们需要使用%~1来删除%1中的引号,并在结尾处添加反斜杠。然后再把全部放入qutes。

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

(换句话说,`D:\ Documents and Settings`是<!> quot; bad <!> quot;而`D:\ DOCUME~1`是<!> quot; good <!> quot;)


我认为使用<!>“NUL <!>”有一些困难; tecnique当目录名称中有SPACES时,例如<!> quot; Documents and Settings。<!> quot;

我正在使用Windows XP Service Pack 2并从%SystemRoot%\ system32 \ cmd.exe启动cmd提示

以下是一些不适用的例子以及对我有用的内容:

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

此DID不起作用:

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

此DID不起作用:

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

这项工作(对我而言):

D:\Documents and Settings>cd ..

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

D:\>dir /x

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

[[看这里!!!! ]]点击 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结果

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

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

yes

这很有效,但它有点傻,因为点已经暗示我在一个目录中:

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

这适用于处理带有空格的路径:

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

if not errorlevel 1 (
    echo Directory exists.
) else (
    echo Directory does not exist.
)

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

一个变化@batchman61的做法(检查的目录属性)。

这一次我使用一个外部'发现'的命令。

(噢,并注意 && 伎俩。这是为了避免长无聊的 IF ERRORLEVEL 语法。)

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

产出是:

  • 目录。
  • 目录象征性的联系或连接。
  • 破碎的 目录象征性的联系或连接。(并不试图解决的链接。)
  • 目录,你有没有读的权限(例如"C:\System 量的信息")

我用这个:

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

一种非常简单的方法是检查孩子是否存在。

如果孩子没有孩子,exist命令将返回false。

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

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

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

但我刚试过这个:

@echo off
IF EXIST %1\NUL goto print
ECHO not dir
pause
exit
:print
ECHO It's a directory
pause

似乎有效

这是我的解决方案:

REM make sure ERRORLEVEL is 0
TYPE NUL

REM try to PUSHD into the path (store current dir and switch to another one)
PUSHD "insert path here..." >NUL 2>&1

REM if ERRORLEVEL is still 0, it's most definitely a directory
IF %ERRORLEVEL% EQU 0 command...

REM if needed/wanted, go back to previous directory
POPD

如果你可以cd进入它,那就是一个目录:

set cwd=%cd%

cd /D "%1" 2> nul
@IF %errorlevel%==0 GOTO end

cd /D "%~dp1"
@echo This is a file.

@goto end2
:end
@echo This is a directory
:end2

@REM restore prior directory
@cd %cwd%

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

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

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

<强>用法:

script.bat <!> quot; PATH <!> quot;

退出代码:

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

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

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

当指定的目录不存在时,

CD返回EXIT_FAILURE。并且您获得了 条件处理符号 ,因此您可以执行以下操作。

SET cd_backup=%cd%
(CD "%~1" && CD %cd_backup%) || GOTO Error

:Error
CD %cd_backup%

在Windows 7和XP下,我无法让它在映射的驱动器上告诉文件与dirs。以下脚本:

@echo off
if exist c:\temp\data.csv echo data.csv is a file
if exist c:\temp\data.csv\ echo data.csv is a directory
if exist c:\temp\data.csv\nul echo data.csv is a directory
if exist k:\temp\nonexistent.txt echo nonexistent.txt is a file
if exist k:\temp\something.txt echo something.txt is a file
if exist k:\temp\something.txt\ echo something.txt is a directory
if exist k:\temp\something.txt\nul echo something.txt is a directory

产生

data.csv is a file
something.txt is a file
something.txt is a directory
something.txt is a directory

因此,请注意您的脚本是否可能被提供映射或UNC路径。下面的推动解决方案似乎是最简单的。

这是我在BATCH文件中使用的代码

```
@echo off
set param=%~1
set tempfile=__temp__.txt
dir /b/ad > %tempfile%
set isfolder=false
for /f "delims=" %%i in (temp.txt) do if /i  "%%i"=="%param%" set isfolder=true
del %tempfile%
echo %isfolder%
if %isfolder%==true echo %param% is a directory

```

好的......如果在名称中使用引号,那么'nul'技巧就不起作用了,因为大多数文件都是长文件名或空格。

例如,

if exist "C:\nul" echo Directory

什么都不做,但是

if exist C:\nul echo Directory

作品。

我终于想出了这个,这似乎适用于所有情况:

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

或者如果您能确保满足'nul'的所有要求,则解决方案是:

if exist %~sf1\nul echo Directory

将这些文件放入像'test.bat'这样的批处理文件中,并执行'test.bat MyFile'。

这里是我的解决方案后,许多试验,如果存在,on,dir/广告,等等。

@echo off
cd /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

:isdir
echo.%~a1 | findstr /b "d" >nul
exit /b %errorlevel%

:: Errorlevel
:: 0 = folder
:: 1 = file or item not found
  • 它的工作原理与文件有没有扩展
  • 它适用文件夹,名为文件夹。ext
  • 它的工作与UNC路径
  • 它与双引用完整的路径或只是dirname或文件只。
  • 它的工作,甚至如果你没有读的权限
  • 它的工作与目录链接(结).
  • 它的工作原理与文件的路径包含一个目录的链接。

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

另一种方法:

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

您可以将(goto IsFile)||(goto IsDir)替换为其他批处理命令:
(echo Is a File)||(echo is a Directory)

我们不能只用这个来测试:

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

这似乎对我有用。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top