Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

@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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top