Created
July 19, 2025 18:58
-
-
Save banhill/46eb5e87f53662b45026dca5942ca83f to your computer and use it in GitHub Desktop.
Windows script to prepend directory name to files in a directory.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @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. |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: