Sub MoveFilesAndFolders()
Dim c As Range
Dim destPath As String
Dim sourcePath As String
Dim fName As String
Dim myRange As Range
'CHANGE THESE LINES
'===============
'Where are we moving everything to?
destPath = "C:\My Documents"
'Which cells have hyperlinks?
Set myRange = Range("A1:A100")
'===============
'Error checking
If Right(destPath, 1) <> "\" Then
destPath = destPath & "\"
End If
For Each c In myRange
sourcePath = ""
On Error Resume Next
sourcePath = c.Hyperlinks(1).Address
On Error GoTo 0
If sourcePath = "" Then
MsgBox c.Address(False, False) & " does not have a hyperlink." & vbNewLine & "Moving to next cell..."
GoTo nextCell
End If
'Check if it's a file or folder
fName = Dir(sourcePath)
If fName = "" Then
'It's a folder
Call Copy_Folder(sourcePath, destPath)
Else
'It's a file
FileCopy sourcePath, destPath & fName
End If
nextCell:
Next c
MsgBox "Files and folders copied to:" & vbNewLine & destPath
End Sub
Private Sub Copy_Folder(FromPath As String, ToPath As String)
'Macro obtained from:
'http://www.rondebruin.nl/win/s3/win026.htm
'This example copy all files and subfolders from FromPath to ToPath.
'Note: If ToPath already exist it will overwrite existing files in this folder
'if ToPath not exist it will be made for you.
Dim FSO As Object
'If you want to create a backup of your folder every time you run this macro
'you can create a unique folder with a Date/Time stamp.
'ToPath = "C:\Users\Ron\" & Format(Now, "yyyy-mm-dd h-mm-ss")
If Right(FromPath, 1) = "\" Then
FromPath = Left(FromPath, Len(FromPath) - 1)
End If
If Right(ToPath, 1) = "\" Then
ToPath = Left(ToPath, Len(ToPath) - 1)
End If
Set FSO = CreateObject("scripting.filesystemobject")
If FSO.FolderExists(FromPath) = False Then
MsgBox FromPath & " doesn't exist"
Exit Sub
End If
FSO.CopyFolder Source:=FromPath, Destination:=ToPath
'MsgBox "You can find the files and subfolders from " & FromPath & " in " & ToPath
End Sub
Sub MoveFilesAndFolders()
Dim c As Range
Dim destPath As String
Dim sourcePath As String
Dim fName As String
Dim myRange As Range
'CHANGE THESE LINES
'===============
'Where are we moving everything to?
destPath = "C:\Users\swami\Desktop\Dest Files Move"
'Which cells have hyperlinks?
Set myRange = Range("A1:A100")
'===============
'Error checking
If Right(destPath, 1) <> "\" Then
destPath = destPath & "\"
End If
For Each c In myRange
sourcePath = ""
On Error Resume Next
sourcePath = c.Hyperlinks(1).Address
On Error GoTo 0
If sourcePath = "" Then
MsgBox c.Address(False, False) & " does not have a hyperlink." & vbNewLine & "Moving to next cell..."
GoTo nextCell
End If
'Check if it's a file or folder
fName = Dir(sourcePath)
If fName = "" Then
'It's a folder
Call Copy_Folder(sourcePath, destPath)
Else
'It's a file
FileCopy sourcePath, destPath & c.Offset(, 1).Value & " " & fName
End If
nextCell:
Next c
MsgBox "Files and folders copied to:" & vbNewLine & destPath
End Sub
Private Sub Copy_Folder(FromPath As String, ToPath As String)
'Macro obtained from:
'http://www.rondebruin.nl/win/s3/win026.htm
'This example copy all files and subfolders from FromPath to ToPath.
'Note: If ToPath already exist it will overwrite existing files in this folder
'if ToPath not exist it will be made for you.
Dim FSO As Object
'If you want to create a backup of your folder every time you run this macro
'you can create a unique folder with a Date/Time stamp.
ToPath = "C:\Users\Ron\" & Format(Now, "yyyy-mm-dd h-mm-ss")
If Right(FromPath, 1) = "\" Then
FromPath = Left(FromPath, Len(FromPath) - 1)
End If
If Right(ToPath, 1) = "\" Then
ToPath = Left(ToPath, Len(ToPath) - 1)
End If
Set FSO = CreateObject("scripting.filesystemobject")
If FSO.FolderExists(FromPath) = False Then
MsgBox FromPath & " doesn't exist"
Exit Sub
End If
FSO.CopyFolder Source:=FromPath, Destination:=ToPath
'MsgBox "You can find the files and subfolders from " & FromPath & " in " & ToPath
End Sub
With a hyperlinked folder, we don't look at each individual file in the folder (and subfolders), so there's no chance to rename them.Folders are handled differently, since they get copied over in a single shot
Sub CreateHyperlinks()
'Creates a Table of Contents (ToC) in active sheet of PDFs in specified folder
Dim myFolder As String
Dim fName As String
Dim myArry As Variant
Dim fPath As String
Dim recCount As Long
Sheet1.Columns("A:A").Clear
'Where are the files located?
If Application.FileDialog(msoFileDialogFolderPicker).Show = flase Then
Exit Sub
Else
myFolder = Application.FileDialog(msoFileDialogFolderPicker).SelectedItems(1)
End If
'Make sure that final slash mark was added
If Right(myFolder, 1) <> "\" Then
myFolder = myFolder & "\"
Range("IV2").Value = myFolder
End If
fPath = Dir(myFolder)
'Create ToC on active sheet, starting in row 1
recCount = 0
Application.ScreenUpdating = False
'Loop through all the files, building Hyperlink formulas
Do Until fPath = ""
recCount = recCount + 1
Cells(recCount, "A").Formula = "=HYPERLINK(""" & myFolder & fPath & """,""" & fPath & """)"
fPath = Dir()
Loop
Application.ScreenUpdating = True
'Brief message to user
MsgBox "Number of files found: " & recCount, vbOKOnly, "Files found"
End Sub
Sub Example1()
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim i As Integer
'Create an instance of the FileSystemObject
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Get the folder object
Set objFolder = objFSO.GetFolder("C:\Users\swami\Desktop\PDF")
i = 1
'loops through each file in the directory
For Each objFile In objFolder.Files
'select cell
Range(Cells(i + 1, 1), Cells(i + 1, 1)).Select
'create hyperlink in selected cell
ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:= _
objFile.Path, _
TextToDisplay:=objFile.Name
i = i + 1
Next objFile
End Sub
Sub MainMacro()
'This is the macro to run for all things
Application.ScreenUpdating = False
'CHANGE THIS LINE
'===============
'What Range has hyperlinks originally?
Call MoveFilesAndFolders(Range("A1:A100"))
'===============
MsgBox "Operation complete!"
Application.ScreenUpdating = True
End Sub
Private Sub MoveFilesAndFolders(myRange As Range)
Dim c As Range
Dim destPath As String
Dim sourcePath As String
Dim fName As String
'CHANGE THIS LINE
'===============
'Where are we moving everything to?
destPath = "C:\Users\swami\Desktop\Dest Files Move"
'===============
'Error checking
If Right(destPath, 1) <> "\" Then
destPath = destPath & "\"
End If
For Each c In myRange
sourcePath = ""
On Error Resume Next
sourcePath = c.Hyperlinks(1).Address
On Error GoTo 0
If sourcePath = "" Then
MsgBox c.Address(False, False) & " does not have a hyperlink." & vbNewLine & "Moving to next cell..."
GoTo nextCell
End If
'Check if it's a file or folder
fName = Dir(sourcePath)
If fName = "" Then
'It's a folder
Call MakeLinks(sourcePath, c.Offset(0, 1).Value)
Else
'It's a file
FileCopy sourcePath, destPath & c.Offset(, 1).Value & " " & fName
End If
nextCell:
Next c
End Sub
Private Sub MakeLinks(fPath As String, newNum As String)
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim i As Integer
'Pick some column out of the way to put links.
'Currently using col Z
Const myCol As Long = 26
'Create an instance of the FileSystemObject
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Get the folder object
Set objFolder = objFSO.GetFolder(fPath)
i = 1
'loops through each file in the directory
For Each objFile In objFolder.Files
'create hyperlink in selected cell
ActiveSheet.Hyperlinks.Add Anchor:=Cells(i, myCol), Address:= _
objFile.Path, TextToDisplay:=objFile.Name
Cells(i, myCol + 1).Value = newNum
i = i + 1
Next objFile
'Move over the newly copied files
Call MoveFilesAndFolders(Range(Cells(1, myCol), Cells(i - 1, myCol)))
'Clear links
Range(Cells(1, myCol), Cells(1, myCol + 1)).EntireColumn.Clear
End Sub