2010-04-30 121 views
1

所以我需要一個Windows腳本,我可以告訴它一個目錄經過,它會解析所有的子目錄,而在每個子目錄中,將存檔具有特定文件擴展名的所有文件並保存它相同的子目錄,然後移動到下一個。Windows批處理腳本問題

這是什麼最好的方法呢? Perl自動化腳本,AutoIt?

任何示例代碼,你們可以給我嗎?

+3

什麼意思是「歸檔」,確切地說?一些歸檔工具可以在一個命令中完成... – 2010-04-30 15:00:35

+0

例如,我有一個包含15個子目錄的目錄,每個子目錄都有更多的子目錄。 Sharepoint不喜歡某些文件擴展名,因此所有這些都必須壓縮。 – Scott 2010-04-30 15:16:57

回答

1

下面的一種方式,我會做它的AutoIt既然你問。將MsgBox行替換爲您需要執行的任何代碼。 AutoIt是有趣的東西!

#include <File.au3> 

archiveDir(InputBox("Path","Enter your start path.")) 

Func archiveDir($rootDirectory) 
    $aFiles = _FileListToArray($rootDirectory) 

    For $i = 1 To UBound($aFiles) - 1 
     If StringInStr(FileGetAttrib($aFiles[$i]),"D") Then archiveDir($rootDirectory & $aFiles[$i] & "\") 
     MsgBox(0,"This would be your archive step!",'"Archiving" ' & $rootDirectory & $aFiles[$i]) 
    Next 
EndFunc 
0

一種解決方案可能是:

my $dirCnt = 0; 
traverse_directory('C:\Test'); 

sub traverse_directory{ 
    my $directory = shift(@_); 
    $dirCnt++; 

    my $dirHandle = "DIR".$dirCnt;  
    opendir($dirHandle, $directory); 

    while (defined(my $file = readdir($dirHandle))){ 
     next if $file =~ /^\.\.?$/;    # skip . and .. ... 
     if (-d "$directory\\$file"){ traverse_directory("$directory\\$file"); } 
     if ($file =~ /\.txt/){ #find txt files, for example 

      print "$file\n";  #do something with the text file here 
     } 
    } 
    closedir($dirHandle); 
} 
3

Perl是不是批處理腳本更強大,但由於不包含在Windows的Perl似乎矯枉過正這樣的任務,這一個。這應該例如工作:

FOR /R C:\hello\ %%G IN (*.txt) DO "c:\Program Files\7-Zip\7z.exe" a %%G.zip %%G && del %%G 

請注意,您不能直接在提示中執行此操作,您必須將其另存爲一個.bat文件。當然,還可以允許用戶指定,像這樣的命令行的路徑和擴展:

FOR /R %1 %%G IN (%2) DO "c:\Program Files\7-Zip\7z.exe" a %%G.zip %%G && del %%G 

有關FOR和其他Windows命令行命令的詳細信息可以在這裏找到:http://ss64.com/nt/

test.bat C:\Hello\ *.txt 

編輯:隨後將被運行顯然,這需要你有安裝的7-Zip,但是這是很明顯,如果你想使用一些其他的拉鍊,其中更改代碼。同時請記住,在嘗試使用這樣的腳本時始終要非常小心。一個小錯誤可能會導致它刪除大量文件,因此您應該始終在文件系統的副本上對其進行測試,直到您完全確定它能正常工作。

+1

在我的星球上,Perl多年來一直在Windows上運行良好。 – mob 2010-04-30 15:38:07

+0

所以,說我創建一個.bat文件,第二個的內容來指定文件擴展名。我將如何運行? – Scott 2010-04-30 15:39:59

+0

你可以用「test.bat c:\ hello \ txt」來運行它。或者,您可以用%2替換batfile中的*。%2,以指定任何通配符。 @mobrule:讓我改述一下:它本身運行,但前提是你在系統上安裝了像ActivePerl這樣的Perl解釋器。它對於很多目的是有用的,但在這裏似乎有點矯枉過正。 – MatsT 2010-04-30 15:51:13

3

FORFILES包含在Windows和可能更適用比你現在要做什麼:

FORFILES [/ P路徑] [/ M搜索掩碼] [/ S] [/ C命令] [/ D [+ | - ] {MM/dd/yyyy | dd}]

說明: 選擇一個文件(或一組文件)並在該文件上執行一條 命令。這對批處理作業很有幫助。

參數列表:

/P pathname  Indicates the path to start searching. 
        The default folder is the current working 
        directory (.). 

/M searchmask Searches files according to a searchmask. 
        The default searchmask is '*' . 

/S     Instructs forfiles to recurse into 
        subdirectories. Like "DIR /S". 

/C command  Indicates the command to execute for each file. 
        Command strings should be wrapped in double 
        quotes. 

        The default command is "cmd /c echo @file". 

        The following variables can be used in the 
        command string: 
        @file - returns the name of the file. 
        @fname - returns the file name without 
           extension. 
        @ext  - returns only the extension of the 
           file. 
        @path - returns the full path of the file. 
        @relpath - returns the relative path of the 
           file. 
        @isdir - returns "TRUE" if a file type is 
           a directory, and "FALSE" for files. 
        @fsize - returns the size of the file in 
           bytes. 
        @fdate - returns the last modified date of the 
           file. 
        @ftime - returns the last modified time of the 
           file. 

        To include special characters in the command 
        line, use the hexadecimal code for the character 
        in 0xHH format (ex. 0x09 for tab). Internal 
        CMD.exe commands should be preceded with 
        "cmd /c". 

/D date   Selects files with a last modified date greater 
        than or equal to (+), or less than or equal to 
        (-), the specified date using the 
        "MM/dd/yyyy" format; or selects files with a 
        last modified date greater than or equal to (+) 
        the current date plus "dd" days, or less than or 
        equal to (-) the current date minus "dd" days. A 
        valid "dd" number of days can be any number in 
        the range of 0 - 32768. 
        "+" is taken as default sign if not specified. 
+0

不包含在Windows中。它是Windows資源工具包的一部分。 – 2011-03-05 17:46:07

+1

從Windows Vista開始,FORFILES本身包含在Windows中,不需要任何資源工具包。 – 2012-06-20 11:54:12