Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ScribbleGhost/c2dd2ee618870f2acad6a5e2a76c7e77 to your computer and use it in GitHub Desktop.
Save ScribbleGhost/c2dd2ee618870f2acad6a5e2a76c7e77 to your computer and use it in GitHub Desktop.
How to print file names with and without extensions in a Windows batch script. With relative or full path.

Getting file names in a batch script

Remember:

  • Replace double precentages with single precentage if you want to use it in CMD directly.
  • When file names are printed out, they already include double quotes.
@echo off

echo - - - Print the file names of all files in folder (with extension - relative path)
for %%A IN (*) DO (echo "%%~nxA")

echo. & echo.

echo - - - Print the file names of all files in folder (without extension - relative path)
for %%A IN (*) DO (echo "%%~nA")

echo. & echo.

echo - - - Print the file names of all files in folder (with extension - absolute path)
for %%A IN (*) DO (echo "%%~dpnxA")

echo. & echo.

echo - - - Print the file names of all files in folder (without extension - absolute path)
for %%A IN (*) DO (echo "%%~dpnA")

Example result:

- - - Print the file names of all files in folder (with extension - relative path)
"A beautiful picture.png"
"A movie file.mkv"
"An archive file.zip"
"Some text file.txt"
"test2.bat"


- - - Print the file names of all files in folder (without extension - relative path)
"A beautiful picture"
"A movie file"
"An archive file"
"Some text file"
"test2"


- - - Print the file names of all files in folder (with extension - absolute path)
"C:\example\A beautiful picture.png"
"C:\example\A movie file.mkv"
"C:\example\An archive file.zip"
"C:\example\Some text file.txt"
"C:\example\test2.bat"


- - - Print the file names of all files in folder (without extension - absolute path)
"C:\example\A beautiful picture"
"C:\example\A movie file"
"C:\example\An archive file"
"C:\example\Some text file"
"C:\example\test2"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment