This is my first question and hope you can help.
It's a script about moving file in every sub folders into another folder only if the file is new
For example
C:\Test\Sub1
C:\Test\Sub1\Sub
C:\Test\Sub2\Sub
D:\Test\Sub1
D:\Test\Sub1\Sub
D:\Test\Sub2\Sub
What i want to do right now is that when it finds there's a new file with extension Pdf
,zip
,xls
in C:\Test\Sub2\Sub
, it will move to D:\Test\Sub2\Sub
directly.
Then it will loop the whole folder of test and move the file according the above rule. I have been searching for some example but those don't fit.
Thank you in advance.
Edit
Option Explicit
const DestFolder = "B:\Testing\"
MoveFiles
Sub MoveFiles
' folder to look in
Dim strFolderPath : strFolderPath = "D:\Temp\Testing\"
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim RegEx : Set RegEx = New RegExp
' specify the extension you want to search for; seperate with a |
' currently searching for .txt and .mdb files
RegEx.Pattern = "\.(pdf|zip|xls|txt)$"
RegEx.IgnoreCase = True
RecurseFolder objFSO, strFolderPath, RegEx
End Sub
Sub RecurseFolder(objFSO, strFolderPath, RegEx)
Dim objFolder : Set objFolder = objFSO.GetFolder(strFolderPath)
Dim objFile, strFileName,dest
For Each objFile In objFolder.Files
strFileName = objFile.Path
If RegEx.Test(strFileName) Then
'Checking whether file exist in destination
if not objFSO.FileExists(destfolder.strFileName) then
objFile.Move destfolder
else
msgbox "File is already existed"
End If
End If
Next
Dim objSubFolder
For Each objSubFolder In objFolder.SubFolders
RecurseFolder objFSO, objSubFolder.Path, RegEx
Next
End Sub
I can loop through the sub folders but can't move to the folder according the source folder. For example,
FileA
come from D:\Temp\A
. It'd be moved to B:\Temp\A
. But now it moved to B:\Temp
only. Furthermore, since I can only use NotePad to write vbs, i can't figure out is there any bug for checking existing file. Is it correct?
Please lend me a helping hand. I will be very grateful for your kindness.