Quantcast
Channel: VBForums - ASP, VB Script
Viewing all articles
Browse latest Browse all 688

Vbscript - copy folder & sub folder & files

$
0
0
Hello All,

I have a need to create a script that copies all folders from a Source Folder to a Destination folder, keeping the folder structure. It also has to copy all files within each sub folder whilst appending the date and time to the file.

Here is what I have so far, which works well for me.

Code:

option explicit

DIM filesystemobject, folder, folder2, file
DIM strTarget, strSource
DIM strDate, strTime
Dim objFSO

strDate = Year(Date) & "-" & Right("0" & Month(Date),2) & "-" & Right("0" & Day(Date),2)                'Set date format to YYYY-MM-DD
strTime = Right("0" & Hour(Now()),2) & "-" & Right("0" & Minute(Now()),2)                                                'Set time format to HH-MM
strSource = "C:\Users\gwilliama\Documents\Temp"                                                                                                        'Source folder location
strTarget = "C:\Users\gwilliama\Documents\Orders\Temp\"                                                                                        'Destination folder location

set filesystemobject = CreateObject("Scripting.FileSystemObject")
set folder = filesystemobject.getfolder(strSource)
set folder2 = filesystemobject.getfolder(strTarget)

                For each file in folder.files

                        If DateDiff("d", file.DateLastModified, Now) > 90 Then                                                                                'Checks the date difference between now and date modified
       
                                file.name = Left(file.name, InstrRev(File.Name,".")-1) _                                                               
                                & " " & strDate & "_" & strTime & Right(file.name,len(file.name) _
                                - InstrRev(file.name,".")+1)                                                                                                                        'Appends YYYY-MM-DD_HH-MM before the . in the file extension
               
                                filesystemobject.Movefile file, strTarget                                                                                                'Moves the selected file to the destination folder

                        End If

                Next                                                                                                                                                                                        'Loops through every file in the folder

I've been searching the web and this forum, now I've seen a few posts where people have had what they wanted but I can't make any of it work with what I need.

I know what I want, and looking at what others have done I can roughly see what it is they've done but I can't get my head around this.

Viewing all articles
Browse latest Browse all 688

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>