Home
Manage Your Code
Snippet: Manipulating File Name and path in a DOS Batch File (Other)
Title: Manipulating File Name and path in a DOS Batch File Language: Other
Description: If you need to extract the drive letter, path, full path, file name, extension of a file in a DOS Batch file then this snippet is for you. Views: 206
Author: Stephen Smith Date Added: 3/15/2010
Copy Code  
@echo off
:menu
cls
echo          ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
echo          º  Main Menu        º
echo          ÌÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ͹
echo          ºE. Extension       º 
echo          ºN. File Name       º   
echo          ºF. File Name + Ext º   
echo          ºP. Path            º
echo          ºQ. Qualified Path  º
echo          ºS. Short Path      º
echo          ºU. Drive and Path  º
echo          ºD. Drive           º
echo          ºT. Date and Time   º
echo          ºA. Attributes      º
echo          ºZ. Size            º
echo          ºX. Exit            º
echo          ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ   
set choice = 
set /p choice=Enter the Menu Number to Continue:
if '%choice%'=='E' goto ext 
if '%choice%'=='e' goto ext
if '%choice%'=='N' goto name
if '%choice%'=='n' goto name
if '%choice%'=='F' goto fname
if '%choice%'=='f' goto fname
if '%choice%'=='P' goto path
if '%choice%'=='p' goto path
if '%choice%'=='Q' goto qpath
if '%choice%'=='q' goto qpath
if '%choice%'=='S' goto spath
if '%choice%'=='s' goto spath
if '%choice%'=='U' goto fpath
if '%choice%'=='u' goto fpath
if '%choice%'=='D' goto drive
if '%choice%'=='d' goto drive
if '%choice%'=='T' goto date
if '%choice%'=='t' goto date
if '%choice%'=='A' goto attrs
if '%choice%'=='a' goto attrs
if '%choice%'=='Z' goto size
if '%choice%'=='z' goto size
if '%choice%'=='X' goto exit
if '%choice%'=='x' goto exit
ECHO "%choice%" is not valid please try again!
pause
goto menu

:ext
rem Print the file extension
for %%i in (*.*) do echo "File extension = %%~xi"
pause
goto :menu
:name
rem Print the filename
for %%i in (*.*) do echo "File name = %%~ni"
pause
goto :menu
:path
rem Print the path
for %%i in (*.*) do echo "Path = %%~pi"
pause
goto :menu
:qpath
rem fully qualitifed path
for %%i in (*.*) do echo "Qualified path = %%~fi"
pause
goto :menu
:drive
rem drive letter
for %%i in (*.*) do echo "Drive letter = %%~di"
pause
goto :menu
:spath
rem short path
for %%i in (*.*) do echo "Short path = %%~si"
pause
goto :menu
:attrs
rem attributes
for %%i in (*.*) do echo "Short path = %%~ai"
pause
goto :menu
:date
rem date time
for %%i in (*.*) do echo "Short path = %%~ti"
pause
goto :menu
:size
rem size
for %%i in (*.*) do echo "Size = %%~zi"
pause
goto :menu
:fpath
rem drive letter and path
for %%i in (*.*) do echo "Drive Letter and Path = %%~dpi"
pause
goto :menu
:fname
rem filename and extension
for %%i in (*.*) do echo "File name and ext = %%~nxi"
pause
goto :menu
:exit
echo Goodbye!
Usage
Dos Prompt