Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 391 Vote(s) - 3.46 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Windows Script to move files older than 30 days with a specific extension to another folder

#1
I am in need of a script preferably a vbscript for a Windows Server which will archive files in a folder to another folder. Say from `\\folder1\` to `\\folder1\archive\`

The files have the extensions of `.doc` and `.xls`

But also I only want to move files older than 30 days.

Is there a simple way to do this?
Reply

#2
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
Reply

#3
Since you tagged your question with [tag:batch-file], 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. 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.

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). Since `forfiles` supports a single mask only, I simply use it twice.

The `popd` command at the end removes that temporary drive which has been created by `pushd`.

For more details about each used command, type it into the command prompt, followed by ` /?`.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through