Issue with File looping

Hi Team,

I am working on one of the scenarios, where I have source folder and destination folder.
In Source Folder I have multiple PDF files like
PDF1,PDF2,PDF 3, PDF,4,PDF 5 and more.

Now using for each loop to get one by one PDF files.
And inside loop I have to read pdf and read get accountname.
And after reading file need to move same PDF file with new name like accountname is “PPA” and move this file to destination folder.

now the issue is whenever account name is same in some pdf file so, I have to use count like “PPA1” .
this count is working when acount name has PPA1,PPA2, but when account name is change like PPB,PPB1 than count is adding like PPB2 direct because count is already increase in PPA1 file.

For example:-

Current Folder dest Folder
PDF1 PPA
PDF2 PPA1
PDF3 PPB
PDF4 PPA2
PDF5 PPB1

so in this case count did not add with fresh it is adding with previous one. which is incorrect.

Can anyone help me in this?

Really appreciate your help.

Hi @shuklarchana001

On what basis are you selecting what account name to use? PPA or PPB?

is it based on data in the file or is included in pdf file name? where are you picking account name from?

create a Dictionary with string as key(Account) and int as value(count) to store a count for each unique account name (PPA , PPB).

In your loop for each file after read pdf and read get accountname.
Check if key(Account) is present in dictionary if yes increment the count else add key with count as 1.

Yes , based on data in file

@shuklarchana001

if there are only 2 types of names that you can use. define 2 counter variables, one for PPA and one for PPB.

So when PPA name is being used, only increment PPA counter

And when PPB name is being used, only increment PPB counter

There are multiple account name I can not define multiple count

file name checking from destination folder if it is arleady there than only use count for file name

@shuklarchana001

Per my understanding, one counter variable can not be used in this case.

I dont see any other way to define the name using different counter values without using different counter variables or you can choose to create dictionary like @adi.mehare suggested.

Yes, exactly I am stuck because of single counter is not supporting for this case.

One of many options:
separate Analysis and File moving

  • loop over files

    • Open PDF / Extract Account Name
    • Store FileName, Account name in a Temporary pre-prepred Datatable / Dictionary
  • Calculate the counters

    • by Grouping the data and update temporary DataTable / Dictionary with the counter value
  • Loop over the temp Datatable / Dictionary

    • move the file by using filename / accountname / counter info

You can handle files in destination as well.

You have account name(PPA), so you can get count of files from destination folder starting with account name(PPA).
From this count you can rename your file accordingly.

You can use below code to get count then increment count and save file.

Directory.GetFiles(folderPath) _
                .Select(Function(f) Path.GetFileName(f)) _
                .Count(Function(name) name.StartsWith("<account_name>", StringComparison.OrdinalIgnoreCase))

@adi.mehare Thank you so much for your help. It’s finally working.

1 Like

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