Windows脚本将具有特定扩展名的超过30天的文件移动到另一个文件夹

时间:2015-11-05 09:53:51

标签: windows batch-file vbscript

我需要一个脚本,最好是Windows Server的vbscript,它将文件夹中的文件存档到另一个文件夹。从\\folder1\\\folder1\archive\

这些文件的扩展名为.doc.xls

但我也只想移动超过30天的文件。

有一种简单的方法吗?

2 个答案:

答案 0 :(得分:10)

由于您使用标记了问题,我认为您也接受批处理文件解决方案 你在这里:

pushd \\folder1
forfiles /M *.doc /D -30 /C "cmd /C move @file .\archive\"
forfiles /M *.xls /D -30 /C "cmd /C move @file .\archive\"
popd

由于您用于源目录路径(\\folder1\)的语法,我认为它是由UNC路径给出的。所以我使用了解这样的pushd命令,将它映射到它创建的临时驱动器,并将当前工作目录更改为该驱动器的根目录。

forfiles命令能够枚举给定目录(树)并遍历满足特定掩码和修改日期(年龄)的所有项目。由于forfiles仅支持单个掩码,因此我只使用它两次。

最后的popd命令会删除由pushd创建的临时驱动器。

有关每个已使用命令的更多详细信息,请在命令提示符下键入,然后键入/?

答案 1 :(得分:1)

下面的代码可以执行必需的操作。我只是在这里添加注释来解释这里的代码。

Option Explicit 
On Error Resume Next 
Dim oFSO, oFolder, sSrcDirectoryPath, sDstDirectoryPath
Dim oFileCollection, oFile, sDir 
Dim iDaysOld 

sSrcDirectoryPath = "C:\folder1" 'Source folder location
sDstDirectoryPath = "C:\folder1\archive" ' archieve folder location
iDaysOld = 30

Set oFSO = CreateObject("Scripting.FileSystemObject") 
Set oFolder = oFSO.GetFolder(sSrcDirectoryPath) 
Set oFileCollection = oFolder.Files 

For each oFile in oFileCollection
    'Change the code here if any other file extension also required to be archieved.
    If (LCase(Right(Cstr(oFile.Name), 3)) = "doc" Or LCase(Right(Cstr(oFile.Name), 3)) = "xls") Then
        If (oFile.DateLastModified < (Date() - iDaysOld)) Then
        oFile.Move(sDstDirectoryPath & "\" & oFile.Name)
        End If 
    End If   
Next 

Set oFSO = Nothing 
Set oFolder = Nothing 
Set oFileCollection = Nothing 
Set oFile = Nothing