Please some one help me out with this issue. In this scenerio my bot will be runned by user daily where it will download a single csv file from NSE Website…So in this task i want that bot should delete the yesterday’s file and read latest Today’s downloaded csv file.
How can i perform this task?
Hi @Priyesh_Shetty1 ,
Are you using a custom Location and a custom file name while downloading the CSV file ?
If so, then we can leverage that by using a date value in to identify the Previous Dated file and delete it.
@supermanPunch this is the CSV file the starting name will remain same only the date will keep changing…Like some times the user wants 1 week report or 1 month report date will keep changing…How can i do this?
You can use the following to get the latest file full path
Directory.GetFiles("Downloadsfolderpath","circular-SURV*.csv").OrderByDescending(function(x) file.GetCreationTime(x))(0)
Cheers
Assuming that the beginning of the filename and the ending of the file name is as How we require it, meaning the beginning will always be circular-SURV-
and the ending would be previous Date 19-04-2023
, then the following Expression should be able to get you the previous day file :
Directory.GetFiles(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),"Downloads"),"circular-SURV-*"+Now.AddDays(-1).ToString("dd-MM-yyyy")+".csv").First
The above expression will fetch the file based on the file name that is if it contains the Previous Date at the end of the file name. You can use the above Expression directly in the Delete File
activity. But to handle errors you could use an If condition and check if the file or value exists in the above expression and then perform the Delete operation.
Also, a Suggestion is to use a customised location for the Downloads Path as required and storing the file in that location, this can be made possible by configuring the Chrome Downloads Settings :
In this way you will have the control over the file naming and you can retrieve and delete the file as you require.
@Priyesh_Shetty1
Try this
Based on file name
System.io.Directory.GetFiles("C:\Users\Download","*"+now.ToString("dd-MM-yyyy")+".csv")(0).ToString
Or Try this
Use for each file in the folder activity
with If condition
To read file
CurrentFile.CreationTime.Date = Now.Date AND CurrentFile.Name.Contains("SURV")
To delete yesterday's file
CurrentFile.CreationTime.Date = Now.AddDays(-1).Date AND CurrentFile.Name.Contains("SURV")
If you are downloading the files suppose in “XYZ” Location. You are specifying that path in the Bot. So, before downloading New files for today. You can add below sequence for deleting old files from that CSV folder where there is old files. Inside Directory.Getfiles you can mention the folder Path.
Hope this works,
Best Regards.
@pratik.pandya But daily one csv file will get downloaded not multiple and i need to delete yesterdays csv file…So for that also i need to use for each?
Bot will download one csv file and after that bot will read that latest downloaded csv file.
@Priyesh_Shetty1 If you use for Each for deleting the previous CSV File…We don’t need to specify the Exact previous CSV File format name contains date in it. we Know Only 1 CSV will gets downloaded daily so the loop will also delete 1 file because it has only one file for daily.
And how can i move that one Csv file from download folder to another folder…Dyanmically?
@Priyesh_Shetty1 if you are downloading the CSV File, you are giving any specific path where the download files are getting place. So, instead of giving Download folder path, give another folder path where except CSV no files will be there.
@Gokul_Jayakumar is this process work’s for single csv file? I mean will it delete yesterdays downloaded CSV and it will read today’s downloaded csv?
Yes, @Priyesh_Shetty1
For Conformation, Print the log message of message box in If condition, Then “CurrentFile.Fullname” and ensure it.
@Gokul_Jayakumar When should i use Delete activity? I can use that inside if before Read CSV?
yes, you can place If the condition before the read CSV if Condition.
Also, test it by printing the message box before deleting it to ensure it.
Currentfile.fullname
Thank you everyone for your help
This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.