如何测试一个文件是在一个批处理脚本的目录?(How to test if a file is a d

2019-07-05 10:35发布

有什么办法找出如果一个文件是目录?

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

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

Answer 1:

你可以做到这一点,如下所示:

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 ,在命令提示符。

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



Answer 2:

这工作:

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

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



Answer 3:

日前未能与上述不同的方法。 肯定他们在过去的工作,可能与这里的DFS。 现在使用的文件的属性和切割的第一字符

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


Answer 4:

继我以前的产品,我觉得这也可以工作:

if exist %1\ echo Directory

需要大约%1没有引号,因为主叫方将提供给他们。 这节省了一个完整的按键了我一年前的答案;-)



Answer 5:

下面是一个用来建立一个完全合格的路径,然后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

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


Answer 6:

这工作完全

if exist "%~1\" echo Directory

我们需要使用%〜1%1除去报价,并在结尾处添加一个反斜杠。 然后把THW整成qutes一次。



Answer 7:

该NUL技术似乎只在8.3兼容的文件名的工作。

(换句话说,'d:\ Documents和Settings`是 “坏” 和'd:\ DOCUME〜1`是 “好”)


我认为这是使用时有空格的目录名,如“NUL” tecnique一些困难“文件和设置。”

我使用Windows XP Service Pack 2和发射距离的%SystemRoot%\ SYSTEM32 \ CMD.EXE的命令提示符

下面是一些例子什么也没有工作,什么不工作对我来说:

(这些都是示威做“活”在互动提示。我想,你应该得到的东西来试图调试他们的脚本之前,在那里工作。)

这并不工作:

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

这并不工作(对我来说):

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



Answer 8:

这个工程还负责与空格的路径:

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

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

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



Answer 9:

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

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

(呵呵,注意&&把戏。这是为了避免长时间枯燥的IF ERRORLEVEL语法)。

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

输出是在:

  • 目录。
  • 目录的符号链接或路口。
  • 残破的目录的符号链接或路口。 (不设法解决的链接。)
  • 你对(如“:\系统卷信息C”)没有读取权限目录


Answer 10:

我用这个:

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


Answer 11:

一个非常简单的方法是检查,如果孩子存在。

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

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

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



Answer 12:

根据这篇文章题为“哪有一个批处理文件测试存在一个目录的”这“并不完全可靠。”

但我只是测试这一点:

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

它似乎工作



Answer 13:

这里是我的解决方案:

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


Answer 14:

如果你能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%


Answer 15:

我想发表我自己的函数脚本关于这个问题希望能有人1天有用。

@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 “PATH”

退出代码(S):

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

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

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



Answer 16:

CD返回EXIT_FAILURE时指定的目录不存在。 而你得到的条件处理符号 ,所以你可以为这个不喜欢的下方。

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

:Error
CD %cd_backup%


Answer 17:

在Windows 7和XP,我不能让它告诉在映射驱动器的文件与迪尔斯。 下面的脚本:

@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路径。 下面的PUSHD解决方案似乎是最万无一失。



Answer 18:

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

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

```



Answer 19:

好吧...如果你在名字中使用引号,占长文件名或空格大多数文件的“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的”批处理文件。



Answer 20:

这是经过多次试验我的解决方案与是否存在,PUSHD,DIR / AD,等...

@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
  • 它的工作原理与没有扩展名的文件
  • 它的工作原理与folder.ext命名的文件夹
  • 它的工作原理与UNC路径
  • 它与双引号的完整路径,或者仅仅只有目录名或文件名。
  • 它的工作原理,即使你没有读取权限
  • 它的工作原理与目录链接(路口)。
  • 它与文件的路径包含目录链接。


Answer 21:

用一个问题%%~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)



Answer 22:

我们不能只用这个测试:

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

这似乎为我工作。



文章来源: How to test if a file is a directory in a batch script?