所以我需要一个Windows脚本,我可以告诉它一个目录,它将解析所有子目录,而在每个子目录中,将归档所有具有特定文件扩展名的文件,并将其保存在同一个子目录中,然后转到下一个。
最好的方法是什么? Perl Automation Scripting,AutoIt?
你们可以给我的任何示例代码?
答案 0 :(得分:3)
Perl比批处理脚本更强大,但由于Perl不包含在Windows中,因此对于像这样的任务来说似乎有些过分。这应该是例如:
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 :(得分:3)
FORFILES包含在Windows中,可能比FOR更适用于您要执行的操作:
FORFILES [/ P pathname] [/ M searchmask] [/ 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.
答案 2 :(得分: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
答案 3 :(得分: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);
}