2017-04-06 11 views
0

古いファイル(主にpdfsですが、いくつかの.docまたは.docx)があります。その内容は日付が追加されています。末尾のファイル名の末尾に(dd.mm.yyyy)をプレフィックス(yyyy-mm-dd)に改名します。

銀行不要な支払い2007年4月10日

銀行その他のトピック2007年4月11日

論文2007年4月10日

私は内のすべてのファイルの名前を変更バルクにしたいを追加するには何もありません名前を変更する特定のフォルダ

銀行u 2007年4月10日 2007-10-04 に銀行不要な支払いnneeded支払い

私はすでに直接任意のフォルダにそれを活性化させることができるように私の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
  • は(拡張子ドットを含む)は、少なくとも3つのドットのパターンを探し始めフォルダからすべてのフォルダ
  • 拡張子のない名前が日付で終了し、call :renサブ
  • はサブリオーダーで始まり最後の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" 
    

    出力がOKに見える場合

は、最後の行を削除し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

関連する問題