簡體   English   中英

Powershell 復制-即使文件夾存在,項目也不會復制

[英]Powershell Copy-Item not copying even when folder exists

我連續兩次調用Copy-Item但第二次沒有復制文件。 兩者都將相同的文件復制到兩個不同的文件夾。 兩個文件夾都存在。 但是第二個從不復制。 我嘗試注釋掉第一個,但第二個仍然沒有復制。

$deliveryFolder = "C:\Users\administrator.GTCS\Desktop\Delivery\1.5.6.1140\"    
$localFolder = "C:\WebServerGateway\"    
$fileSharingServerManifestFileName = "GTCS_GCS_FileSharingServer_Manifest.xml"

Copy-item -Path $localFolder$fileSharingServerManifestFileName -Destination $deliveryFolder"GcsData\Configurations\Platforms\DEV\Web Server Gateway" -Force

Copy-item -Path $localFolder$fileSharingServerManifestFileName -Destination $deliveryFolder"GcsData\Configurations\Platforms\INT\Web Server Gateway" -Force

如果要以這種方式組合字符串,則需要將它們用雙引號括起來,以擴展變量:

$deliveryFolder = "C:\Users\administrator.GTCS\Desktop\Delivery\1.5.6.1140\"    
$localFolder = "C:\WebServerGateway\"    
$fileSharingServerManifestFileName = "GTCS_GCS_FileSharingServer_Manifest.xml"

    Copy-item -Path "$localFolder$fileSharingServerManifestFileName" -Destination "$($deliveryFolder)GcsData\Configurations\Platforms\DEV\Web Server Gateway" -Force
    
    Copy-item -Path "$localFolder$fileSharingServerManifestFileName" -Destination "$($deliveryFolder)GcsData\Configurations\Platforms\INT\Web Server Gateway" -Force

請注意,我在-Destination參數中使用了子表達式$() 因為在您的情況下,解析器可能無法確定變量名稱的結束位置,因此字符串可能無法正確擴展。

也就是說,arguments 都不是特別漂亮/可讀,所以我建議不要使用這種語法。 連接或更好地使用Join-Path派生 arguments 是更好的選擇:

$Path = $localFolder + $fileSharingServerManifestFileName
$Dest = $deliveryFolder + "GcsData\Configurations\Platforms\INT\Web Server Gateway"

注意:連接迫使您注意用反斜杠分隔Join-Path中的-Parent / -Child arguments

或者:

$Path = Join-Path $localFolder $fileSharingServerManifestFileName
$Dest = Join-Path $deliveryFolder "GcsData\Configurations\Platforms\INT\Web Server Gateway"

然后簡單地修改命令:

Copy-item -Path $Path -Destination $Dest -Force
Copy-item -Path $Path -Destination $Dest -Force

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM