Search with a letter

Greetings of the day…
I had given a task where I should get a character as an input from the user and I should search with that char in a specified folder for the files that are starting with the given char and move those files in another folder. How can I find the first char and match them with user input?

Thanks and Regards
Soumiya

1 Like

Hello @soumi_soumiya,
You need to get all files from the target directory using Directory.GetFiles(Directory.GetCurrentDirectory) and loop through each file to determine if it starts with the user input character. You can then use Path.GetFileName(file.ToString).StartsWith(userInput) in an if condition which will return True when its a match. Replace Directory.GetCurrentDirectory with your target directory.

Example code for reference. You can replace the Message Box activity with your code to move the matched file to the destination folder.
GetFilesByStartsWith.xaml (6.9 KB)

2 Likes

@soumi_soumiya

Please refer attachment

Main.xaml (9.7 KB)

1 Like

Try this:

  1. get all files using Directory.getfiles("") and store in a array of type string
  2. loop through the array and check if the string starts with as (change the item to string in for loop)

Item.startswith(char you have)

  1. in then condition, use move files…

Hope this helps

1 Like

Hi @soumi_soumiya,
For sure there are many method to achieve this. Personally I would use powershell here. Like this:


image
Powershell script is loaded from txt file. Variable is used as well and taken from Input Dialog:

And finally test (the letter I was looking for was “Z”:
image
image

2 Likes

It Works but i have a problem that when two files having a same starting char for example if the first doc starts with s and second also starts with s then the first file get moves to the specified folder but the second throws the exception that file already exist. I should move all the files that starts with S so how can i do this without overwriting and throwing exception

Thanks
Soumiya

This will help in that case @soumi_soumiya

I had done it identifies and moves the file but I have on concern is that if the file with the same name exists it gives me an already exists file exception I need to move all the file how to achieve this

Thanks
Soumiya

I don’t understand this @soumi_soumiya,

File manager won’t allow two files with same name in a same folder right? Then , How will you have two files with same name :slight_smile:

1 Like

@soumi_soumiya

If you say that while moving the second file throws an exception saying that the file with the same name already exists when you paste them in the destination path, then how do you have the same name files in the the source.

Even if you try to create a same name files anywhere in the same directory (MANUALLY) then you will get very same error because this is file manager error, not UiPath exception.

Regards,
@hacky

i am done with all but i need to create a new folder with the name of user input and move the file that matches with user input i am using function
destinationPath+letter+""+files
Where destination path is the path the folder to be created and letter is a variable that stores the user input wich should be the name of th efolder and files is the file name that is renamed
I am doing it in move activity but i am getting an error that path not exists how to overcome this
Thanks
Soumiya

Try appending a “\” after letter in the path you have @soumi_soumiya

1 Like