Log in

View Full Version : [SLEEPER:] Backup files from one folder to other date separated folders



AlexPerry
01-24-2019, 07:55 AM
Hi guys. I'm totally new to VBA, and this forum.
I need help with a script.
I have a folder with txt and xml files, and I need to move files from that folder to backup folder.
The script should run twice a week and should create (in the destination folder) subfolder with date in format like 2018.12 (year and month), and move files there. If there is already a subfolder with current month's date, than it should just move files there.
Any help will be appreciated. Thank you.

大灰狼1976
02-13-2019, 09:40 PM
Hi AlexPerry!
Something like below:

Sub test()
Dim sourcePath$, fileName$, targetPath$
sourcePath = "C:\abc\sourcefolder\"
targetPath = "C:\abc\backup\" & Format(Date, "yyyy.mm") & "\"
If Dir(targetPath) = "" Then MkDir targetPath
ChDir (sourcePath)
fileName = Dir("*.xls")
Name sourcePath & fileName As targetPath & fileName
Do
fileName = Dir
If fileName = "" Then Exit Do
Name sourcePath & fileName As targetPath & fileName
Loop
End Sub