开发者

PowerShell: How to check for multiple conditions (folder existence)

I am in the process of writing a script to make changes to folder permissions. Before it does that I would to do some checking to make sure that I am working in the correct directory. My problem is how do I check to see if four subf开发者_如何转开发olders (i.e. Admin, Workspace, Com, & Data) exists before the script progresses. I assume I would be using Test-Path on each directory.


What's wrong with the following?

if ( (Test-Path $path1) -and (Test-Path $path2) ) { 

}


Hint:

Remember to specify -LiteralPath - stops any possible misinterpretation. I've "been there" (so to speak) with this one, spending hours debugging code.


Test-Path can check multiple paths at once. Like this:

Test-Path "c:\path1","c:\path2"

The output will be an array of True/False for each corresponding path.

This could be especially helpful if you have a lot of files/folders to check.

Check if all paths are exists:

if ((Test-Path $arraywithpaths) -notcontains $false) {...}

Same way for the non-existence:

if ((Test-Path $arraywithpaths) -contains $false) {...}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜