寻找一个脚本,用文件名中的charactares重命名多个文件

时间:2016-09-12 16:43:58

标签: file batch-file

我在许多文件夹中有很多文件需要重命名。 例如 来自cgs2016-09-05-05-40-34.xls 到cgs0905.xls

和 来自cgs2016-09-06-05-40-34 到cgs0906.xls

非常感谢任何帮助!

3 个答案:

答案 0 :(得分:0)

不是最好的方式,但适用于您的示例:

@echo off
setlocal enabledelayedexpansion
for %%x in (*.xls) do (
  set "filename=%%x"
  ECHO ren "%%x" "!filename:~0,3!!filename:~8,2!!filename:~11,2!.xls"
)
如果输出正常,

删除ECHO

答案 1 :(得分:0)

@Jamaz在您的文件示例中尝试以下代码。请再次在文件的测试样本上使用它,这样如果出错就不会导致问题。谢谢,如果这对您有用,请向上投票。

setlocal enabledelayedexpansion
REM Collect list of file names in current directory that have the .xls file extension. Output to text file.
dir /b "*.xls" >xls.txt
REM For loop examines the text file for individual file names.
FOR /F "tokens=1" %%# in (.\xls.txt) do ( 
REM SET variable "#" to equal "token"
    Set "token=%%#"
REM Extract the first 3 characters (year) from the file name and set is to variable "token"
    Set "tokenchar=!token:~0,3!"
REM Extract the month characters from the file name and set the variable as "tokenmonth"
    Set "tokenmonth=!token:~8,2!"
REM Extract the day characters from the file name and set the variable as "tokenday"
    Set "tokenday=!token:~11,2!"
    ren "%%#" "!tokenchar!!tokenmonth!!tokenday!.xls" 
    echo %%#

)
Pause

答案 2 :(得分:0)

因为最后十九个字符日期和时间戳比前三个(特别是多个文件夹)更可能是常量,所以我会改变以前的答案都是为了满足这个理由。

<button/>

由于OP不清楚代码是针对多个相同级别的文件夹还是源自单个位置的子文件夹,我选择后者,因为之前的回复已经涵盖了这一点。

在第4行和第5行更改您选择的文件路径和扩展名 如果您对控制台输出感到满意,请从第10行删除@Echo Off SetLocal EnableDelayedExpansion (Set _rf=C:\Users\jamaz\TestDir) (Set _fe=xls) If Not Exist "%_rf%\" Exit/B For /R "%_rf%" %%I In (*.%_fe%) Do (Set "_fn=%%~nI" Echo=Ren "%%I" "!_fn:~,-19!!_fn:~-14,2!!_fn:~-11,2!%%~xI") Timeout -1 1>Nul EndLocal Exit/B 并删除第11行