简体   繁体   English

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

[英]Windows Script to move files older than 30 days with a specific extension to another folder

I am in need of a script preferably a vbscript for a Windows Server which will archive files in a folder to another folder.我需要一个脚本,最好是用于 Windows Server 的 vbscript,它将文件夹中的文件存档到另一个文件夹。 Say from \\\\folder1\\ to \\\\folder1\\archive\\\\\\folder1\\\\\\folder1\\archive\\

The files have the extensions of .doc and .xls这些文件的扩展名为.doc.xls

But also I only want to move files older than 30 days.但我也只想移动超过 30 天的文件。

Is there a simple way to do this?有没有一种简单的方法可以做到这一点?

Since you tagged your question with , I suppose you are accepting batch file solutions too.由于您用标记了您的问题,我想您也接受了批处理文件解决方案。
Here you are:这个给你:

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

Due to the syntax you used for the source directory path ( \\\\folder1\\ ) I suppose it is given by a UNC path.由于您用于源目录路径( \\\\folder1\\ )的语法,我想它是由 UNC 路径给出的。 So I use the pushd command which understands such, maps it to a temporary drive it creates and changes the current working directory to the root of that drive.所以我使用了理解这样的pushd命令,将它映射到它创建的临时驱动器并将当前工作目录更改为该驱动器的根目录。

The forfiles command is capable of enumerating a given directory (tree) and iterates through all items that meet a certain mask and modification date (age). forfiles命令能够枚举给定目录(树)并遍历满足特定掩码和修改日期(年龄)的所有项目。 Since forfiles supports a single mask only, I simply use it twice.由于forfiles仅支持单个掩码,因此我只需使用它两次。

The popd command at the end removes that temporary drive which has been created by pushd .最后的popd命令删除了由pushd创建的临时驱动器。

For more details about each used command, type it into the command prompt, followed by /?有关每个使用的命令的更多详细信息,请在命令提示符中键入它,后跟/? . .

Below code can do the required.I just added comments here to explain the code here.下面的代码可以做需要的。我只是在这里添加了注释来解释这里的代码。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 批处理脚本可将文件从scource移到目标文件夹,以保存30天以上的文件 - Batch script to move file from scource to destination folder for files older than 30 day 如何在Windows中压缩30天以上的日志文件? - how to compress log files older than 30 days in windows? Windows批处理文件:移动超过10天的文件和不带特定字符串的文件名 - Windows batch file: move files that older than 10 days and the filename without a specific string 用于将文件从一台服务器移动到另一台服务器超过 45 天的批处理脚本 - batch script to move files from one server to another older than 45 days 如果文件夹中的子文件夹包含超过 30 天的文件,则删除文件夹 - Delete a folder if a sub-sub-folder within it has files older than 30 days 批处理文件将超过30分钟的文件从一个文件夹复制到另一个文件夹 - batch file to copy files older than 30 minutes from one folder to another 在Windows 7上批处理作业以删除7天以上的文件夹 - Batch job to delete folder older than 7 days on windows 7 Windows批处理脚本,用于基于文件名搜索文件,检查文件是否早于N天,如果为true,则删除这些文件 - Windows batch script for searching files based on file name,checks if the files are older than N days, if true, it deletes those files Windows 批处理文件删除超过 X 天的文本文件 - Windows Batch file to delete text files older than X days 删除超过N天的文件的脚本不起作用 - Script for deleting files older than N days not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM