【发布时间】:2022-01-02 14:01:18
【问题描述】:
过去 2 周以来,我一直在编写 Powershell 脚本,但在这方面没有取得太大进展。
所以我试图将一个名为 version.properties 的文件从我的 gradle 项目的根目录复制到“src/main/resources”、“src/main/webapp”和“src/main/application”等子目录中. 如果我硬编码它正在工作的路径,但我试图通过找到目录并将我的文件复制到该目录来使其通用。
我希望在我运行我的 powershell 脚本后将我的 version.properties 文件复制到“resources”、“webapp”和“application”目录。
我该怎么做?任何建议表示赞赏。
$SourceDirectory = "Projectroot\version.properties"
$folders = gci $SourceDirectory -Recurse -Directory
$jar = "src/main/resources"
$ear = "src/main/application"
$war = "src/main/webapp"
foreach ($folder in Sfolders) {
if (Test-Path $folder/$jar) {
write-host "copying to $folder/$jar"
Copy-Item-Path "{$SourceDirectory}\version.properties" -Destination $folder/$jar -Recurse -Force
}
elseif (Test-Path $folder/$ear) {
write-host "copying to $folder/$ear"
Copy-Item-Path "{$SourceDirectory}\version.properties" -Destination $folder/$ear -Recurse -Force
}
elseif (Test-Path $folder/$war) {
write-host "copying to $folder/$war"
Copy-Item-Path "{$SourceDirectory}\version.properties" -Destination $folder/$war -Recurse -Force
}
else {
Write-Host "No such path"
}
}
【问题讨论】:
-
那么到目前为止你有什么?只是运行
gci查找该文件并在此之后执行三个复制命令的情况吗? -
是的,绝对正确。我有一个 gci,然后我运行一个 for 循环来查找这些文件夹,如果我找到那个文件夹,我会尝试将该文件复制到我想要的文件夹中。
-
那么问题出在哪里?你的代码是什么样的?由于您确实已经定义了文件夹,因此您想将其复制到仅使用搜索根目录的相对部分或从根目录构建绝对路径应该可以吗?
-
我的代码看起来如何。我已经在问题中添加了它。
-
您的
$Jar、$Ear和$War行看起来有问题。那些看起来你打算定义一个 $Var ...但你没有任何任务。
标签: powershell powershell-3.0 powershell-4.0