检查文件是否存在并在 PowerShell 中运行批处理文件? [英] Check if file exist and run a batch file in PowerShell?
本文介绍了检查文件是否存在并在 PowerShell 中运行批处理文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这可能是一个简单的问题,但我是 PowerShell 的新手,找不到解决方法.基本上,如果指定的文件不存在,我必须运行 .BAT 文件.文件名在文件夹中的格式类似于mmddyyy.dat",其中 mmddyyyy 是今天的月、日(如果 <10 则为 0 前缀)和年.伪代码将是这样的:
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?
推荐答案
我建议创建一个可重用的函数,如下所示:
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
}
这篇关于检查文件是否存在并在 PowerShell 中运行批处理文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文