Voici un fichier batch commenté pour cette tâche:
@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
Ce fichier batch doit être démarré avec le chemin du dossier dans lequel les fichiers doivent être renommés. Le chemin d'accès au dossier peut également être un chemin relatif comprenant uniquement .
ou .\
pour exécuter le fichier de commandes sur le dossier en cours. Pour comprendre les commandes utilisées et leur fonctionnement, ouvrez une fenêtre d'invite de commande, exécutez les commandes suivantes et lisez attentivement toutes les pages d'aide affichées pour chaque commande.
call /?
... explique %~1
et %~nx0
.
echo /?
endlocal /?
for /?
goto /?
if /?
pause /?
rem /?
ren /?
set /?
setlocal /?
Voir également la réponse sur Single line with multiple commands using Windows batch file pour une explication de l'opérateur &
tel qu'il est utilisé une fois dans le fichier batch.
S'il existe plusieurs fichiers du même type, _i.e. avec la même extension_, votre plan ne peut pas fonctionner. – Compo