How do I split the filename out of a full path in batch scripting?
For simple batch files, a single character such as %%f will work. You can use multiple values for variable in complex batch files to distinguish different replaceable variables.
%%a refers to the name of the variable your for loop will write to. Quoted from for /? : FOR %variable IN (set) DO command [command-parameters] %variable Specifies a single letter replaceable parameter. (set) Specifies a set of one or more files. Wildcards may be used.
batch-file Echo @Echo off @echo off prevents the prompt and contents of the batch file from being displayed, so that only the output is visible. The @ makes the output of the echo off command hidden as well.
@echo off Set filename=C:\Documents and Settings\All Users\Desktop\Dostips.cmd For %%A in ("%filename%") do ( Set Folder=%%~dpA Set Name=%%~nxA ) echo.Folder is: %Folder% echo.Name is: %Name%
But I can't take credit for this; Google found this at http://www.dostips.com/forum/viewtopic.php?f=3&t=409
Parse a filename from the fully qualified path name (e.g., c:\temp\my.bat) to any component (e.g., File.ext).
Single line of code:
For %%A in ("C:\Folder1\Folder2\File.ext") do (echo %%~fA)
You can change out "C:\Folder1\Folder2\File.ext" for any full path and change "%%~fA" for any of the other options you will find by running "for /?" at the command prompt.
Elaborated Code
set "filename=C:\Folder1\Folder2\File.ext" For %%A in ("%filename%") do ( echo full path: %%~fA echo drive: %%~dA echo path: %%~pA echo file name only: %%~nA echo extension only: %%~xA echo expanded path with short names: %%~sA echo attributes: %%~aA echo date and time: %%~tA echo size: %%~zA echo drive + path: %%~dpA echo name.ext: %%~nxA echo full path + short name: %%~fsA)
Standalone Batch Script
Save as C:\cmd\ParseFn.cmd.
Add C:\cmd to your PATH environment variable and use it to store all of you reusable batch scripts.
@echo off @echo ::___________________________________________________________________:: @echo :: :: @echo :: ParseFn :: @echo :: :: @echo :: Chris Advena :: @echo ::___________________________________________________________________:: @echo. :: :: Process arguements :: if "%~1%"=="/?" goto help if "%~1%"=="" goto help if "%~2%"=="/?" goto help if "%~2%"=="" ( echo !!! Error: ParseFn requires two inputs. !!! goto help) set in=%~1% set out=%~2% :: echo "%in:~3,1%" "%in:~0,1%" if "%in:~3,1%"=="" ( if "%in:~0,1%"=="/" ( set in=%~2% set out=%~1%) ) :: :: Parse filename :: set "ret=" For %%A in ("%in%") do ( if "%out%"=="/f" (set ret=%%~fA) if "%out%"=="/d" (set ret=%%~dA) if "%out%"=="/p" (set ret=%%~pA) if "%out%"=="/n" (set ret=%%~nA) if "%out%"=="/x" (set ret=%%~xA) if "%out%"=="/s" (set ret=%%~sA) if "%out%"=="/a" (set ret=%%~aA) if "%out%"=="/t" (set ret=%%~tA) if "%out%"=="/z" (set ret=%%~zA) if "%out%"=="/dp" (set ret=%%~dpA) if "%out%"=="/nx" (set ret=%%~nxA) if "%out%"=="/fs" (set ret=%%~fsA) ) echo ParseFn result: %ret% echo. goto end :help @echo off :: @echo ::___________________________________________________________________:: :: @echo :: :: :: @echo :: ParseFn Help :: :: @echo :: :: :: @echo :: Chris Advena :: :: @echo ::___________________________________________________________________:: @echo. @echo ParseFn parses a fully qualified path name (e.g., c:\temp\my.bat) @echo into the requested component, such as drive, path, filename, @echo extenstion, etc. @echo. @echo Syntax: /switch filename @echo where, @echo filename is a fully qualified path name including drive, @echo folder(s), file name, and extension @echo. @echo Select only one switch: @echo /f - fully qualified path name @echo /d - drive letter only @echo /p - path only @echo /n - file name only @echo /x - extension only @echo /s - expanded path contains short names only @echo /a - attributes of file @echo /t - date/time of file @echo /z - size of file @echo /dp - drive + path @echo /nx - file name + extension @echo /fs - full path + short name @echo. :end :: @echo ::___________________________________________________________________:: :: @echo :: :: :: @echo :: ParseFn finished :: :: @echo ::___________________________________________________________________:: :: @echo.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With