Author Topic: vba renaming files  (Read 1837 times)

0 Members and 1 Guest are viewing this topic.

krampaul82

  • Newt
  • Posts: 123
vba renaming files
« on: October 19, 2011, 09: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

M@yhem

  • Needs a day job
  • Posts: 9861
  • Pain is temporary. Quitting is forever.
Re: vba renaming files
« Reply #1 on: October 19, 2011, 09: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

krampaul82

  • Newt
  • Posts: 123
Re: vba renaming files
« Reply #2 on: October 20, 2011, 11:23:35 am »
Matt

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

Mark

M@yhem

  • Needs a day job
  • Posts: 9861
  • Pain is temporary. Quitting is forever.
Re: vba renaming files
« Reply #3 on: October 20, 2011, 12: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

krampaul82

  • Newt
  • Posts: 123
Re: vba renaming files
« Reply #4 on: October 20, 2011, 12:54:26 pm »
Do I need the message box?

M@yhem

  • Needs a day job
  • Posts: 9861
  • Pain is temporary. Quitting is forever.
Re: vba renaming files
« Reply #5 on: October 20, 2011, 12:55:35 pm »
No.  You would replace it with your code to add new files.

fixo

  • Swamp Rat
  • Posts: 782
  • Pietari, Venäjä
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
\\\"Always drink upstream from the herd."\\\ - Will Rogers, was died in 1935 plane crash

--> Donate to TheSwamp <--

krampaul82

  • Newt
  • Posts: 123
Re: vba renaming files
« Reply #7 on: December 12, 2011, 05:10:08 pm »
Thank You Fixo...

BlackBox

  • Water Moccasin
  • Posts: 2245
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?
"Potential has a shelf life." - Margaret Atwood

BlackBox

  • Water Moccasin
  • Posts: 2245
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?
"Potential has a shelf life." - Margaret Atwood