0

I'm fairly new to vbscript and programming! As I've already mentioned in the title, I've written (or at least I've tried) a vbscript which should copy and rename all files in C:\test\ and the subfolders of C:\test\ to a another Folder, named C:\test1.

Here's what I've got so far:

Dim objStartFolder, objtargetFolder, objDateCreatedName
Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "C:\test"
objtargetFolder = "C:\test1"

Set objFolder = objFSO.GetFolder(objStartFolder)
WScript.Echo objFolder.Path
Set colFiles = objFolder.Files
For Each objFile In colFiles
    WScript.Echo objFile.Name
Next
WScript.Echo

ShowSubFolders objFSO.GetFolder(objStartFolder)

Sub ShowSubFolders(Folder)
    For Each Subfolder In Folder.SubFolders
        WScript.Echo Subfolder.Path
        Set objFolder = objFSO.GetFolder(Subfolder.Path)
        Set colFiles = objFolder.Files
        For Each objFile In colFiles
        Set objDateCreatedName = objFSO.GetFile(objStartFolder)
        WScript.Echo objDateCreatedName.DateCreated
        WScript.Echo "I'm going to copy " & objFolder.Path & objFile.Name & " to " & objtargetFolder & objtargetFolder & objFile.Name & "." 
    Next
    WScript.Echo
    ShowSubFolders Subfolder
Next
End Sub

It would be really nice if you could help me and if you need more Information I'll make sure to deliver them.

NiceTry
  • 113
  • 1
  • 2
  • 8

1 Answers1

1

If every file should go into the same destination folder you could simply do something like this:

Set fso = CreateObject("Scripting.FileSystemObject")

Function Pad(s)
  Pad = Right("00" & s, 2)
End Function

Sub CopyFiles(fldr, dst)
  'Copy all files from fldr to destination folder and append the date (in ISO
  'format) to the name. Overwrite existing files.
  For Each f In fldr.Files
    created = Year(f.DateCreated) & "-" & Pad(Month(f.DateCreated)) & "-" & _
              Pad(Day(f.DateCreated))
    newname = fso.GetBaseName(f) & "_" & created & "." & fso.GetExtensionName(f)
    f.Copy fso.BuildPath(dst, newname), True
  Next

  'Recurse into subfolders.
  For Each sf In fldr.SubFolders
    CopyFiles sf, dst
  Next
End Sub

CopyFiles fso.GetFolder("C:\test"), "C:\test1"
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328