How do I save the latest file from a folder, move it to another folder and rename the file?

Hi, I need help with the following according to its sequence.

  1. How to save the latest file from the downloads folder
  2. How do I move this file to another folder
  3. How do I rename this file

I can work with variables.

Regards,
Shannon

Hey @Shannon_Quek ,
1.You can use this
Directory.GetFiles(“C:\Users\Jithesh.R\Downloads\Whatsapp”,“*.xlsx”,SearchOption.AllDirectories).OrderByDescending(Function(d) New FileInfo(d).CreationTime).Take(1)

2.You can use Move File Activity to move the file from one location to another

image

  1. You can use Rename FIle Activity to rename a file

image

Regards,

5 Likes

Hi @Jithesh_R,

My file name does not contain a date.

@Shannon_Quek

this expression would get you the latest file
str_filepath = Directory.GetFiles(yourfolder_path,“*.txt”).OrderByDescending(Function(d) New FileInfo(d).CreationTime).Tolist(0)

Use this

In the To you can give the path that you want to be renamed as it will work.

Hope this helps
Cheers.

1 Like

@Shannon_Quek
You can try this then.
(Assuming you file is an .xlsx File)

Directory.GetFiles(folder_path,“.xlsx”,SearchOption.AllDirectories).OrderByDescending(Function(d) New FileInfo(d).CreationTime).Take(1)

Cheers,

1 Like

Do I need to change any value under Assign function?
Mine is to save the latest file from Downloads, there is no additional folder that I have to access to

which function do i use this under? and for the folder_path is it a variable or the folder path link?

@Shannon_Quek
Folder_Path is the path of the folder where your Files Exists

Try Using You path instead of this(“C:\Users\Jithesh.R\Downloads\Whatsapp”),

Directory.GetFiles(“C:\Users\Jithesh.R\Downloads\Whatsapp”,“*.xlsx”,SearchOption.AllDirectories).OrderByDescending(Function(d) New FileInfo(d).CreationTime).Take(1)

Note : Make Sure to check the Double Quotes(“”) while copy pasting.

Output :smiley:
image

Regards,

1 Like

Hey @Shannon_Quek
That path you need to give buddy where you sav the file

Thn it will work

@Shannon_Quek ,
If you want to take the files directly from downloads you can try this
This will provide your required output

LatestFile(StringType) =Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)+“\Downloads”).[Select](Function(x) New FileInfo(x)).Where(Function(f) f.Extension.Equals(“.xlsx”)).OrderByDescending(Function(x) x.LastWriteTime).Take(1).ToArray()(0).ToString

Output :

image

Cheers,

1 Like

is there variable in the expression? if so, what is the variable type?

@Shannon_Quek ,
Nothing You just Take Assign activity and continue with the same query
Left side of the assign activity u can create String Type Variable
Right side you can provide below query

Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)+“\Downloads”).[Select](Function(x) New FileInfo(x)).Where(Function(f) f.Extension.Equals(“.xlsx”)).OrderByDescending(Function(x) x.LastWriteTime).Take(1).ToArray()(0).ToString

Reagrds,

1 Like

for the rename file function, what do i put under file?

@Shannon_Quek

Use Rename File Activity
Provide the same variable in the “File” Section that is LatestFileVariable
Provide a New Name whichever you want inside Double Quotes Like below

image

Cheers,

1 Like

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