1. 程式人生 > 其它 >批處理如何判斷路徑是檔案,還是資料夾

批處理如何判斷路徑是檔案,還是資料夾

因工作需要,需要判斷引數傳遞過來的路徑是資料夾還是檔案,於是從網上找來一些,供大家參考吧

版本1

@echo off
if exist "C:\1" (dir /ad/b "C:\1" 2>nul&&set a=0||set a=1)
echo %a%
pause

版本2

for %%a in (C:\1) do set "b=%%~aa"
if defined b (
if %b:~0,1%==d (set a=1 ) else ( set a=0) )

說明:得到C:\1的屬性,然後判斷屬性首位元組是否為d,是為資料夾,否為檔案,如果變數a沒被賦值,則沒有這個檔案(夾)

版本3

@echo off
cd C:\1 2>nul
if %ERRORLEVEL% ==1 (set a=1) else (set a=0)
echo %a%
pause

版本4

@echo off
if exist c:\1 if exist c:\1\nul echo c:\1 is a folder.
if exist c:\1 if not exist c:\1\nul echo c:\1 is a file.

出處:https://zhidao.baidu.com/question/303954579943369244.html

=======================================================================================

版本5

REM 判斷要複製的是檔案還是目錄
FOR %%i IN ("%FileName%") DO SET FileAttrib=%%~ai
IF %FileAttrib:~0,1%==d (
GOTO COPYDIR
) ELSE (
GOTO COPYFILE
)

版本6

@echo off
    for /f "delims=" %%i in ('dir /a/b/s') do pushd "%%i" 2>nul && (call :folder "%%i" & popd) || call :file "%%i"
    pause
goto :eof

:file
echo %~1 是檔案 goto :eof :folder echo %~1 是目錄 goto :eof

pushd %%i 2>nul && (echo 目錄 & popd )|| echo 檔案
或者:
pushd %%i 2>nul
if %errorlevel% == 1 (echo 檔案
)else(echo 目錄)
大概就是這樣吧,具體的語法不大記得了,不知道上面的能不能正常執行。
另外可以通過dir /ad/b/s來獲得所有目錄,dir /a-d/b/s來獲得所有非目錄。
個人認為在dir裡面區別對待檔案和目錄是提高效率的做法。

出處:https://bbs.csdn.net/topics/360126535

=======================================================================================

版本7

set arg=c:\ttt
if exist "%arg%\.\" echo yes

出處:https://tieba.baidu.com/p/1204278138

=======================================================================================

使用批處理檢查路徑是“檔案"還是“資料夾"(Check if the path is File or Folder using batch)

我正在嘗試使用批處理檔案檢查程式中定義的路徑是檔案還是資料夾.一切正常,但是當我嘗試提供的路徑不是檔案或資料夾或沒有訪問許可權時,它會顯示"這是一個檔案".

這是程式碼.

@ECHO off
SETLOCAL ENABLEEXTENSIONS
set ATTR=D:\Download\Documents\New


dir /AD "%ATTR%" 2>&1 | findstr /C:"Not Found">NUL:&&(goto IsFile)||(goto IsDir)

:IsFile
  echo %ATTR% is a file
  goto done

:IsDir
  echo %ATTR% is a directory
  goto done

:done
解決方案

我建議使用以下方法:

 @Echo Off
Set "ATTR=D:\Download\Documents\New"
For %%Z In ("%ATTR%") Do If "%%~aZ" GEq "d" (Echo Directory
) Else If "%%~aZ" GEq "-" (Echo File) Else Echo Inaccessible
Pause
 

I am trying to check if the path defined in the program is file or a folder using batch file. Everything is working fine but when I try to give a path that isn't file or folder or doesn't have permission to access it, it gives output saying "it is a File".

Here is the code.

@ECHO off
SETLOCAL ENABLEEXTENSIONS
set ATTR=D:\Download\Documents\New


dir /AD "%ATTR%" 2>&1 | findstr /C:"Not Found">NUL:&&(goto IsFile)||(goto IsDir)

:IsFile
  echo %ATTR% is a file
  goto done

:IsDir
  echo %ATTR% is a directory
  goto done

:done
解決方案

I would suggest the following method:

@Echo Off
Set "ATTR=D:\Download\Documents\New"
For %%Z In ("%ATTR%") Do If "%%~aZ" GEq "d" (Echo Directory
) Else If "%%~aZ" GEq "-" (Echo File) Else Echo Inaccessible
Pause

出處:https://www.it1352.com/1966715.html

=======================================================================================

我推薦使用版本7,比較簡單,其他的各個版本,大家自行選擇吧

您的資助是我最大的動力!
金額隨意,歡迎來賞!
款後有任何問題請給我留言。

如果,您認為閱讀這篇部落格讓您有些收穫,不妨點選一下右下角的推薦按鈕。
如果,您希望更容易地發現我的新部落格,不妨點選一下綠色通道的關注我。(●'◡'●)

如果你覺得本篇文章對你有所幫助,請給予我更多的鼓勵,求打 付款後有任何問題請給我留言!!!

因為,我的寫作熱情也離不開您的肯定支援,感謝您的閱讀,我是【Jack_孟】!