ZTree.com  | ZEN  | About...  

 Index   Back

[Help!] batch - how to find if it is a Drive or Dir or File   [Help!]

By: Ron Metzger       
Date: May 19,2020 at 18:02
In Response to: [Help!] batch - how to find if it is a Drive or Dir or File (Ryan)

> ::how to find if it is a Drive or
> Dir or File
>
> method 1=
>
> IF "%3"=="\" IF "%4"=="" GOTO :DoDrive
> SET ZTMode=File
> REM #ZTIfDir SET ZTMode=Dir
> IF [%%ZTMode%%]==[File] GOTO :DoFile
> IF [%%ZTMode%%]==[Dir] GOTO :DoDir
> GOTO :END
>
> -------------------------------------
>
> method 2=
>
> IF "%3"=="\" IF "%4"=="" (
> SET P1=Adrive
> SET P2=Bdrive
> SET P3=%2
> )
> GOTO :MODE
>
> SET ZTMode=File
> REM #ZTIfDir SET ZTMode=Dir
> IF [%%ZTMode%%]==[File] (
> SET P1=Adir
> SET P2=Bdir
> SET P3=%1
> )
> IF [%%ZTMode%%]==[Dir] (
> SET P1=Afile
> SET P2=Bfile
> SET P3=%1
> )
> GOTO :MODE
>
> REM ------------------------------
> :MODE
>
> ECHO.
> ECHO %P1%
> ECHO %P2%
> ECHO %P3%
> ECHO.
> PAUSE
>
>
> i can do this with method 1, but it has too many GOTO's
> i like to know how i can do with method 2. is there a way?
> i am stuck with this line IF [%%ZTMode%%]==[File],it wont take multiple
> lines.
>

Hi Ryan,

Another approach exists within a Batch file to extract drive, path, filename, and Extension that has existed since at least Windows 2000, maybe Windows NT SP6 (though I have not tried this on NT in many many years).

I have listed an example of the batch file below. For simplicity, I simply made a set environment variable for each option. Note, that many combinations are available. My results do NOT put Quotation Marks around values, so properly Quotate in use to protect against spaces existing in filenames or paths.

Note: When calling the :SplitSpec subroutine, %~1 is split up for you. However, cmd.exe does not Test for the real existence of the specified filespec passed. It doesn't even check if it is a File or Path, ignoring a trailing \ character.

I have not tried this within the F9 menu system. Not sure if any of this works there. Possibly use :SplitSpec to set vars as needed for you use.

Let us know what does or doesn't work for you,

Thanks, stay safe,

Ron Metzger

ex. bat
@echo off

rem Set fully qualified Drive:\Path\File.ext of current batch file
set "_dpf0=%~dpf0"

rem Set fully qualified Drive:\Path\ of current batch file
set "_dp0=%~dp0"

rem Set File.Ext of current batch file
set "_nx0=%~nx0"

rem Set Drive: of current batch file
set "_d0=%~d0"

rem Set Filename only of current batch file
set "_n0=%~n0"

rem Set .Ext Only of current batch file
set "_x0=%~x0"

set _d
set _n
set _x
pause

call :SplitSpec "X:\My path\subpath\whatever\a file.txt"
call :SplitSpec "X:\My path\subpath\whatever\a file.txt\"

goto :eof

:SplitSpec
rem Set fully qualified Drive:\Path\File.ext of %~1
set "_dpf1=%~dpf1"

rem Set fully qualified Drive:\Path\ of %~1
set "_dp1=%~dp1"

rem Set File.Ext of %~1
set "_nx1=%~nx1"

rem Set Drive: of %~1
set "_d1=%~d1"

rem Set Filename only of %~1
set "_n1=%~n1"

rem Set .Ext Only of %~1
set "_x1=%~x1"

set _d
set _n
set _x
pause
goto :eof

226 views      
 

Messages in this Thread

 
95,114 Postings in 11,994 Threads, 350 registered users, 80 users online (0 registered, 80 guests)
Index | Admin contact |   Forum Time: Oct 31, 2020 - 1:56 am UTC  |  Hits:35,026,853  (3,424 Today )
RSS Feed