- 1 year ago
How would I write a batch file which will name files in a directory like Windows Explorer auto rename?
Comment
1 / 1
To write a batch file that mimics the Windows Explorer auto rename functionality for files in a directory, you can use the following approach:
batch@echo off
setlocal enabledelayedexpansion
set "dirPath=C:\Path\to\Your\Directory"
for %%F in ("%dirPath%\*") do (
set "fileName=%%~nxF"
set "filePath=%%~dpF"
set "extension=%%~xF"
set /p "newName=Enter new name for !fileName!: "
if defined newName (
ren "!filePath!!fileName!" "!newName!!extension!"
)
)
endlocal
Here's an explanation of the code:
- Replace
"C:\Path\to\Your\Directory"
with the actual path to the directory where you want to rename the files. - The
for
loop iterates over each file in the specified directory. %%~nxF
extracts the file name with extension from the file path.%%~dpF
extracts the drive and path from the file path.%%~xF
extracts the file extension from the file path.- The
set /p
command prompts the user to enter a new name for each file. - The
ren
command renames the file using the new name provided by the user.
Save the above code into a batch file with a .bat
extension, and then double-click the batch file to run it. It will prompt you to enter a new name for each file in the specified directory. If you provide a new name, the file will be renamed accordingly.
Please exercise caution when running batch files that modify files in bulk. Make sure to test the script on a small set of files before applying it to a large number of files to avoid unintended consequences.