Skip to content

Instantly share code, notes, and snippets.

@banhill
Created July 19, 2025 18:58
Show Gist options
  • Select an option

  • Save banhill/46eb5e87f53662b45026dca5942ca83f to your computer and use it in GitHub Desktop.

Select an option

Save banhill/46eb5e87f53662b45026dca5942ca83f to your computer and use it in GitHub Desktop.
Windows script to prepend directory name to files in a directory.
@echo off
setlocal enabledelayedexpansion
:: Check if a directory path is provided
if "%~1"=="" (
echo Please provide a directory path.
exit /b 1
)
:: Set the directory path
set "dir_path=%~1"
:: Extract the directory name
for %%I in ("%dir_path%") do set "dir_name=%%~nxI"
:: Loop through each file in the directory
for %%F in ("%dir_path%\*") do (
:: Skip if it's a directory
if not exist "%%~F\" (
:: Extract the file name and extension
set "file_name=%%~nF"
set "file_ext=%%~xF"
:: Rename the file
ren "%%F" "%dir_name%!file_name!!file_ext!"
)
)
echo Done.
@banhill
Copy link
Author

banhill commented Jul 19, 2025

Usage:

prepend_dir_name.bat "C:\path\to\your\directory"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment