如何在Windows 7中批量重命名文件名,以便它们在结尾处具有数字顺序?

时间:2016-04-19 06:25:28

标签: php windows batch-file cmd

我的计算机上有一个php文件存档,我想将它们大量重命名。这就是问题所在。这是一个文件样本(我有大约420,000个)。

viewtopic.php_id=4
viewtopic.php_id=5
viewtopic.php_id=6

我希望所有这些文件都在.php中。现在,我知道要将文件扩展名重命名为cmd并cd到该文件,然后转到"ren *.* *.php"

但是,我不能这样做,因为所有文件都被命名为VIEWTOPIC。我很乐意拥有它,所以它将所有文件重命名为  viewtopic1, viewtopic2, viewtopic3,或甚至地狱1,2,3,4,5,6,7, etc,。php。然后,我可以通过cmd批量重命名文件。

1 个答案:

答案 0 :(得分:0)

如果我理解正确,您想将viewtopic.php_id = x重命名为viewtopic x。php。

:: Simple Batch Script

@echo off
setlocal enabledelayedexpansion

:: Assuming all files exist in same directory tree.  
:: Note - this will take a while.  Probably 30 minutes or more ( just a guess )

:: Also - I am not going to add a check to see if they are php_id files.
:: If you have other file types in this subdirectory,
:: it  will effect them too, and the results will be unpredictable. 
:: Could add a check for it, but easier move *.php_id* into their own subdirectory

for /r %%i in ( * ) do (
    :: set extension to everything after the period
    set "extension=%%~nxi"

    :: remove everything that is not the number
    set "num=!extension:php_id^==!"

    :: rename the original file the file name (%%~ni) + the number + ".php"
    rename %%i %%~ni!num!.php
 )

endlocal
@echo on