Powershell 基本文件操作
请参阅我在PS中的第一个代码,如下所示: 我创建了一个Powershell 基本文件操作,powershell,Powershell,请参阅我在PS中的第一个代码,如下所示: 我创建了一个Test.ps1文件,并包含以下代码: $path = D:\Five-Levels_Deep_Subfolder\Data $file = A_Very_Long_INI_FileName.ini #If $file exists, Delete it: if (Test-Path + $path) { Remove-Item $path + $file } #Run the following Application: &a
Test.ps1
文件,并包含以下代码:
$path = D:\Five-Levels_Deep_Subfolder\Data
$file = A_Very_Long_INI_FileName.ini
#If $file exists, Delete it:
if (Test-Path + $path) { Remove-Item $path + $file }
#Run the following Application:
& $path + myApplication.exe
但是,结果试图用一些可怕的错误消息来吓唬我。事实上,上面的代码行没有一行是无错误的
请容忍一个学PS的蹒跚学步的孩子,并帮助我使其获得巨大成功:-)。试试这个
$path = "D:\Five-Levels_Deep_Subfolder\Data"
$file = "A_Very_Long_INI_FileName.ini"
$filepath = join-path $path $file
#If $filepath exists, Delete it:
if (Test-Path $filepath) { Remove-Item $filepath}
#Run the following Application:
& ($path + "\myApplication.exe")
谢谢你,克里斯蒂安。除了($path+“\IllustratorPortable.exe”)错误消息报告“未识别为cmdlet、函数的名称,…”之外,一切正常。请您再次查看!为什么不为exe使用join path呢?@jpmc是的,但希望用户知道从错误代码开始的两种方式。我相信通常认为手动连接文件路径是一种倒退的做法。连接路径更可靠,因为它处理尾随和前导目录分隔符。例如,如果将
$path
设置为“D:\Five-Levels\u Deep\u Subfolder\Data\”
,则如上手动操作将产生“D:\Five-Levels\u Deep\u Subfolder\Data\\myApplication.exe”
。