批處理如何判斷路徑是文件,還是文件夾


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

版本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,比較簡單,其他的各個版本,大家自行選擇吧


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM