Check if file exist and run a batch file in PowerShell?
This may be a simple question, but I am new to PowerShell and could not find a way to do it. Basically, I have to run a .BAT file if a specified file does not exist. The file name is in a patten like "mmddyyy.dat" in a folder, where mmddyyyy is today's month, day(0 prefix if < 10) and year. Pseudo codes would be something like this:
$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?
The command is :
test-path .\example.txt
Returns True or False
For Docs how about official documentation? That's where I check. http://technet.microsoft.com/en-us/scriptcenter/dd742419.aspx
also eggheadcafe.com has a lot of examples: http://www.eggheadcafe.com/conversationlist.aspx?groupid=2464&activetopiccard=0
Although I haven't tried regex in poweshell this may help you:
http://www.eggheadcafe.com/software/aspnet/33029659/regex-multiline-question.aspx
I'd recommend making a reusable function like the following:
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
}
精彩评论