1

Thanks to other posts here, I know how to make a batch file parse a dropped file into a path and a filename, like this:

@echo off
setlocal EnableDelayedExpansion
FOR %%a IN (%*) DO (
    if exist %%a (
        set DRVPATH="%%~dpa"
        set FILEEXT="%%~nxa"
    )
)
echo DRVPATH: %DRVPATH%
echo FILEEXT: %FILEEXT%

This works perfectly if I drop a file on the batch file. But if I drop a directory onto it, then it interprets the name of the directory as the filename (my %FILEEXT% variable).

Is it possible to make the batch file test whether %FILEEXT% is actually a directory? If I drop a directory on the batch file, I want the %DRVPATH% variable to contain the full path including the directory name, and I want the %FILEEXT% variable to be empty.

Any help in sorting this out will be gratefully received.

1
  • Pushd returns errorlevel of 9009 if directory doesn't exist, 1 if it exists, and 0 if it's a file. See pushd /?, popd /?, and if /? (if errorlevel 0 if not errorlevel 1). So pushd, do your ifs, then popd. Commented Apr 21, 2014 at 0:20

2 Answers 2

2

Try like this:

@echo off
FOR %%a IN (%*) DO (
    if exist %%a\ (
        set DRVPATH="%%~fa"
        set FILEEXT=NIL
    ) else (
        if exist %%a (
            set DRVPATH="%%~pa"
            set FILEEXT="%%~nxa"
        )
    )
)

echo DRVPATH: %DRVPATH%
echo FILEEXT: %FILEEXT%
pause

If a directory with the name you drop exist then the var %FILEEXT% is set to NIL.

Sign up to request clarification or add additional context in comments.

2 Comments

You cannot have a directory that has the same name as a file in any given folder. The filesystem won't allow it.
This works perfectly. Thank you! And tony bd's solution works perfectly also. Thank you both.
-1
@Echo off
pushd %1 >nul 2>&1  
If errorlevel 0 if not errorlevel 1 Echo %~nx1 is a folder
If errorlevel 1 Echo %~nx1 is not a folder
Popd

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.