How to get the full path of a directory by using it's name?

uiautomation
studio

#1

I want to check for a specific file in Downloads directory and delete it if it exists?
Someone please tell me how to get the full path of “Downloads” directory? Because If I use the full path, it doesn’t work when others use the same code, they need to change the path again.

Thanks and Regards,
Namratha


#2

Hi,

The full path can be something like below.
C:\Users\user_name\Downloads

The user name can be taken as a variable, if different user log in, his username value can be given in the full path.

Thanks,
Saranya K R


Dynamic Path
#3

Hi,
You can find all special folder using the below code, it gives the user desktop path.
Environment.GetFolderPath(Environment.SpecialFolder.Desktop)

But Download Path is not treated as special folder so you can try below code
Environment.ExpandEnvironmentVariables("%USERPROFILE%\Downloads")

Thanks


#4

@Bharat, Environment.ExpandEnvironmentVariables("%USERPROFILE%\Downloads") this one worked.

Thanks a ton :slight_smile:


#5

Thanks a lot for the solution @SaranyaKishore. Currently used the suggestion provided by @Bharat. But definitely your solution will be helpful in future :slight_smile:


#6

But I am getting one more problem in this. Suppose if store the path in a variable say "downloadDirPath. then using directory.GetFiles(downloadDirPath) doesn’t give the list of files. Instead it prints the System.string[]. Actually now I want to list out all the files and search the specific file out of those.


#7

directory.GetFiles(downloadDirPath) will store the filenames in an array.
Use a for each loop to access all the files.

Untitled


#8

@SaranyaKishore

Thanks a ton, it worked.