Thursday, November 6, 2025

Different File Modifiers in Windows Batch Scripting

File Modifiers are used with replacement parameter in FOR loop. They begin with ~ tilde sign which is followed by replacement parameter(also called loop variable) which can be of 1 letter.

You can run following examples individually to learn about file modifiers.

SYNTAX: %%Tilde Then Single Letter File Modifier Then Single Letter Loop Variable
Note that you can combine file modifiers also.

Example

@echo off

REM List filename of each batch file, file modifier is not used here

FOR %%f in (*.bat) DO echo %%f

pause

Example

@echo off

REM List filename without extension of each batch file, ~n for filename

FOR %%f in (*.bat) DO echo %%~nf

pause

Example

@echo off

REM List file extension of each file, ~x for file extension

FOR %%f in (*) DO echo %%~xf

pause

Example

@echo off

REM List file name with extension of each file, ~nx for file name and extension

FOR %%f in (*.bat) DO echo %%~nxf

pause

Example

@echo off

REM List full path of each file, ~f for fully qualified path including filename

FOR %%f in (*.bat) DO echo %%~ff

pause

Example

@echo off

REM List full path of each file, ~f for fully qualified path excluding filename

FOR %%f in (*.bat) DO echo %%~pf

pause

Example: This example shows the usage in one script.

@echo off
SET "tab= "

ECHO Get list of files 
for %%i in (*) do (
 echo %tab% %%i
)


ECHO Get list of files with fullpath
for %%i in (*) do (
 echo %tab% %%~fi
)

ECHO Get list of files with name only
@echo off
for %%i in (*) do (
 echo %tab% %%~ni
)

ECHO Get list of files with extension only
@echo off
for %%i in (*) do (
 echo %tab% %%~xi
)

ECHO Get list of files with name and extension only
@echo off
for %%i in (*) do (
 echo %tab% %%~nxi
)

ECHO Get list of files with path
@echo off
for %%i in (*) do (
 echo %tab% %%~pi
)


The output of this script is as follows:
Get list of files
         file.txt
         file2.txt
         file_info.bat
Get list of files with fullpath
         D:\BAT Examples\New folder\file.txt
         D:\BAT Examples\New folder\file2.txt
         D:\BAT Examples\New folder\file_info.bat
Get list of files with name only
         file
         file2
         file_info
Get list of files with extension only
         .txt
         .txt
         .bat
Get list of files with name and extension only
         file.txt
         file2.txt
         file_info.bat
Get list of files with path
         \BAT Examples\New folder\
         \BAT Examples\New folder\
         \BAT Examples\New folder\
Press any key to continue . . .

Tips: To get the complete list of file modifier, run the command FOR/?

Here is some list:

    %~I         

 expands %I removing any surrounding quotes (")

    %~fI        

 expands %I to a fully qualified path name

    %~dI       

 expands %I to a drive letter only

    %~pI       

 expands %I to a path only

    %~nI       

 expands %I to a file name only

    %~xI       

 expands %I to a file extension only

    %~sI        

 expanded path contains short names only

    %~aI       

 expands %I to file attributes of file

    %~tI        

 expands %I to date/time of file

    %~zI       

 expands %I to size of file



No comments:

Post a Comment

Hot Topics