Windows 存档具有特定扩展名的文件

Windows 存档具有特定扩展名的文件,windows,scripting,automation,Windows,Scripting,Automation,因此,我需要一个Windows脚本,我可以告诉它一个目录,它将解析所有子目录,而在每个子目录中,将存档具有特定文件扩展名的所有文件,并将其保留在同一子目录中,然后移到下一个子目录 最好的办法是什么?Perl自动化脚本,AutoIt 你们能给我任何示例代码吗?一个解决方案可以是: my $dirCnt = 0; traverse_directory('C:\Test'); sub traverse_directory{ my $directory = shift(@_); $

因此,我需要一个Windows脚本,我可以告诉它一个目录,它将解析所有子目录,而在每个子目录中,将存档具有特定文件扩展名的所有文件,并将其保留在同一子目录中,然后移到下一个子目录

最好的办法是什么?Perl自动化脚本,AutoIt

你们能给我任何示例代码吗?

一个解决方案可以是:

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);
}
一种解决办法可以是:

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);
}

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命令行命令的详细信息,请参见:

然后,将使用以下工具运行此操作:

test.bat C:\Hello\ *.txt

编辑:这显然需要安装7-Zip,但如果您想使用其他拉链,很明显在哪里更改代码。还请记住,在使用这样的脚本进行实验时,一定要非常小心。一个小错误可能会让它删除很多文件,因此您应该始终在文件系统的副本上测试它,直到您完全确定它可以工作。

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命令行命令的详细信息,请参见:

然后,将使用以下工具运行此操作:

test.bat C:\Hello\ *.txt

编辑:这显然需要安装7-Zip,但如果您想使用其他拉链,很明显在哪里更改代码。还请记住,在使用这样的脚本进行实验时,一定要非常小心。一个小错误可能会使它删除很多文件,因此您应该始终在文件系统的副本上测试它,直到您完全确定它工作正常为止。

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.

FORFILES包含在Windows中,可能比FOR更适用于您尝试执行的操作:

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.

下面是一个方法,我会在AutoIt中做,因为你要求。将MsgBox行替换为您想要执行的任何操作所需的任何代码。这是有趣的东西

#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

下面是一个方法,我会在AutoIt中做,因为你要求。将MsgBox行替换为您想要执行的任何操作所需的任何代码。这是有趣的东西

#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

你说的归档到底是什么意思?一些归档工具可以在一个命令中完成这项工作……例如,我有一个目录,其中有15个子目录,而每个子目录又有更多的子目录。Sharepoint不喜欢某些文件扩展名,因此所有这些文件扩展名都必须压缩。您所说的归档到底是什么意思?一些归档工具可以在一个命令中完成这项工作……例如,我有一个目录,其中有15个子目录,而每个子目录又有更多的子目录。Sharepoint不喜欢某些文件扩展名,因此必须对所有这些文件进行压缩。因此,假设我创建一个.bat文件,第二个文件的内容用于指定文件扩展名。我将如何运行它?您将使用test.bat c:\hello\txt运行它。或者,您可以将batfile中的*%2替换为简单的%2,以便指定任何通配符@mobrule:让我换一句话:它确实以本机方式运行,但只有在系统上安装了类似ActivePerl的Perl解释器时才能运行。它在很多方面都很有用,但在这里似乎有些过分。注意-您可以使用命令行版本的7-zip来避免安装任何东西。命令行版本的可执行文件名为7za.exe.So,假设我创建一个.bat文件,第二个文件的内容指定文件扩展名。我将如何运行它?您将使用test.bat c:\hello\txt运行它。或者,您可以将batfile中的*%2替换为简单的%2,以便指定任何通配符@mobrule:让我换一句话:它确实以本机方式运行,但只有在系统上安装了类似ActivePerl的Perl解释器时才能运行。它是有用的
ful有很多用途,但在这里似乎有些过分。注意-您可以使用命令行版本的7-zip来避免安装任何东西。命令行版本的可执行文件名为7za.exe。Windows中不包括该命令行。它是Windows资源工具包的一部分。从Windows Vista开始,FORFILES本机包含在Windows中,不需要资源工具包。Windows中不包含FORFILES。它是Windows资源工具包的一部分。从Windows Vista开始,FORFILES本机包含在Windows中,不需要资源工具包。