Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 487 Vote(s) - 3.51 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to test if a file is a directory in a batch script?

#11
Can't we just test with this :

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

It seems to work for me.
Reply

#12
Here's my solution:

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
Reply

#13
Here is my solution after many tests with if exist, pushd, dir /AD, etc...

@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

- It works with files that have no extension
- It works with folders named folder.ext
- It works with UNC path
- It works with double-quoted full path or with just the dirname or filename only.
- It works even if you don't have read permissions
- It works with Directory Links (Junctions).
- It works with files whose path contains a Directory Link.




Reply

#14
This works and also handles paths with spaces in them:

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

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

Probably not the most efficient but easier to read than the other solutions in my opinion.
Reply

#15
If you can `cd` into it, it's a directory:

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%
Reply

#16
This works:

if exist %1\* echo Directory

Works with directory names that contains spaces:

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

Note that the quotes are necessary if the directory contains spaces:

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

Can also be expressed as:

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

This is safe to try at home, kids!
Reply

#17
You can do it like so:

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

However, this only works for directories without spaces in their names. When you add quotes round the variable to handle the spaces it will stop working. To handle directories with spaces, convert the filename to short 8.3 format as follows:

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

The `%%~si` converts `%%i` to an 8.3 filename. To see all the other tricks you can perform with `FOR` variables enter `HELP FOR` at a command prompt.

(Note - the example given above is in the format to work in a batch file. To get it work on the command line, replace the `%%` with `%` in both places.)
Reply

#18
One issue with using `%%~si\NUL` method is that there is the chance that it guesses wrong. Its possible to have a filename shorten to the wrong file. I don't think `%%~si` resolves the 8.3 filename, but guesses it, but using string manipulation to shorten the filepath. I believe if you have similar file paths it may not work.

An alternative method:

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

You can replace `(goto IsFile)||(goto IsDir)` with other batch commands:
`(echo Is a File)||(echo is a Directory)`
Reply

#19
Recently failed with different approaches from the above. Quite sure they worked in the past, maybe related to dfs here. Now using the [files attributes](

[To see links please register here]

) and cut first char

<!-- language: lang-dos -->

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





Reply

#20
I would like to post my own function script about this subject hope to be useful for someone one day.


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


This batch script checks if file/folder is exist and if it is a file or a folder.

**Usage:**

script.bat "PATH"

**Exit code(s):**

0: file/folder doesn't exist.

1: exists, and it is a folder.

3: exists, and it is a file.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through