Author Topic: vba renaming files  (Read 7696 times)

0 Members and 1 Guest are viewing this topic.

krampaul82

  • Guest
vba renaming files
« on: October 19, 2011, 10:26:46 AM »
The code below will go into a directory called cut_sheets and delete any existing pdf files and re-populate with the new selection of hyperlinked pdf files. is it possible to skip over or rename any existing pdf files and only add any new hyperlinked pdf files? 

Any Help Appreciated......

Option Explicit

Public Sub Gartner_Get_Hyperlink_Main()
    Dim objBlock As AcadBlockReference
    Dim objEnt As AcadEntity
    Dim colHyps As AcadHyperlinks
    Dim fso As FileSystemObject
   
   
    Set fso = New FileSystemObject
   
    If Len(Dir(ThisDrawing.Path & "\Cut Sheets\*.pdf")) <> 0 Then ' Checks to see if there are any PDFs in CURRENT_Project\Cut Sheets\
        fso.DeleteFile ThisDrawing.Path & "\Cut Sheets\*.pdf", True ' If there are, delete them
        could i rename any existing files or skip over without deleting them and only add only new .pdf's  ? 
    End If
   
    For Each objEnt In ThisDrawing.ModelSpace
        If TypeOf objEnt Is AcadBlockReference Then
            Set objBlock = objEnt
            Set colHyps = objBlock.Hyperlinks
            On Error Resume Next ' In case we encounter any blocks that DON'T have hyperlinks
            fso.CopyFile colHyps.Item(0).URL, ThisDrawing.Path & "\Cut Sheets\", True ' The TRUE option will automatically overwrite any existing files with the same name
        End If
    Next objEnt
    Set fso = Nothing
End Sub

Matt__W

  • Seagull
  • Posts: 12955
  • I like my water diluted.
Re: vba renaming files
« Reply #1 on: October 19, 2011, 10:35:09 AM »
Code: [Select]
Public Function CheckFilesExistance(strFileName As String) As Boolean
    Dim FSO As FileSystemObject
    Set FSO = New FileSystemObject
    CheckFilesExistance = FSO.FileExists(strFileName)
    Set FSO = Nothing
End Function

If CheckFilesExistance(strFileName) = True then
    Msgbox "Skip existing file"
Else
    Msgbox "Do something"
End If
Autodesk Expert Elite
Revit Subject Matter Expert (SME)
Owner/FAA sUAS Pilot @ http://skyviz.io

krampaul82

  • Guest
Re: vba renaming files
« Reply #2 on: October 20, 2011, 12:23:35 PM »
Matt

Thank you for your response
what code would I write to skip an existing file?

Mark

Matt__W

  • Seagull
  • Posts: 12955
  • I like my water diluted.
Re: vba renaming files
« Reply #3 on: October 20, 2011, 01:47:15 PM »
Matt

Thank you for your response
what code would I write to skip an existing file?

Mark
You could flip it around and do a search for files that DON'T exist.

Code: [Select]
If Not CheckFilesExistance(strFileName) = True then
    Msgbox "Do something"
End If
Autodesk Expert Elite
Revit Subject Matter Expert (SME)
Owner/FAA sUAS Pilot @ http://skyviz.io

krampaul82

  • Guest
Re: vba renaming files
« Reply #4 on: October 20, 2011, 01:54:26 PM »
Do I need the message box?

Matt__W

  • Seagull
  • Posts: 12955
  • I like my water diluted.
Re: vba renaming files
« Reply #5 on: October 20, 2011, 01:55:35 PM »
No.  You would replace it with your code to add new files.
Autodesk Expert Elite
Revit Subject Matter Expert (SME)
Owner/FAA sUAS Pilot @ http://skyviz.io

fixo

  • Guest
Re: vba renaming files
« Reply #6 on: November 12, 2011, 05:00:30 PM »
You have to look at  Copy, CopyFile and Move methods
of FileSystemObject here:
http://msdn.microsoft.com/en-us/library/6tkce7xa(v=VS.85).aspx
HTH

krampaul82

  • Guest
Re: vba renaming files
« Reply #7 on: December 12, 2011, 05:10:08 PM »
Thank You Fixo...

BlackBox

  • King Gator
  • Posts: 3770
Re: vba renaming files
« Reply #8 on: December 13, 2011, 01:47:31 AM »
With VBA is one relegated to using the FileSystemObject Object, or can the System.IO.File Class be invoked?
"How we think determines what we do, and what we do determines what we get."

BlackBox

  • King Gator
  • Posts: 3770
Re: vba renaming files
« Reply #9 on: December 13, 2011, 01:49:54 AM »
Also, instead of the sub-routine get/creating the FileSystemObject Object each time it is called, perhaps it is more efficient for the calling function to supply the FSO as an argument with the file path, no?
"How we think determines what we do, and what we do determines what we get."