2010-04-30 137 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