How to save new pdf file in folder with respect to existing sequence of files

Hi there,

I´m not able to solve my problem despite browsing through variety of topics in forum. I have a folder where are pdf files in sequence, e.g. WR 11.pdf, WR 16.pdf, WR 21.pdf, WR 22.pdf and WR 23.pdf. I need to save .pdf file automatically as WR 24.pdf (generally existing file with highest No. in folder + 1). Any ideas, how to proceed?

Thx.

1 Like

Hello @Peter_Carnogursky

You might assume that most recent file has the highest number, you might sometimes want to overwrite, etc.

Here, you won’t overwrite. The searchPattern will be "WR*.pdf" and destinationDirectory is the directory you’re working with.

The idea is to count the number of file matching searchPattern and use that number + 1 to obtain the new filename candidate. If the filename already exists, we compute a new filename candidate by adding 1 more until the filename is “free”.

Variables

prefix (String) = "WR"

destination (String)
searchPattern (String)
files  (Array of String)
i (Int32)
fileName (String)

Workflow

// Assign
searchPattern = String.Format("{0}*.pdf", prefix)

// Assign
files = Directory.GetFiles(destinationDirectory, searchPattern)

// Assign
i = files.Length

// Do While
Do:
    // Assign
    i = i + 1

    // Assign
    fileName = String.Format("{0} {1}.pdf", prefix, i)

    // Assign
    destination = Path.Combine(destinationDirectory, fileName)

While:
    File.Exists(destination) // Or files.Contains(destination)
3 Likes

@Peter_Carnogursky you just need to loop over all the files in a folder


an then inside the loop get the max index of file name and use the same into new file name

2 Likes

Hi Arpit,

I´m trying your solution, but got stuck with regex and read of decimal No. in argument with error message. I think it´s related to a variable type somehow.

Thx.

You can do like this:

// Assign (Array of String)
files = Directory.GetFiles(destinationDirectory, "*WR*.pdf")

// Assign (Int32)
fileIndex = files.Max(Function(p) CInt(System.Text.RegularExpressions.Regex.Match(p, "\d+(?=\.pdf)").Value)) + 1

// Assign (String)
destination = Path.Combine(destinationDirectory, "WR " & fileIndex.ToString & ".pdf")

EDIT: the error is from the ForEach’s TypeArgument set to Object. Change it to String to resolve it. In addition, the trim in unnecessary as you capture only digits.

3 Likes

@Peter_Carnogursky can you share your workflow what you have done

@Peter_Carnogursky Just change your loop variable item type from object into String
image

Arpit, your sequence returns empty string

@Peter_Carnogursky There might be a reason that logic didn’t went into For Each loop. you can use Write Line/Message box to check whether it was executed or not

Hi msan,

I tried your solution with the result below. So if there are 3 files in the folder, your solution offers to name next file Monthly report 4.pdf. But my intention is to rename Monthly report actual.pdf as Monthly report 25.pdf in this case to continue in order. If the last file would be Monthly report 35, I´d like to rename Monthly report actual.pdf as Monthly report 36.pdf. Sorry if I misled you with my first post.

Thx.

Yes, Arpit_Kesharwani approach is better suited. Did you try the three line implementation I posted? I see that the file names are different, please adapt the searchPattern accordingly.

I tried your proposal right now and it returns empty string like with Arpit logic. I see both of your approaches either to trim value or set a function, but I´m :cry: how to solve this challenge

But you did it, when I deleted file without value at the end it worked finally!

Many thanks!

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.