2016-06-01 145 views
1

我有一個文件夾,其中有大量.tif文件放入。如何根據部分文件名將文件移動到新文件夾或子文件夾?

它們都以MW_SW_開頭,後面還有NSSW。所以我需要能夠從前兩個擴展到NSSW

我已經有一個批處理文件,它首先根據文件名的前兩個字符將文件移動到文件夾MWSW。這是我目前的批處理文件,它工作得很好。但我想我需要第二個批處理文件或添加到這個步驟來執行下面的步驟1 &。此代碼後請參閱下文。

REM Sort by First name. 
REM This script creates a folder for either the full file name, 
REM or if it contains an underscore, the part before the underscore. 

REM TODO - Don't copy over existing files. 
REM TODO - Move files into Sub folders based on Date in file name "last 8 characters .tif 

@echo off 
REM Needed because you are working with variables that are immediately called 
setlocal enabledelayedexpansion 

REM Start of the loop to get all files with a psd or jpg Extension 
for %%A in (*.tif *.jpg *.pdf) do (
    echo file found %%A 

    REM Grabs only the file name 
    for /f "delims=" %%B in ("%%A") do set fname=%%~nB 
    REM Grabs only the extension 
    for /f "delims=" %%C in ("%%A") do set fextn=%%~xC 

    REM Using the File name it separates it into 2 part using "_" as a delimiter so 120_low becomes 120 and low 
    for /f "tokens=1* delims=_" %%D in ("!fname!") do set folname=%%D 
    echo folder name !folname! 

    REM Checks for the existence of the folder, if the folder does not exist it creates the folder 
    if not exist "!folname!" (
     echo Folder !folname! does not exist, creating 
     md "!folname!" 
    ) else (
     echo Folder !folname! exists 
    ) 

    REM Moves the file to the folder 
    echo Moving file %%A to folder !folname! 
    REM if not exist "%%D\%%A" move "%%A" "!folname!" 
    if not exist "%%D\%%A" copy "%%A" "!folname!" 

    REM add the date DDMMYYYY to the end of each file. Name can be 80 characters long. 
    rem ren "!folname!\%%A" "????????????????????????????????????????????????????????????????????????????????_%date:~-10,2%%date:~-7,2%%date:~-4,4%.tif" 
) 

echo Finished 
pause 

所以這裏是我需要我猜。第二或第三批文件做以下。我希望有人能幫幫忙。

注:請記住,如果一個文件存在與xxxx(1).tifxxx(2).tif等對文件名末尾移動時它會重命名該副本。

  1. 移動文件現在列在MW文件夾複製到基於從第4個文件的文件名的10字符的名稱,或從第四字符到第二或下一個「_」新的或現有的子文件夾。

  2. 根據文件名的最後8個字符,將指定文件夾的子文件夾中的文件移動到指定文件夾的子日期。

我需要的是再從MW文件夾中的文件移動到基​​於文件名「的日期一節」的最後7個字符新的或現有的子文件夾。

例如我們先從MW文件
文件進來的文件夾C:\temp\Test_Moving_Files\

MW_VRL5VF10000_6542234_01052016.TIF 
MW_Flybuys_677888_01052016.TIF 
MW_VRL5VF10000_333443_02052016.TIF 
MW_Flybuys_555555_02052016.TIF 
MW_goodguys_534535_02052016.TIF 
MW_goodguys_222222_02052016.TIF 
MW_Flybuys_123443_03052016.TIF 
MW_Flybuys_3545555_03052016.TIF 
MW_goodguys_444444_03052016.TIF 
MW_goodguys_888888_03052016.TIF 

輸出到子文件夾應被分類到子文件夾象下面這樣:

MW\VRL5VF10000\01052016\MW_VRL5VF10000_6542234_01052016.TIF 
MW\VRL5VF10000\02052016\MW_VRL5VF10000_333443_02052016.TIF 

MW\Flybuys\01052016\MW_Flybuys_677888_01052016.TIF 
MW\Flybuys\02052016\MW_Flybuys_555555_02052016.TIF 
MW\Flybuys\03052016\MW_Flybuys_123443_03052016.TIF 
MW\Flybuys\03052016\MW_Flybuys_3545555_03052016.TIF 

MW\goodguys\01052016\MW_goodguys_222222_02052016.TIF 
MW\goodguys\02052016\MW_goodguys_534535_02052016.TIF 
MW\goodguys\03052016\MW_goodguys_444444_03052016.TIF 
MW\goodguys\03052016\MW_goodguys_888888_03052016.TIF 

回答

0

它看起來任務即可通過修改實現

REM Using the File name it separates it into 2 part using "_" as a delimiter so 120_low becomes 120 and low 
    for /f "tokens=1* delims=_" %%D in ("!fname!") do set folname=%%D 
    echo folder name !folname! 

    REM Checks for the existence of the folder, if the folder does not exist it creates the folder 
    if not exist "!folname!" (
     echo Folder !folname! does not exist, creating 
     md "!folname!" 
    ) else (
     echo Folder !folname! exists 
    ) 

REM Using the file name it separates it into 4 parts using "_" as a delimiter. 
    for /f "tokens=1-4 delims=_" %%D in ("!fname!") do set "folname=%%D\%%E\%%G" 
    echo Folder name !folname! 

    REM Checks for the existence of the folder path. If the folder 
    REM path does not exist, it creates the complete folder structure. 
    if not exist "!folname!\*" (
     echo Folder !folname! does not exist, creating ... 
     md "!folname!" 
    ) else (
     echo Folder !folname! exists. 
    ) 

代替使用只是下劃線分隔文件名的第一子串,該代碼使用上述現在第一,第二和第四子串。我沒有測試它!

命令MD也可以一次創建多個文件夾,因此變量folname也可以是文件夾路徑字符串。那麼,我建議在批處理代碼中將變量folname重命名爲FolderPath

當然,您還必須在其他批處理腳本中使用folname或更好的FolderPath而不是%%D

+0

好神!在最初的測試這工作完美!非常感謝。莫菲迴應「傳奇!」 –

相關問題