问题描述
$sursa_folder="C:\folder\folder\folder\folder"
$sursa_file="file"
$dest_folder="F:\+\name\Dump"
copy-Item (Join-Path $sursa_folder $sursa_file) -Destination $dest_folder
try {Compress-Archive -Path (Join-Path $dest_folder $sursa_file) -CompressionLevel Optimal -DestinationPath ((Join-Path $dest_folder $sursa_file) + (Get-Date).ToString('ddMMyyyy-hh.mm.ss') + ".zip") -ErrorAction SilentlyContinue
#Compress-Archive -Path (Join-Path $dest_folder $sursa_file) -CompressionLevel Optimal -DestinationPath ("X:\" + (Get-Date).ToString('ddMMyyyy-hh.mm.ss') + ".zip") -ErrorAction SilentlyContinue Remove-Item (Join-Path $dest_folder $sursa_file) -Force -ErrorAction Stop Write-Host "finalizat OK"
}
catch {
write-host "Eroare: $_"
}
finally {
}
我在想这个:
$sursa_folder="C:\folder\folder\folder\folder"
$sursa_file="file1""file2""file3""file4""file5""file6""file7"
$dest_folder="F:\+\name\Dump"
copy-Item (Join-Path $sursa_folder $sursa_file) -Destination $dest_folder
try {
Compress-Archive -Path (Join-Path $dest_folder $sursa_file) -CompressionLevel Optimal -DestinationPath ((Join-Path $dest_folder $sursa_file) + (Get-Date).ToString('ddMMyyyy-hh.mm.ss') + ".zip") -ErrorAction SilentlyContinue
#Compress-Archive -Path (Join-Path $dest_folder $sursa_file) -CompressionLevel Optimal -DestinationPath ("X:\" + (Get-Date).ToString('ddMMyyyy-hh.mm.ss') + ".zip") -ErrorAction SilentlyContinue
Remove-Item (Join-Path $dest_folder $sursa_file) -Force -ErrorAction Stop
Write-Host "finalizat OK"
}
catch {
write-host "Eroare: $_"
}
finally {
}
但无济于事......
感谢读者抽出宝贵时间。
L.E.:现在是完成结果。
# this function will zip an entire folder (in your case the destination folder)
function ZipFiles( $zipfilename,$sourcedir )
{
Add-Type -Assembly System.IO.Compression.FileSystem
$compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
[System.IO.Compression.ZipFile]::CreateFromDirectory($sourcedir,$zipfilename,$compressionLevel,$false)
}
$sursa_folder="C:\folder\folder\folder\folder"
$sursa_file=@("file1","file2","file3","file4","file5","file6","file7") # <= eaxample name files,rename as needed.
$dest_folder= "F:\+\name\Dump"
$folderName = "File_Folder" # <= rename as needed.
$destinationfolder = New-Item -Path (Join-Path $dest_folder $folderName) -ItemType Directory
foreach($file in $sursa_file)
{
copy-Item (Join-Path $sursa_folder $file) -Destination (Join-Path $dest_folder $folderName) # this copies the item from source to destination >> destination is a folder within the destination
#Now you will have multiple files ("file1","file7") that are copied from source to a destination folder
}
try {
ZipFiles -sourcedir (Join-Path $dest_folder $folderName) -zipfilename ((Join-Path $dest_folder $folderName) + (Get-Date).ToString('ddMMyyyy-hh.mm.ss') + ".zip") # zip the destination folder and give it a new name
Remove-Item -Path (Join-Path $dest_folder $folderName) -Force -ErrorAction Stop -Recurse -Confirm:$false # this will delete the unzipped old folder
Write-Host "finalizat OK"
}
catch {
write-host "Eroare: $_"
}
finally {}
非常感谢 Clint Oliveira,如果没有你的帮助,我将无法成功。 :)
解决方法
考虑将您的 $sursa_file
变量转换为数组,然后像这样执行 foreach ($file in $sursa_file)
:
$sursa_file=@("file1","file2","file3","file4","file5","file6","file7")
您收到错误,因为 Join-Path $sursa_folder $sursa_file
正在尝试查询
如果您将 $sursa_file
转换为数组并执行 foreach-object
,那么它会在每次循环中查找正确的文件路径,尽管您是可变的,因此 Join-Path $sursa_folder $sursa_file
变为
这是我的尝试:您可以自己填写 catch/finally 操作。
# this function will zip an entire folder (in your case the destination folder)
function ZipFiles( $zipfilename,$sourcedir )
{
Add-Type -Assembly System.IO.Compression.FileSystem
$compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
[System.IO.Compression.ZipFile]::CreateFromDirectory($sourcedir,$zipfilename,$compressionLevel,$false)
}
$sursa_folder="C:\folder\folder\folder\folder"
$sursa_file=@("file1","file7")
$dest_folder= "F:\+\name\Dump"
$folderName = Read-Host "Please Enter the folder Name"
$destinationfolder = New-Item -Path (Join-Path $dest_folder $folderName) -ItemType Directory
foreach($file in $sursa_file)
{
Copy-Item (Join-Path $sursa_folder $file) -Destination (Join-Path $dest_folder $folderName) # this copies the item from source to destination >> destination is a folder within the destination
#Now you will have multiple files ("file1","file7") that are copied from source to a destination folder
}
try {
ZipFiles -sourcedir (Join-Path $dest_folder $folderName) -zipfilename ((Join-Path $dest_folder $folderName) + (Get-Date).ToString('ddMMyyyy-hh.mm.ss') + ".zip") # zip the destination folder and give it a new name
Remove-Item (Join-Path $dest_folder $folderName) -Force -ErrorAction Stop # this will delete the unzipped old folder
Write-Host "finalizat OK"
}
catch {
write-host "Eroare: $_"
}
finally {}
,
1 > 我想给那个文件夹一个永久的名字。将 File_Folder
更改为您想要的文件夹名称。
- 用
$folderName = Read-Host "Please Enter the folder Name"
替换$folderName = "File_Folder"
2 > 我不希望 PowerShell 要求我确认是否要删除所有带有 item 的子项。
- 将
Remove-item
行修改为:Remove-Item -Path (Join-Path $dest_folder $folderName) -Force -ErrorAction Stop -Recurse -Confirm:$false
3 > 在哪里可以找到此 PS 文档?我的意思是,我试图用 google 搜索 $folderName = 并且几乎一无所获。只有其他人提出不同的问题。我知道在 cmd 中(我想......如果我没记错的话)你可以使用 ?或 tab 键(我认为),您可以获得可能命令的列表。我如何用 PS 确认?
- PowerShell 文档:https://docs.microsoft.com/en-us/powershell/
- 可能的命令列表> 使用“Tab 键”,但是您需要输入动词,例如。
get-
或set-
等此片段来自 PowerShell ISE