Share via

Dir() loop changing filenames

Anonymous
2011-07-13T10:02:34+00:00

I am trying to debug the following code:

Fname2 = Dir(Folder2, MacID("XLSX"))

Do While Fname2 <> ""

Debug.Print "Do While: " & Fname2

Fname2 = Dir()

Debug.Print "Do While: " & Fname2

Loop

The display on the Immediate Window shows the below result.

Do While: AJProjBillv149_donotuse_0712.xlsx

Dir() :APProjBillv149_donot#F9BA0.xlsx

As shown above, the first loop returns the correct file name (AJProjBillv149_donotuse_0712.xlsx). However, after setting Fname2 = Dir(), the filename of the 2nd file was renamed automatically.

How do I resolve this to let it retrieve the right file and not to rename it?

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2011-07-13T14:41:53+00:00

Yes, I had the same problem.  See http://answers.microsoft.com/en-us/mac/forum/macoffice2011-macexcel/help-xl-2011s-dir-function-truncates-filename/e72fbf5d-749c-4a55-a77c-e2def6db24d9

I ended-up using AppleScript to get around the issue for now.  I created a function that returns an array of files found in the path.  I have a separate one to return folders.  This could use some error handling, but I just needed to get it working.

You could add some language to the set list to every file to filter for XLSX files only.  Google Applescript & "every file" and there are example of how to use something like "whose name contains .xlsx".  There may even be a filter for "has the extension" or something like that.  Good luck.

The argument passed to the function needs to be the full path in Apple format ... HardDrive:Users:....

Public Function myFilesystem_GetArrayOfFolderFiles(theFullPath)

thisPathFileArray = MacScript("set list_of_file_names to {}" & Chr(13) & _

"tell application ""Finder""" & Chr(13) & _

"set the source_folder to """ & theFullPath & """ as alias" & Chr(13) & _

"set file_list to (every file in source_folder) as alias list" & Chr(13) & _

"repeat with current_file in file_list" & Chr(13) & _

"set current_file_name to (the name of current_file)" & Chr(13) & _

"copy current_file_name to the end of list_of_file_names" & Chr(13) & _

"end repeat" & Chr(13) & _

"return list_of_file_names" & Chr(13) & _

"end tell")

MyArray = Split(thisPathFileArray, ",")

myFilesystem_GetArrayOfFolderFiles = MyArray

End Function

Was this answer helpful?

0 comments No comments

5 additional answers

Sort by: Most helpful
  1. Anonymous
    2011-07-14T01:16:47+00:00

    The file wasn't actually renamed. But the strange thing is, it opened as read-only when the macro was working on the renamed file, and prompted if i want to over-write the existing copy when I don't see the 'existing' file in my folder. The result was all the updates were not saved.

    Was this answer helpful?

    0 comments No comments