Renaming Files Loop order - MS Scripting Runtime

DaveBlakeMAAT

Board Regular
Joined
Feb 28, 2016
Messages
190
Hi

I am having a bit of a brain fart on this particular problem when it comes to ordering my loops correctly. I have a folder of scanned invoices (file names are sequentially numbered), I also have a spreadsheet which contains the invoice numbers in column A and a Page Count in Column B (one invoice can have multiple scanned image files.

I am fine with using Scripting Runtime and looping through each file in the nominated folder. Where I am struggling is getting my loops in the correct order!

My objective is to rename the files so that they are prefixed with the invoice number and suffixed with Page "x".

Any pointers in the right direction would be very much appreciated!

Regards

Dave
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Is there anything which relates the invoice numbers in the cells to the existing file names? Give examples of the invoice number and its corresponding file name. It might be easier to loop through the cells and find the corresponding file, instead of looping through the folder.
 
Upvote 0
Is there anything which relates the invoice numbers in the cells to the existing file names? Give examples of the invoice number and its corresponding file name. It might be easier to loop through the cells and find the corresponding file, instead of looping through the folder.

Hi John

unfortunatly there is no connection between the file names and the spreadsheet. The files are given a serial file name by the scanner, and therefore the only thing I can rely on is that the files will be listed in the same order as the rows in the spreadsheet.
 
Upvote 0
I did something similar on a recent thread - final code at https://www.mrexcel.com/forum/excel...hrough-folder-rename-files-2.html#post5132076

Change the last loop to:

Code:
    Dim p As Long
    For n = 0 To UBound(files)
        p = InStrRev(files(n), ".")
        Name path & files(n) As path & ActiveSheet.Cells(n + 2, "A") & " Page " & ActiveSheet.Cells(n + 2, "B") & Mid(files(n), p)
    Next
Expects the first invoice number to be in A2 of the active sheet.
 
Upvote 0
UPDATE - the above Name statement renames the files to "[column A cell] Page [column B cell]", losing the original file name.

You talked about a prefix and a suffix - if you want to keep the original file name and add the prefix and suffix, then change the Name statement to:

Code:
        Name path & files(n) As path & ActiveSheet.Cells(n + 2, "A") & Left(files(n), p - 1) & " Page " & ActiveSheet.Cells(n + 2, "B") & Mid(files(n), p)
I suggest testing the code on a copy of your folder to ensure it renames the files correctly.
 
Upvote 0
Thanks John

Gonna be a bit busy the next few days so won't have the chance to play around with it until the weekend, but will report back once tested.
 
Upvote 0
Thanks John

I understand your approach, generate a list of required file names, which you are storing in an array, then use that when looping through the files. That approach is going to work perfectly for me, I was overthinking the problem!

Many thanks

Regards

Dave
 
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,271
Members
452,628
Latest member
dd2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top