Move File - Wild Cards

Hi All,

First time poster, I’ve tried reading some of the training documents and searched the forums for something similar but I couldn’t really find anything so hoping someone can help please.

I’m grabbing an excel file off an application which extracts the file to my downloads folder titled “report”+lots of random numbers. Therefore I want to move this file to another folder and rename it with today’s date.

Is there a way to use wildcard on the move file activity to look for the file that starts with “report”?

I’m sure that it’s just late and my mind isn’t working but any help would be appreciated.

Thanks.

Hi there,
Check out the below. Directory.EnumerateFiles() can get you a list of files that meet your needs.

Thanks.

1 Like

Thank you very much Clayton - did exactly what I wanted!

One question which probably has a really simple answer…when we’re converting to date format why do we have to capitalise the months? E.g. DateTime.Now.Date.ToString(“yyyyMMdd”)

Is it something to do with UK/US Format?

No Problem.
I could be wrong but I think it’s MM instead of mm because mm is for minutes. You can also take a look at Excel Formats and see how it’s used there because it’s similar.

Ah makes sense! Thanks very much.

Hi Clayton, how you doing, I am currently performing an automation, but I am stuck with the Directory.EnumerateFiles, I am try to convert a file with the following format

file_name_2018-11-09_10-45.csv
file_name_yyyy-MM-dd_HH-mm.csv

to be

file_name.csv

it means, removing the timestamp.

Using the value

Directory.EnumerateFiles("(file_path)", “"+DateTime.Now.Date.ToString(“yyyyMMddHHmm”)+".csv”, SearchOption.AllDirectories)

is not working for me.

Do you have another suggestion?

Hi @fgutierrez

Are you trying to pull in all the files that start with “file_name” or all files that end with the timestamp?

I will hopefully solve for if “file_name” is consistent, then you can use it next to a wildcard

Assign folder = "\\serverdomain\directory\foldername"
Assign fileKey = "file_name"
For each file In Directory.EnumerateFiles(folder, fileKey+"*.csv", SearchOption.AllDirectories)

Let me know if anything else does not work.

Regards.

Hi Clayton,

What I’m trying to do is move all files named as "file_name(some_time_stamp).csv, for the time stamp I will use wildcards, because it changes depending on the downloading date, then when founded the file to be moved I want to move it to another folder, and save it as “file_name.csv”.

Can’t I use the Move file activity to do this?

Hi @fgutierrez

The Move File doesn’t support wildcards, but you can use EnumerateFiles() or GetFiles() to get all files that meet your pattern using the wildcard. Then, run the list of files it found through a ForEach with the MoveFile inside.

Using example in my previous post would be something like the below pseudocode:

Declare variable: srcDir = "\\serverdomain\directory\foldername"
Declare variable: destDir = "\\serverdomain\directory\foldername2"
Declare variable: fileKey = "file_name"

// ForEach activity with TypeArgument String:
For each file In Directory.EnumerateFiles(srcDir, fileKey+"*.csv", SearchOption.AllDirectories)
    Move File // From: file, To: Path.Combine(destDir, Path.GetFileName(file))

Hope that explains it better.

Regards.

Hi Clayton,

Thanks for your valuable help on this, moving file worked perfectly for me in the flow I am designing, I just need two things more, after my file is downloaded by using the Open Browser activity and the download URL, the downloading process in chrome lasts, 1 minute aprox, so before the file is stored in srcDir, the robot continues with the For Each activity, thus the moving file is not done because it is not found, due to it haven’t been downloaded by that time. Even though, I am not getting any error, so I would need a step that waits until the file is downloaded, and then continue with For Each activity and move the file, I tried with Delay activity, but after downloads, never stop.

Another thing I want to do is move the file to destDir without the timestamp on the filename, e.g it must be moved as filename, instead of filename_YYYYMMDDhhMM, this is the default download name from the system. What should I add to Path.Combine(Crosscheck, Path.GetFileName(file)) string?

Thanks in advance.

The Timespan you used on the Delay was done wrong :face_with_raised_eyebrow:
I think it should be 00:00:65
which says 65 seconds

However, I don’t recommend using hardcoded delays. But, I understand if you don’t know what the filename will be after you download (unless you can get it to download and let you specify the filename, like for example if it’s in Chrome, you need to enable the setting so it will prompt you where to download the file). Using this approach let’s you choose the filename, and then you can check for the file to be completely downloaded with a more dynamic approach like a Retry scope with File Exists inside it.

Looking over what I posted above, I see that I suggested you store the “filename” to a variable, which I called filekey. So, for destDir, you can use Path.Combine(Crosscheck, Path.GetFileName(fileKey))
where Crosscheck is the destination direction and fileKey is the new filename you would like.

Regards.

Clayton, I wish you had a good and Happy New Year,

Thanks for your help, it helped me a lot!!

Regards.

1 Like