1

I have a folder "Documents" with a lot of subfolders. There are files under these subfolders.

My objective is to move the files to one folder location then at some point be able to move them back to the subfolders they came from.

My general idea is to append the subfolder name to the beginning of the file after being moved. Then maybe use this as a reference when they need to be moved back to their source subfolder.

Is this doable or do you have any other ideas on how I can achieve this? I only know how to move/copy the files in VBA.

This is the current code I use:

Fldr= "C:\Documents"
Set fs=createobject("scripting.filesystemobject")
Set f=fs.getfolder(Fldr)
Set fsub=f.subfolders

For each f in fc Subfolderspec=Fldr&""&f.name fs.copyfile Subfolderspec & ".", "C:\Test" On Error Resume Next Next

1 Answers1

1

I don't know if this is the best way to do this, but to follow your request, this code would work:

Fsrc = "C:\Documents"
Fdst = "C:\Test\"
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(Fsrc)
Set fsub = f.SubFolders
If Mid(Fdst, Len(Fdst), 1) <> "\" Then Fdst = Fdst & "\"

For Each f In fsub For Each fl In fs.GetFolder(Fsrc & "&quot; & f.name).Files new_name = Fsrc & "&quot; & f.name new_name = Replace(new_name, ":", "") new_name = Replace(new_name, "&quot;, "") new_name = new_name & "" & fl.name fs.CopyFile fl, Fdst & new_name Next On Error Resume Next Next

Chris
  • 36
  • Thank you for sharing this. It works! Now I see why this won't be a good solution due to the lengthy file name. What can I do to adjust it to append only the subfolder name it came from and not including the path? – Coding Newbie Nov 18 '20 at 17:19
  • Scratch my previous question. I was able to edit the code to just show the subfolder name. Will try to use edit your code to return them to the source folders. Many thanks again! – Coding Newbie Nov 18 '20 at 17:30