检查文件是否存在并在PowerShell中运行批处理文件?
这可能是一个简单的问题,但我是PowerShell的新手,无法找到解决方法。基本上,如果指定的文件不存在,我必须运行.BAT文件。文件名称的格式类似于文件夹中的“mmddyyy .dat”,其中mmddyyyy是今天的月份,日期(如果是< 10,则为0前缀)和年份。伪代码将是这样的:检查文件是否存在并在PowerShell中运行批处理文件?
$File = "C:\temp\*mmddyyyy*.dat" # how to parse Get-Date mmddyyyy and build this pattern?
#if $File exist # check any file exist?
.\myBatch.bat # run the bat file, can I run it in hidden mode?
我建议你做可重复使用的功能如下:
function GetDateFileName
{
$date = Get-Date
$dateFileName = "$(get-date -f MMddyyyy).dat"
return $dateFileName
}
$fileName = GetDateFileName
$filePath = "c:\temp\" + $fileName
if([IO.File]::Exists($filePath) -ne $true)
{
#do whatever
}
的命令是:
test-path .\example.txt
返回TRUE或FALSE
对于如何对官方文档的文档?这就是我检查的地方。 http://technet.microsoft.com/en-us/scriptcenter/dd742419.aspx
也eggheadcafe.com有很多例子: http://www.eggheadcafe.com/conversationlist.aspx?groupid=2464&activetopiccard=0
虽然我还没有尝试过的正则表达式中poweshell这可以帮助你:
http://www.eggheadcafe.com/software/aspnet/33029659/regex-multiline-question.aspx
how t根据今天的日期公会一个字符串,模式为mmddyyyy?例如今天的“11132009”。 – 2009-11-13 22:32:23
如果我想将两个参数作为参数添加到函数中,并以“{0} {1:d2} {2:d2} {3} {4}”-f arg [0],的格式返回结果。 ..,ARG [1]。我怎样才能使这个功能?我通过调用GetDateFleName(“C:\ temp \ *”,“* .dat”)失败。 – 2009-11-13 23:00:21
'$ dateFileName =“{0:MMddyyyy} .dat”-f(Get-Date)'会稍微短一些,不太复杂。 – Joey 2009-11-14 01:13:24
您的参数添加到功能,像这样: 函数foo([字符串] $ FOO = “富”,[字符串] $栏= “巴”) { 写主机 “精氨酸:$ foo” 的; 写主机“Arg:$ bar”; } 和调用功能,像这样: 富“参数1”“参数2” – BlueSam 2009-11-15 05:22:01