2017-04-03 40 views
0

我需要將幾個文件從一個文件夾移動到子文件夾。我的文件夾結構已經準備就緒。比較文件名子串與文件夾名稱子串並移動

文件當前文件夾:D:\AB\*.*
的文件名是:    SS-AA-Report-Temp File for Script Testing-Daily-31March.txt
目標文件夾:D:\AB\Pm 1.1 File For Script\Daily\

如何檢查文件名與文件夾名稱子串和移動?

注意我有這樣的多個文件。

set Path1= d:\AB 
Pushd %Path1% 
echo %Path1% 
for %%i in (*.*) do SET "FName=%%~ni" 
For /F "Tokens=4-5 Delims=-" %%A In ("%FName%") Do (
    Set "FoldOne=%%A" 
    Set "FoldTwo=%%B" 
) 
echo out %RDate% 
mkdir %Path1%\"%FoldOne%"\"%FoldTwo%"\%RDate% 
move %Path1%\"%FName%".* %Path1%\"%FoldOne%"\"%FoldTwo%"\%RDate%\ 

編輯:
文件名格式:

A-A-Format-Here First connectivity install on Day 0 regurlarly-Daily-All-2017-03-27-09-31-16.xls  
A-A-Format-Already First connectivity with 10 days created-Weekly-All-2016-11-28-10-01-02.csv  
A-A-Report-withname 1.2 Sample Report (Network Plan Report)-Daily-Detail-2017-01-03-23-53.xls  
A-A-Report-Nextreport 1.2 Sample Report (Network Plan Report)-Weekly-Detail-2017-01-03-23-02-53.csv  

現在我的文件夾結構是:

D:\AB\Pm 1.1 First connectivity install on Day 0\Daily\05042017  
D:\AB\Pm 2.1 First connectivity with 10 days\Weekly\29032017  
D:\AB\Pm 1.2 Sample Report\Daily\05042017  
D:\AB\Pm 1.2 Sample Report\Weekly\29032017  

這裏是批處理文件我已經:

set Path1= d:\AB 
Pushd %Path1% 
echo %Path1% 
for %%i in (*.*) do SET "FName=%%~ni" 
For /F "Tokens=4-5 Delims=-" %%A In ("%FName%") Do (
Set "FoldOne=%%A" 
Set "FoldTwo=%%B" 

) 
echo 1 %FoldOne% 
echo 3 %FoldTwo% 

IF %FoldTwo% == Daily (
echo here Daily 
For /F UseBackQ %%A In (
`PowerShell "(Get-Date).AddDays(-1).ToString('ddMMyyyy')"` 
) Do (Set "RDate=%%A" 
echo ffor %RDate% 
) 
) 

IF %FoldTwo% == Weekly (
Echo Weekly 
For /F UseBackQ %%A In (
`PowerShell "(Get-Date).AddDays(-7).ToString('ddMMyyyy')"` 
) Do (Set "RDate=%%A" 
echo %RDate% 
) 
) 

mkdir %Path1%\"%FoldOne%"\"%FoldTwo%"\%RDate% 
move %Path1%\"%FName%".* %Path1%\"%FoldOne%"\"%FoldTwo%"\%RDate%\ 

Pushd d:\ 

GoTo :EOF 
+0

RDate在哪裏設置?你知道'fold1'將包含一個前導空間嗎? 「p1.1」在哪裏出現?您是否希望刪除「Temp」和「Testing」字符串 - 這些是唯一的字符串,它們會一直出現嗎? – Magoo

+1

第一個錯誤是等號'set Path1 = C:\ Main'之後的空格,Windows命令解釋程序在將字符串分配給環境變量時不會省略該空格。刪除此空格字符。第二個錯誤是,整個目錄/文件字符串必須用雙引號括起來,而不是其中的部分。因此,使用'mkdir'%Path1%\%FoldOne%\%FoldTwo%\%RDate%「'和'移動」%Path1%\%FName%。*「」%Path1%\%FoldOne%\%FoldTwo%\% RDATE%\「'。要獲得任何命令的幫助,請打開命令提示符窗口並使用'/?'作爲參數運行命令。用'set /?'和'for /?'試試看,並閱讀輸出幫助頁。 – Mofi

+0

@Magoo感謝您的回覆,Rdate是在系統日期設置的文件夾名稱。 Temp和Testing只是用來解釋需求。 我的基礎要求是我需要匹配文件夾名稱的子字符串與文件名的子字符串,然後將該文件移動到各自的文件夾中,我也會有這樣的多個文件,並希望將它們全部移動。 – Ashu

回答

0

匹配文件名子字符串與文件夾名稱的邏輯仍然非常模糊。

但是,我編寫了兩種可能的解決方案,使用部分不同的方法進行相同的操作。

第一完整批次代碼:

@echo off 
setlocal EnableExtensions EnableDelayedExpansion 
cd /D "D:\AB" 

rem Get name of each subfolder starting with string Pm, a space, two single 
rem digit numbers separated by a dot, one more space and more characters to 
rem indexed environment variables for later usage. And assign the substring 
rem after the first 7 characters of each folder name also to an index 
rem environment variable. 

set FolderIndex=0 
for /D %%I in ("Pm ?.? *") do (
    set "FolderName!FolderIndex!=%%I" 
    set "CurrentPath=%%I 
    set "FolderPart!FolderIndex!=!CurrentPath:~7!" 
    set /A FolderIndex+=1 
) 
set "FolderCount=%FolderIndex%" 
rem set Folder 

rem Get date of yesterday and date of a week ago. 
for /F "usebackq" %%I in (`PowerShell.exe "(Get-Date).AddDays(-1).ToString('ddMMyyyy')"`) do set "DateDaily=%%I" 
for /F "usebackq" %%I in (`PowerShell.exe "(Get-Date).AddDays(-7).ToString('ddMMyyyy')"`) do set "DateWeekly=%%I" 
rem set Date 

rem Process now each file matching the wildcard pattern below in 
rem current folder and calling a subroutine with current file name. 

set "FileNotMoved=0" 
for %%I in (*-*-*-*-*) do call :MoveToFolder "%%I" 

endlocal & if not %FileNotMoved% == 0 pause 
goto :EOF 

rem This subroutine first gets fourth and fifth dash delimited part from 
rem each passed double quoted file name. 

rem Then it replaces in each fourth file name part each folder name part 
rem by an empty string until either all folder name parts are processed 
rem or the string substitution was successful meaning the file name part 
rem really contains the folder name part. 

rem Note: The substitution does not work correct if any folder name part 
rem  contains an equal sign as this character is the delimiter 
rem  between string to find and replace string for substitution. 

rem In second case with substitution being successful the folder for 
rem the file could be determined and the file is moved to the found 
rem folder if also time part could be determined from file name. 

:MoveToFolder 
for /F "tokens=4,5 delims=-" %%A in ("%~1") do set "NamePart=%%A" & set "NameTime=%%B" 
set "FolderIndex=0" 

:FindFolder 
if %FolderIndex% == %FolderCount% (
    set "FileNotMoved=1" 
    echo Found no folder for: %1 
    goto :EOF 
) 

call set "CurrentPart=%%FolderPart%FolderIndex%%%" 
if "!NamePart:%CurrentPart%=!" == "!NamePart!" (
    set /A FolderIndex+=1 
    goto FindFolder 
) 

call set "CurrentFolder=%%FolderName%FolderIndex%%%" 
if /I "%NameTime%" == "Daily" (
    set "FolderTime=%DateDaily%" 
) else if /I "%NameTime%" == "Weekly" (
    set "FolderTime=%DateWeekly%" 
) else (
    set "FileNotMoved=1" 
    echo Undefined time for: %1 
    goto :EOF 
) 

mkdir "%CurrentFolder%\%NameTime%\%FolderTime%" 2>nul 
move "%~1" "%CurrentFolder%\%NameTime%\%FolderTime%\" >nul 
if errorlevel 1 (
    set "FileNotMoved=1" 
    echo Failed to move file: %1 
) 
goto :EOF 

第二批代碼不同於第一溶液僅在子程序MoveToFolder如何被編碼用於查找對應的文件夾爲當前文件名。出於這個原因,子程序的代碼在下面發佈。

:MoveToFolder 
for /F "tokens=4,5 delims=-" %%A in ("%~1") do set "NamePart=%%A" & set "NameTime=%%B" 

for /F "tokens=1* delims==" %%X in ('set FolderPart') do (
    if not "!NamePart:%%Y=!" == "%NamePart%" (
     set "FolderName=%%X" 
     goto FoundFolder 
    ) 
) 

set "FileNotMoved=1" 
echo Found no folder for: %1 
goto :EOF 

:FoundFolder 
if /I "%NameTime%" == "Daily" (
    set "FolderTime=%DateDaily%" 
) else if /I "%NameTime%" == "Weekly" (
    set "FolderTime=%DateWeekly%" 
) else (
    set "FileNotMoved=1" 
    echo Undefined time for: %1 
    goto :EOF 
) 

set "FolderIndex=%FolderName:~10%" 
call set "CurrentFolder=%%FolderName%FolderIndex%%%" 

mkdir "%CurrentFolder%\%NameTime%\%FolderTime%" 2>nul 
move %1 "%CurrentFolder%\%NameTime%\%FolderTime%\" >nul 
if errorlevel 1 (
    set "FileNotMoved=1" 
    echo Failed to move file: %1 
) 
goto :EOF 

對於理解使用的命令以及它們如何工作,打開命令提示符窗口中,執行有下面的命令,並完全讀取顯示每個命令的所有幫助頁面非常謹慎。

  • call /?
  • cd /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • mkdir /?
  • move /?
  • pause /?
  • rem /?
  • set /?
  • setlocal /?

閱讀也是微軟的文章關於Using Command Redirection Operators2>nul>nul的解釋和對問題Single line with multiple commands using Windows batch file回答經營者的意義在Windows命令行上運行。

相關問題