extract file name extension from environment var

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Contact:

extract file name extension from environment var

Post by MigrationUser »

13 Jul 2011 14:10
adealey


I know how to extract a file name extension from a parameter (%~x1) but is there some way to apply this function to an environment variable? I swear I've seen something somewhere about doing that but nothing I have tried has worked.

----------------------------

#2 13 Jul 2011 15:22
RG


Something like this will do it:

Code: Select all

@ECHO OFF
CALL :GetExt xxxx.bat
ECHO.%Ret%
pause
GOTO :eof


:GetExt
REM %1=FileName%
SET Ret=%~x1
SET Ret=%Ret:~1% & REM Only include this line if you want to get rid of the .
GOTO :eof
Windows Shell Scripting and InstallShield
----------------------------

#3 13 Jul 2011 16:44
flabdablet


Closest thing I know of to a neat inline way to do this is using for:

Code: Select all

set file=C:\Program Files\Fubar\fubar.exe
for %%F in ("%file%") do echo Drive=%%~dF Path=%%~pF Name=%%~nF Ext=%%~xF
Post Reply