Could this be done through LINQ or extension methods? @ppr please help
What I’m telling you WILL result in separate files.
- generate filename with today’s date
- check if file exists
- if file doesn’t exist, create it with header row (you could actually do this by having a template Excel file with just the header row, and copying the file)
- append data to it
This process will result in a new file every day, that has the header row.
If you want separate files written within a day, then just do a counter variable. Inside a Do While (with “while” of file exists), increment the counter. When you finally get to a filename that doesn’t exist, the Do While will exit, you’ll have your counter value to append to the filename.
Once this completes, counter will have the next number to append to the filename. If counter is still 0 that means you don’t have to append anything to the filename.
Second assign inside the Do While is FileName = Now.ToString(“yyyyMMdd”) + “_” + counter.ToString + “.xslx”
I need some other approach.
Thanks for your efforts.
- readin file names - Directory.GetFiles(now.toString(“yyyyMMdd”) & “*.xlsx”)
- extract counter with regex
- calculate next file filename
Why do you need another approach? Looping works, it’s clean, simple and efficient. Easy to understand for anyone who needs to work with your code in the future.
fetch all the filename from the folder and put for each activity
in for each, extract the counter from your filename
get the maximum value
increment the maximum value+1
then save the file
Could this be done using one activity only (i.e., Assign Activity) using LINQ. I have given it a try but don’t know how to proceed further.
try to use select folder activity and assign to variable. then use it like directory.getfiles(your variable)
that’s not an issue but how to proceed further.
after getting the file, using right( filename, 1 or 2)
collect the values in loop and check maximum. while coming out of loop, use max+1… only for loop is the activity here
What if the process is running for the first time?
Because, for the first time the file name should be yyyyMMdd.xlsx
you have to get date and check date in your file name with the current date. if date matches proceed with loop else set counter as 0 or 1 and come out
Thanks for the approach
there is a logic you can try out
- Read the directory where the file [of this type ] may be present by using the assign activity [filter all file name like that
file_list = Directory.GetFiles(folderpath).Where(Function(file) Path.GetFileName(file).ToString.Contains(Now.ToString(“yyyyMMdd”))).ToArray()
Now use an if condition with condition as
if the condition is true, then the filename should be set as Now.ToString(“yyyyMMdd”).xlsx
if the condition is false, then the filename should be set as
I hope this will help you
Can we write this whole code inside a single Assign Activity in the form of LiNQ?
You can try this assign activitiy around
file_name= If(Directory.GetFiles(folderpath).Where(Function(file) Path.GetFileName(file).ToString.Contains(Now.ToString(“yyyyMMdd”))).Count=0, Now.ToString(“yyyyMMdd”), Now.ToString(“yyyyMMdd”)+"_"+ Directory.GetFiles(folderpath).Where(Function(file) Path.GetFileName(file).ToString.Contains(Now.ToString(“yyyyMMdd”))).Count.ToString)+".xlsx"
This will give u the file name too
But if it is complex in way if understanding, then u can normal flow approach as I suggested before
@NIVED_NAMBIAR thanks a lot. It is a beautiful piece of code.
This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.