批处理文件重命名给定目录中的文件

时间:2017-10-16 02:42:10

标签: batch-file

我需要将给定目录中的所有文件重命名为文件夹名称并保留文件的扩展名。

示例:

旧: 这是Life / 23223432ww22wse.txt

新: 这就是Life / This is Life.txt

请注意,我并不总是知道扩展名,它可能是txt,avi,jpg或其他任何内容。

1 个答案:

答案 0 :(得分:0)

以下是此任务的注释批处理文件:

@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

必须使用文件夹的路径启动此批处理文件,其中应重命名文件。文件夹路径也可以是相对路径,仅包括..\以在当前文件夹上运行批处理文件。

要了解使用的命令及其工作原理,请打开命令提示符窗口,执行以下命令,并完全阅读为每个命令显示的所有帮助页面。

  • call /? ...解释%~1%~nx0
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • pause /?
  • rem /?
  • ren /?
  • set /?
  • setlocal /?

另请参阅Single line with multiple commands using Windows batch file上的答案,了解批处理文件中使用过的运算符&的说明。

相关问题