【问题标题】:rename files with trailing date (dd.mm.yyyy) to a prefix (yyyy-mm-dd)将带有尾随日期 (dd.mm.yyyy) 的文件重命名为前缀 (yyyy-mm-dd)
【发布时间】:2017-04-06 22:41:21
【问题描述】:

我有很多旧文件(主要是 pdf,但也有一些 .doc 或 .docx),这些文件根据其内容命名并添加了日期。

银行不需要的付款 04.10.2007

银行其他主题 04.11.2007

论文没有什么可添加的 04.10.2007

我想批量重命名特定文件夹中的所有文件以更改名称

银行不需要的付款 04.10.20072007-10-04 银行不需要的付款

我的 sendto 文件夹中已经有一个批处理文件,可以在任何文件夹中直接激活它,并且它已经在迭代所有文件。它甚至可以将文件名分解为标记并可以显示给我。

我根本无法工作:

检查当前令牌 (%%a) 是否是 dd.mm.yyyy 格式的日期(不检查它是否有效!)。如果它找到一个日期,它应该创建一个新字符串 (yyyy-mm-dd) 并使用所有其他令牌来构建一个新文件名并重命名当前文件。

希望你能帮我解决这个问题!

当前代码:

@ECHO OFF
setlocal enabledelayedexpansion

rem *******************************
rem this is used to get the path of the currently used folder.
(set foldername=%1)

rem this overwrites the foldername with the "%~dp1 - expands %1 to a drive letter and path only"
call set foldername=%%~dp1

rem now we have to check how many files we find in our folder
for /F "usebackq delims=" %%i in (`dir /a:-d /b "%%foldername%%\*.*"`) do set /a dirCnt+=1

rem this version works for folders without spaces (C:\scripttest) (no "")
rem for /F "usebackq delims=" %%i in (`dir /a:-d /b %%foldername%%\*.*`) do set /a dirCnt+=1

rem when we don't find any files with the old date format in the folder, we will abort the batch
IF (%dirCnt%) EQU (0) (
  echo No files found in folder "%foldername%". Exiting.
  pause
  EXIT 1
 )

echo Found %dirCnt% files in folder "%foldername%".
rem now we start to finally do stuff!
set tmpCnt=0
set tmp2Cnt=0
rem we will go over each FILE here
for /F "usebackq delims=" %%i in (`dir /a:-d /b "%foldername%\*.*"`) do (
  set /a tmpCnt+=1
  call echo Proceed file %%tmpCnt%% from %%dirCnt%%: %%i.

  rem here we need to find IF there is a date in format dd.mm.yyyy available
  rem here we go over each PART of that file, automatically split by " " removing the ending (~n)
  for %%a in (%%~ni) do (
rem SET "var="&for /f "tokens=1 delims=0123456789" %%i in ("%%a") do set var=%%i
rem if defined var (echo %%a is NOT numeric) else (echo %%a is numeric)
    echo %%a
    )
    rem this echos only the file extension including the . = ".doc" or ".pdf"
    echo %%~xi
    rem echo %%a
  rem IF NOT, jump to next file!

  rem we copy the dd, mm and yyyy to different variables

  rem we build a new variable with yyyy-mm-dd

  rem we remove the old date format (dd.mm.yyyy) and add the new date format (yyyy-mm-dd) to the file as prefix

  rem if this worked, we add 1 to the count!
  set /a tmp2Cnt+=1
 )


rem  set "filename=%%~nf"
rem  ren "%%f" "!filename:~0,-4!%%~xf"
rem  for /f "delims=" %%i in ('dir /b /a-d *.txt') do ren "%%~i" "%%~ni 1.1%%~xi"


call echo process finished without errors - %%tmp2Cnt%% from %%dirCnt%% files processed, check folders for results!
pause

【问题讨论】:

    标签: date batch-file rename


    【解决方案1】:

    以下批次:

    • 从起始文件夹 X:\folder\to\start 迭代所有文件夹
    • 查找具有至少三个点(包括扩展点)的模式
    • 检查不带扩展名的名称是否以日期和call :ren sub 结尾
    • 在子中将最后 10 个字符重新排序到开头。

    @ECHO OFF
    For /R "X:\folder\to\start" %%A in (*.*.*.*
    ) Do Echo:%%~nA|findstr "[0-3][0-9]\.[01][0-9]\.[12][09][0-9][0-9]$">NUL 2>&1 && Call :Ren "%%~fA"
    Goto :Eof
    :Ren
    Set "FileName=%~n1"
    Set "FDate=%FileName:~-10%"
    Set "NewName=%Fdate:~-4%-%Fdate:~3,2%-%Fdate:~0,2% %FileName:~0,-10%"
    If "%NewName:~-1%" Equ " " Set "NewName=%NewName:~0,-1%"
    Echo Ren "%~f1" "%NewName%%~x1"
    

    如果输出看起来没问题,请删除最后一行的回显。

    样本输出:

    Ren "Q:\Test\2017-04\06\Test blah blubb 13.10.1999.txt" "1999-10-13 Test blah blubb.txt"
    

    【讨论】:

    • 嗨!非常感谢!这就像一个魅力!我对其进行了一些更改,以便可以在 sendme 文件夹中使用它,但它的效果非常好,无需对您提交的代码进行任何更改!检查所有文件,如果它们具有有效日期,则日期将被重新格式化和重命名。谢谢,帮了大忙!另外:我永远不会用这么少的字符得到它!
    • 刚刚编辑了您的问题标题以减少罗嗦;-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-13
    • 1970-01-01
    • 2013-03-18
    • 2020-01-11
    • 2017-07-20
    • 2018-03-31
    相关资源
    最近更新 更多