2017-04-06 43 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

相關問題