2017-10-16 186 views
0

我需要將給定目錄中的所有文件重命名爲文件夾名稱並保留該文件的擴展名。批處理文件重命名給定目錄中的文件

例子:

老: 這就是生活/ 23223432ww22wse.txt

新: 這就是生活/這是Life.txt

注意,我總是不知道分機,它可能是TXT,AVI,JPG或其他任何東西。

+0

如果有多個相同文件類型的文件,則_i.e。使用相同的extension_,那麼你的計劃就無法工作。 – Compo

回答

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爲運營商&的解釋。