Programming-idioms.org View history. Check if file exists; Create folder; All content CC-BY-SA? How can I detect when a USB drive is attached to a computer in Windows, Linux, or Mac? The only way I have seen online to do this is to iterate the drives, but I don't think there is a very good way to do that cross-platform (e.g. File.listRoots in Linux only returns '/'). Recently, I felt overly annoyed by my messy download folder on my personal computer. There were too many files, and it was in a total mess. Being someone with mild OCD, I was annoyed.
We have setup a new folder structure for our employees and are just beginning to implement. The new folder structure to begin with will be created when a new employee is hired. We are starting a new LF Form that, when completed, gets filed into an employees folder. The problem is that existing employees will not have the new folder structure. So we want to be able to (within Workflow) look to see if the employee folder exists or not. If it does, then file the document in the correct folder. If not, then Invoke the Employee Folder Creation workflow and then file the document in the appropriate employee folder.
How do I setup the logic to look if that folder already exists though?
In this little article I describe how to use the cmdlet Test-Path to check if a folder exists. Type Get-Help Test-Path for built-in information. I also briefly demonstrate how to use the .NET class method Exists() from the class System.IO.Directory.
How To Check If A Folder Exists With PowerShell
You can use something like this for verification on the command line:
Remember that you need single or double quotes around the path if it contains a space. Single quotes are recommended, since they don't expand/substitute/interpolate variables.
To explicitly make sure it's a directory and not a file, use the -PathType parameter which accepts the following values:
- Any
- Leaf (file)
- Container (directory/folder)
Screenshot Example
Script Usage
In a script, you would typically use it in an if statement. To negate and check if the folder or file does not exist, use either '!' or '-not', and remember to enclose the Test-Path statement in parentheses.
Also remember that if the path or folder name contains a space, you need to surround the entire path in quotes. Single quotes or double quotes will work the same if there are no 'expandable' parts in the path or folder name, but the slightly safer choice is single quotes. This is what PowerShell defaults to when you auto-complete names with tab at the prompt.
You should also be made aware of the parameter -LiteralPath to Test-Path, that you can see in the second example above. This also works if your file contains characters like brackets that causes the -Path parameter to expand the path since it supports wildcard syntax by default. Use -LiteralPath if you don't need expansion from -Path as it's the safer choice for oddly named files.
Create A Directory If It Does Not Exist
To create a directory if it does not exist, this is a very robust example of how you might want to handle it. Adapt to suit your needs.
This code was put in the file 'NewDirDemo.ps1' that you see me using below.
Demonstration of use in the following screenshot.
Enumerating Possible PathType Values
A small 'trick' to see the possible enumeration values for -PathType is to use one that doesn't exist, like this:
C# Check If Folder Exists
Using The .NET System.IO.Directory Class Method 'Exists'
You can also use the Exists() method from the .NET System.IO.Directory class, which requires a full path:
If you want to check if the directory the script/program is currently in contains a subdirectory, you can use the trick I demonstrate below - where I check if there's a subdirectory called 'Windows'.
In PowerShell, the namespace 'System' doesn't have to be typed in explicitly, so you can omit it.