有什麼方法可以找出一個文件是否是一個目錄?如何測試文件是批處理腳本中的目錄?
我在變量中有文件名。在Perl我可以這樣做:
if(-d $var) { print "it's a directory\n" }
有什麼方法可以找出一個文件是否是一個目錄?如何測試文件是批處理腳本中的目錄?
我在變量中有文件名。在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
。
(注 - 。上面給出的例子是在一個批處理文件工作的格式要得到它的命令行上工作,在這兩個地方%
更換%%
)基於this article題爲
「一個批處理文件如何測試一個目錄「它」的存在並不完全可靠「。
,但我只是測試了這一點:
@echo off
IF EXIST %1\NUL goto print
ECHO not dir
pause
exit
:print
ECHO It's a directory
pause
,它似乎工作
閱讀文章「不可靠」意味着它在Pathworks,Novell,DR-DOS和OS/2上失敗,我懷疑這對大多數人來說是一個問題。 – 2008-09-26 12:46:35
因此「不完全可靠」 – 2008-09-26 12:54:08
**這不符合** [目錄符號鏈接(Softlinks)或連接](http://schinagl.priv.at/nt/hardlinkshellext/hardlinkshellext.html):其路徑包含目錄鏈接被誤認爲目錄。要重現,請輸入:
我認爲這是使用一些困難「NUL」tecnique當目錄名稱中有SPACES時,如「Documents and Settings」。
我使用Windows XP Service Pack 2和啓動從的%SystemRoot CMD提示符%\ 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
下面是一個用來建立一個完全合格的路徑的腳本,然後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
這工作:
if exist %1\* echo Directory
與目錄名工程包含空格:
C:\>if exist "c:\Program Files\*" echo Directory
Directory
請注意,引號是ne cessary如果該目錄包含空格:
C:\>if exist c:\Program Files\* echo Directory
也可表示爲:
C:\>SET D="C:\Program Files"
C:\>if exist %D%\* echo Directory
Directory
這是安全的嘗試在家裏,孩子們!
使用%%~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 not [%1] == [] (
pushd %~dpn1 2> nul
if errorlevel == 1 pushd %~dp1
)
日前未能與上述不同的方法。相當肯定他們過去工作過,也許在這裏與dfs有關。現在使用files attributes切第一個字符
@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號引號,因爲主叫方將提供給他們。 這節省了一年前我的答案一個完整的擊鍵;-)
在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路徑。下面推動的解決方案似乎是最簡單的。
這工作完全
if exist "%~1\" echo Directory
我們需要使用%〜1%1除去報價,並在結尾處添加一個反斜槓。然後再把這個整體放入分組中。
這是我在我的批處理文件
```
@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
```
一個非常簡單的方法是檢查是否存在兒童使用的代碼。
如果孩子沒有任何孩子,exist
命令將返回false。
IF EXIST %1\. (
echo %1 is a folder
) else (
echo %1 is a file
)
如果您沒有足夠的訪問權限(我沒有測試過),您可能會有一些錯誤的否定。
我們不能只用這個測試:
IF [%~x1] == [] ECHO Directory
這似乎爲我工作。
這裏是我的解決方案:
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
好......如果您在名稱中使用引號的「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'。
這裏是經過多次試驗我的解決方案與是否存在,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
這個工程還負責在他們的空間的路徑:
dir "%DIR%" > NUL 2>&1
if not errorlevel 1 (
echo Directory exists.
) else (
echo Directory does not exist.
)
可能不是最有效的,但更容易比在我看來,其他的解決方案閱讀。
@ batchman61方法的變體(檢查目錄屬性)。
這次我使用外部'find'命令。
(哦,並注意&&
訣竅這是爲了避免長時間枯燥的語法IF ERRORLEVEL
)上
@ECHO OFF
SETLOCAL EnableExtentions
ECHO.%~a1 | find "d" >NUL 2>NUL && (
ECHO %1 is a directory
)
輸出有:
如果你能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 「PATH」
退出代碼(一個或多個):
0:文件/文件夾不存在。
1:存在,它是一個文件夾。
3:存在,它是一個文件。
你忘了d旗我認爲 – 2008-09-26 12:05:41
這是行不通的。 – Vhaerun 2008-09-26 12:17:27
它適用於我和什麼「D」標誌?羅馬語 - 答案中的「D:」是機器上「D:」驅動器的參考。 Vhaerun - 當你嘗試這個時,你有什麼VAR設置? – 2008-09-26 13:04:32