复制多个文件,存档并在 Powershell 中使用日期和小时命名存档

问题描述

我有一个代码

$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 正在尝试查询 enter image description here

如果您将 $sursa_file 转换为数组并执行 foreach-object,那么它会在每次循环中查找正确的文件路径,尽管您是可变的,因此 Join-Path $sursa_folder $sursa_file 变为 enter image description here

这是我的尝试:您可以自己填写 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 确认?