将文件复制到另一个文件夹并使用 vbscript 根据创建日期重命名它们

copying files to another folder and renaming them based on the creation date using vbscript

我对 vbscript 和编程还很陌生!正如我在标题中已经提到的,我已经编写(或者至少我已经尝试过)一个 vbscript,它应该将 C:\test\ 和 C:\test\ 的子文件夹中的所有文件复制并重命名为另一个文件夹,名为 C:\test1。

这是我目前得到的:

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

如果你能帮助我,那就太好了,如果你需要更多信息,我会确保提供给他们。

如果每个文件都应该放在同一个目标文件夹中,您可以简单地执行以下操作:

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"