2017-04-06 40 views
0

我有很多旧文件(主要是pdf,但也有一些.doc或.docx),它们是根据他们的内容添加日期。将带有结尾日期(dd.mm.yyyy)的文件重命名为前缀(yyyy-mm-dd)

银行不需要支付2007年4月10日

银行等话题2007年4月11日

论文什么要补充2007年4月10日

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

银行u nneeded支付2007年4月10日至 2007-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=" %%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 

回答

0

下面的批处理:

  • Iteraes从开始文件夹的所有文件夹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" 

如果输出看起来不错,除去echoin的最后一行。

输出示例:

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

嗨! 非常感谢!这就像一个魅力!我改了一下,所以我可以在sendme文件夹中使用它,但它的工作非常好,而不需要对提交的代码进行任何更改!所有文件都被检查,如果他们有一个有效的日期日期将被重新格式化和重命名。 谢谢,帮了很多! 另外:我永远不会得到它使用这么少的字符! – mschmidt

+0

刚刚编辑你的问题标题,以减少罗嗦;-) – LotPings

相关问题