#1 24 Mar 2021 21:26

Marceepoo
New Member
Registered: 01 Apr 2019
Posts: 2

Is it possible to parse a file's path created by the set cmd

If I assign a file's full path (e.g, "c:\mydir\text01.txt") to _Fullname using the SET command, is there a way to extract from %_Fullname%:
1.  The file's bare name:           text01     ?
2.  The file's extension:            txt           ?
3.  The file's directory path:     c:\mydir   ?
4.  The file's parent directory:  mydir       ?

I understand that it's possible to do that with a var created from an argument, as indicated on the ss64's CMD page explaining syntax-args.
But I don't think that those rules apply to a var created by the set command.
Am I mistaken?

Any suggestions about how to parse the path into elements (1 through 4, as itemized above) would be much appreciated.

Thanks,

                    Marc

Offline

#2 25 Mar 2021 00:12

RG
Member
From: Minnesota
Registered: 18 Feb 2010
Posts: 362

Re: Is it possible to parse a file's path created by the set cmd

Something like this?

@echo off
set "MyPath=c:\mydir\text01.txt"
call :ExtractParts "%MyPath%
pause
:goto :eof

:ExtractParts
rem 1=filespec
echo(FileSpec=%~1
set "FileName=%~n1"
set "Extension=%~x1"
set "Extension=%Extension:.=%" & rem remove .
set "DirPath=%~dp1"
set "DirPath=%DirPath:~0,-1%" & rem remove trailing \
set "ParentDir=%~p1
set "ParentDir=%ParentDir:\=%" & rem remove leading and trailing \
echo(FileName=%FileName% 
echo(Extension=%Extension% 
echo(DirPath=%DirPath% 
echo(ParentDir=%ParentDir% 
goto :eof
:: end of :ExtractParts

Windows Shell Scripting and InstallShield

Offline

Board footer

Powered by FluxBB