Batch file powershell中的批处理执行/查找等效于什么
在批处理脚本中,我可以执行以下操作Batch file powershell中的批处理执行/查找等效于什么,batch-file,powershell,Batch File,Powershell,在批处理脚本中,我可以执行以下操作 execute command | findstr.exe "string_to_look" 如果不存在,则%errorlevel%与0不同 在Powershell中,select字符串似乎没有相同的行为 好吧,如果您只想知道字符串是否存在,您可以这样做(使用dir作为示例): 如果您只想知道字符串是否存在,可以这样做(以dir为例): 根据条件筛选输出,并使用if语句测试是否存在任何匹配项: PS C:\> dir test* Direc
execute command | findstr.exe "string_to_look"
如果不存在,则%errorlevel%与0不同
在Powershell中,select字符串似乎没有相同的行为 好吧,如果您只想知道字符串是否存在,您可以这样做(使用
dir
作为示例):
如果您只想知道字符串是否存在,可以这样做(以
dir
为例):
根据条件筛选输出,并使用
if
语句测试是否存在任何匹配项:
PS C:\> dir test*
Directory: C:\
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 23/12/2013 16:12 19820 test.html
PS C:\> if (dir | ? { $_ -match 'test' }) { echo 'yes' }
yes
PS C:\> if (dir | ? { $_ -match 'testx' }) { echo 'yes' }
PS C:\>
但是请注意,如果该命令是Powershell cmdlet,则需要测试特定属性,因为您看到的显示行不是测试中的内容:
PS C:\> if (dir | ? { $_.Length -eq 19820 }) { echo 'yes' }
yes
对于我的示例命令,仅测试
$。
仅测试名称。根据条件筛选输出,并使用if
语句测试是否存在任何匹配项:
PS C:\> dir test*
Directory: C:\
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 23/12/2013 16:12 19820 test.html
PS C:\> if (dir | ? { $_ -match 'test' }) { echo 'yes' }
yes
PS C:\> if (dir | ? { $_ -match 'testx' }) { echo 'yes' }
PS C:\>
但是请注意,如果该命令是Powershell cmdlet,则需要测试特定属性,因为您看到的显示行不是测试中的内容:
PS C:\> if (dir | ? { $_.Length -eq 19820 }) { echo 'yes' }
yes
对于我的示例命令,仅测试
$。
仅测试名称。findstr
仍然在PowerShell中工作,因为它是一个外部命令。然而,它不是很时髦,所以我不推荐使用它
您可以将Where Object
cmdlet(或其别名?
)与各种。-like
运算符与findstr
的默认行为最为相似,只是在查找部分匹配时需要在搜索字符串的开头和/或结尾添加通配符:
if (& command | ? { $_ -like '*string_to_look*' }) {
...
}
-match
运算符将输入与正则表达式匹配(如findstr/r
):
您还可以使用Select-String
cmdlet,默认情况下,该cmdlet将使用正则表达式,但可以指示使用与参数的简单匹配:
if (& command | Select-String -SimpleMatch 'string_to_look') {
...
}
如果
Where Object
或Select String
未找到匹配项,则管道的结果为$null
,这将导致。否则,结果是一个非空数组,其计算结果为$true
findstr
在PowerShell中仍然有效,因为它是一个外部命令。然而,它不是很时髦,所以我不推荐使用它
您可以将Where Object
cmdlet(或其别名?
)与各种。-like
运算符与findstr
的默认行为最为相似,只是在查找部分匹配时需要在搜索字符串的开头和/或结尾添加通配符:
if (& command | ? { $_ -like '*string_to_look*' }) {
...
}
-match
运算符将输入与正则表达式匹配(如findstr/r
):
您还可以使用Select-String
cmdlet,默认情况下,该cmdlet将使用正则表达式,但可以指示使用与参数的简单匹配:
if (& command | Select-String -SimpleMatch 'string_to_look') {
...
}
如果
Where Object
或Select String
未找到匹配项,则管道的结果为$null
,这将导致。否则,结果是一个非空数组,其计算结果为$true
您可以这样做:
if (someCommand | Select-String "string_to_look")
{
# found!
}
$found = someCommand | Select-String "string_to_look"
if ($found)
{
# found!
}
或者像这样:
if (someCommand | Select-String "string_to_look")
{
# found!
}
$found = someCommand | Select-String "string_to_look"
if ($found)
{
# found!
}
它工作的原因是
Select String
如果没有找到任何内容,则返回$null
,其计算结果为$false
,如果确实找到某个内容,则返回匹配信息(或匹配信息的数组),其计算结果为true
,您可以这样做:
if (someCommand | Select-String "string_to_look")
{
# found!
}
$found = someCommand | Select-String "string_to_look"
if ($found)
{
# found!
}
或者像这样:
if (someCommand | Select-String "string_to_look")
{
# found!
}
$found = someCommand | Select-String "string_to_look"
if ($found)
{
# found!
}
它工作的原因是Select String
如果没有找到任何内容,则返回$null
,其计算结果为$false
,如果确实找到了某个内容,则返回匹配信息
(或匹配信息
的数组),它的计算结果为true
在使用powershell时,我仍然使用findstr
。在您的命令之后,Echo$?
将根据结果得到True/False。在使用powershell时,我仍然为此目的使用findstr
。Echo$?
在您的命令之后,您将根据结果得到True/False这是我将采用的方法。但是如果someCommand类似于“dir”,那么这将不起作用,因为传递给Select String的对象是DirectoryInfo对象,不是字符串。这是我将采用的方法。但是如果someCommand类似于“dir”,那么这将不起作用,因为传递给Select String的对象是DirectoryInfo对象,而不是字符串。