Results 1 to 6 of 6
  1. #1
    Jen is offline Novice
    Windows 8 Access 2010 32bit
    Join Date
    Jun 2015
    Posts
    2

    Renaming Files in a Folder

    Hi all,



    I am a newbie to Access and I am trying to create a very simple Module that will rename a large number of files from a folder. I wrote the code for one file to test if it will work but it returns a runtime error 53 (where highlighted in red=. However, the file path for sFile is correct. Can anyone help me with that? Thank you very much in advance!

    Sub renaming()

    Dim sFile As String
    Dim SFileNew As String
    sFile = "C:\Users\Jen\Desktop\test\testfile.pptx"
    SFileNew = "C:\Users\Jen\Desktop\test\newtestfile.pptx"

    Name sFile As SFileNew


    End Sub

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    Paste this code and have it call
    RenameFilesInDir

    it will prompt for a file to choose, (you MUST ADD REFERENCE : Microsoft Office 11.0 Object Library)
    then it scans the folder and rename all files by putting the word NEW in front of the old file name.
    This can be changed to any word. (at ScanFolder "New", vRetval )


    Code:
    Public Sub RenameFilesInDir()
    Dim vDir, vRetval, vFile
    
      'pick a file in a folder
    vRetval = UserPick1File("*", vDir)
       
       'if picked , scan all files in it
    If vRetval <> "" Then
        ScanFolder "New", vRetval
    End If
    End Sub
    
    
     ' pick any file in a folder to RENAME ALL files in folder
    Public Function ScanFolder(ByVal pvNewWord, ByVal pvStartFile, Optional pvPattern)
    Dim i As Integer, sDir As String
    Dim fso, oFolder, oFile
    
      'get the folder name
    i = InStrRev(pvStartFile, "\")          'not available in '97
    If i > 0 Then vDir = Left(pvStartFile, i)
    
       'create folder object
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set oFolder = fso.GetFolder(vDir)
    
       'scan all files
    For Each oFile In oFolder.Files
      'If instr(oFile.Name , pvPattern)>0 Then
            vOld = vDir & oFile.Name
            vnew = vDir & pvNewWord & vOld
            
              'MsgBox vOld & vbCrLf & vnew
            Name vOld As vName
         
      'End If
    Next
    
    Set oFile = Nothing
    Set oFolder = Nothing
    Set fso = Nothing
    End Function
    
    
    'open file dialog box
    Public Function UserPick1File(ByVal pvFilter, Optional pvPath)
    Dim strTable As String
    Dim strFilePath As String
    Dim sDialog As String, sDecr  As String, sExt As String
    
    
    With Application.FileDialog(msoFileDialogFilePicker)   'you MUST ADD REFERENCE : Microsoft Office 11.0 Object Library)
        .AllowMultiSelect = False
        .Title = "Locate a file to Import"
        .ButtonName = "Import"
        .Filters.Clear
        .Filters.Add "All Files", "*.*"
        .InitialFileName = "c:\"
        .InitialView = msoFileDialogViewList    'msoFileDialogViewThumbnail
        
            If .Show = 0 Then
               'There is a problem
               Exit Function
            End If
        
        'Save the first file selected
        UserPick1File = Trim(.SelectedItems(1))
    End With
    End Function

  3. #3
    Jen is offline Novice
    Windows 8 Access 2010 32bit
    Join Date
    Jun 2015
    Posts
    2
    Hi Ranman257,

    Thank you very much for your help. Unfortunately, your code returns a runtime error 75 for "Name vOld As vName"...any hints on how to fix this? Apologies, I am really a newbie here.. .

    Thanks in advance for your help!

    cheers,
    Jen

  4. #4
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    The NAME statement is correct.
    Error 75 is a file access error....do you have rights to rename, write to the folder?

  5. #5
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    There is nothing wrong with your code. I tested it (using a file on my desktop) and the file was renamed.
    Do you have a reference set for "Microsoft Access 14.0 Object Library"?

    Try moving "test\testfile.pptx" to "C:\".
    The code would then be:
    Code:
    Sub renaming()
    
        Dim sFile As String
        Dim SFileNew As String
    
        sFile = "C:\testfile.pptx"
        SFileNew = "C:\newtestfile.pptx"
    
        Name sFile As SFileNew
    
    
    End Sub
    I tried a text file (test.txt) and a powerpoint (test.pptx) file. Both times the file was renamed.

  6. #6
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    @ranman256

    Shouldn't the Name statement be:

    Code:
    Name vOld As vNew
    instead of
    Code:
    Name vOld As vName
    ???

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 05-15-2015, 10:58 AM
  2. Renaming Files using data from a table or query
    By enzokevin in forum Programming
    Replies: 7
    Last Post: 10-17-2014, 05:42 AM
  3. How to get name of files in a folder
    By behnam in forum Programming
    Replies: 1
    Last Post: 09-18-2014, 07:46 AM
  4. Replies: 7
    Last Post: 08-27-2012, 10:28 AM
  5. Number of files in a folder.
    By Iain in forum Reports
    Replies: 3
    Last Post: 06-17-2012, 01:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums