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

Copy multiple files, archive and name the archive with date and hour in 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 {

}

但是没有用...

谢谢 reader 抽出时间。

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","file2","file3","file4","file5","file6","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 {}

非常感谢克林特奥利维拉,没有你的帮助我无法完成。 :)

考虑将您的 $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","file2","file3","file4","file5","file6","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","file2","file3","file4","file5","file6","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 with all 的子项时询问我的确认。

  • Remove-item行修改为:
    Remove-Item -Path (Join-Path $dest_folder $folderName) -Force -ErrorAction Stop -Recurse -Confirm:$false

3 > 我在哪里可以找到 PS 的文档?我的意思是,我尝试 google for $folderName = 但几乎一无所获。只有其他人问不同的问题。我知道在 cmd 中(我想……如果我没记错的话)你可以使用 ?或 Tab 键(我认为),您可以获得可能的命令列表。我如何检查 PS?