您现在的位置是:网站首页> 编程资料编程资料
PowerShell中使用Test-Path命令检查文件或文件夹路径是否存在示例_PowerShell_
2023-05-26
405人已围观
简介 PowerShell中使用Test-Path命令检查文件或文件夹路径是否存在示例_PowerShell_
检查文件或文件夹路径是否存在,在编程中是经常用到的功能。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开头的项目
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语句中,效果是非常好的。
您可能感兴趣的文章:
相关内容
- PowerShell读取文件内容、替换文件内容、读取限定行的例子_PowerShell_
- PowerShell中把IP转换为长整形数字的方法_PowerShell_
- PowerShell统计文件夹下文件个数的方法_PowerShell_
- PowerShell生成随机密码的方法_PowerShell_
- PowerShell中的TimeSpan时间差对象使用实例_PowerShell_
- PowerShell中常用的一些特殊运算符介绍_PowerShell_
- PowerShell把IP地址转换成二进制的方法_PowerShell_
- PowerShell获取Windows用户列表、用户信息的方法_PowerShell_
- PowerShell脚本实现添加、修改任务计划的例子_PowerShell_
- PowerShell中查询错误编号信息的2个方法_PowerShell_
