Here is a commented batch file for this task:
@echo off
if "%~1" == "" (
echo/
echo %~nx0 must be started with a folder name as parameter.
echo/
pause
goto :EOF
)
rem Push the current states of command extension and delayed environment
rem variables expansion as well as the current directory path and pointer
rem to list of current environment variables on stack and create a copy
rem of all environment variables.
setlocal EnableExtensions DisableDelayedExpansion
rem In new local environment define an environment variable FolderPath
rem with the string passed as first argument to the batch file.
set "FolderPath=%~1"
rem Replace all / by \ as very often people use / as directory separator
rem which is wrong because on Windows \ is the directory separator.
set "FolderPath=%FolderPath:/=\%"
rem Remove backslash at end of folder path if there is one already at end.
if "%FolderPath:~-1%" == "\" set "FolderPath=%FolderPath:~0,-1%"
rem Check if a folder with passed path really exists.
if not exist "%FolderPath%\*" (
endlocal
echo/
echo Folder "%~1" does not exist or is a file and not a folder.
echo/
pause
goto :EOF
)
rem Determine folder name and full foler path from specified folder.
for %%I in ("%FolderPath%") do set "FolderName=%%~nxI" & set "FolderPath=%%~fI"
rem Rename all files in that folder to folder name with keeping file extension
rem except the running batch file if it is also in that folder and the files
rem which have already the right name and of course those files which could
rem not be renamed because of another file has the same file extension and
rem has already the folder name as file name. Rename operation also fails if
rem a file in the folder is currently opened by a running application with
rem a file lock or NTFS permissions deny renaming the file.
set "RenameError=0"
for %%I in ("%FolderPath%\*") do (
if not exist "%FolderPath%\%FolderName%%%~xI" (
if not "%%I" == "%%~f0" (
ren "%%I" "%FolderName%%%~xI%"
if errorlevel 1 set "RenameError=1"
)
) else if not "%%~nxI" == "%FolderName%%%~xI" (
echo/
echo File "%%I"
echo cannot be renamed to "%FolderName%%%~xI"
echo because such a file exists already in folder:
echo "%FolderPath%"
set "RenameError=1"
)
)
rem Pause batch file execution in case of a file could not be renamed
rem because another file with same name already exists in the folder
rem or any other reason.
if %RenameError% == 1 (
echo/
pause
)
rem Remove from memory all environment variables of local environment, restore
rem the previous environment variables list and current directory not modified
rem by this script at all, and pop from stack the states of command extensions
rem and delayed environment variable expansion and restore them too.
endlocal
This batch file must be started with path of folder in which the files should be renamed. The folder path can be also a relative path including just .
or .\
to run the batch file on current folder.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
... explains %~1
and %~nx0
.
echo /?
endlocal /?
for /?
goto /?
if /?
pause /?
rem /?
ren /?
set /?
setlocal /?
See also answer on Single line with multiple commands using Windows batch file for an explanation of operator &
as used once in the batch file.