当前位置: 移动技术网 > IT编程>脚本编程>Shell > PowerShell中使用Test-Path命令检查文件或文件夹路径是否存在示例

PowerShell中使用Test-Path命令检查文件或文件夹路径是否存在示例

2017年12月08日  | 移动技术网IT编程  | 我要评论

检查文件或文件夹路径是否存在,在编程中是经常用到的功能。powershell对于这个需求提供了一个非常便捷的cmdlet,那就是test-path!呵呵,看个名字,你就明白它有多么专业了。看看关于它的基本介绍先:

test-path,检查路径是否存在。

语法:test-path <路径>
说明:这里的路径可以是:文件、文件夹、hklm路径、环境变量env:路径

下面来举一些例子,让大家更方便懂:

复制代码 代码如下:
test-path d:\q.txt
test-path c:\scripts\archive -pathtype container
test-path "hkcu:\software\microsoft\driver signing"
test-path alias:\gci
test-path env:\username
test-path c:\scripts\archive -pathtype leaf
test-path c:\scripts\archive\*.ps1
test-path c:\scripts\archive\* -include *.ps1, *.vbs
test-path c:\scripts\archive\* -include test*.ps1, test*.vbs
test-path c:\scripts\archive\* -exclude *.ps1
test-path c:\scripts\archive\* -exclude *.gif, *.jpg
test-path d:\网站备份\2013-04-*\cnsm* #检查2013-04开头的目录下有没有cnsm开头的项目

看完之后,冰雪聪明的你,是不是觉得这玩意儿太tm的强大了?唉,这是它为什么叫powershell的原因了。

说说返回值,test-path的返回值为true或false,所以,你可以把它放到if语句中,效果是非常好的。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网