复制和存档目录以及仅早于 x 天的文件
Copy and archive directories and only files older than x Days
我搜索并尝试过。我知道我不太擅长 powershell,但我开始觉得这是不可能的。
我想做的是移动我的 C:\Data 中 X 天前的所有内容并将它们存档。
听起来很简单..但是如果
C:\data\Subject 包含从 2015 年到当前的文件。我只想归档 X 天前的文件。所以新的存档路径将是 C:\DataBackup 并且在那里它将包含 C:\data 的所有文件夹,但其中唯一的文件将是 X 天前的..
这是我用来复制和存档 folders/files 的内容,但它没有考虑年龄。如果有,我会让它删除它从 [=21= 移动的文件] 因为它们现在备份在 C:\dataBackup
$sourcePath = "C:\Data"
$path = "C:\DataBackup"
$source = Get-ChildItem -Path $sourcePath -Directory
Add-Type -assembly "system.io.compression.filesystem"
Foreach ($s in $source)
{
$destination = Join-path -path $path -ChildPath "$($s.name).zip"
If(Test-path $destination) {Remove-item $destination}
[io.compression.zipfile]::CreateFromDirectory($s.fullname, $destination)}
您可以这样修改脚本:
$sourcePath = "C:\Users\kim_p\Downloads"
$path = "C:\DataBackup"
$tempPath = "C:\DataTemp"
# Any file younger than $limit will be excluded from the zip archive
$limit = (Get-Date).AddDays(-10)
$source = Get-ChildItem -Path $sourcePath -Directory
# Make sure the temporary folder doesn't exist
If(Test-path $tempPath) {Remove-item $tempPath -Recurse}
Add-Type -assembly "system.io.compression.filesystem"
Foreach ($s in $source)
{
$destination = Join-path -path $path -ChildPath "$($s.name).zip"
If(Test-path $destination) {Remove-item $destination}
# Create the temporary directory
New-Item $tempPath -Type Directory
# Copy files older than the date set in $limit to the temporary folder $tempPath
Get-ChildItem -Path $s.fullname -Recurse -Force | Where-Object { $_.CreationTime -lt $limit } | Copy-Item -Destination $tempPath
# Create the zip archive using the files copied to $tempPath
[io.compression.zipfile]::CreateFromDirectory($tempPath, $destination)
# Remove the temporary folder and all the files inside of it
Remove-item $tempPath -Recurse
# Remove the source files added to the archive
Get-ChildItem -Path $s.fullname -Recurse -Force | Where-Object { $_.CreationTime -lt $limit } | Remove-Item -Recurse -Force
}
我添加了一个名为 $tempPath
的文件夹,其中包含将由 CreateFromDirectory
添加到存档中的文件。早于某个定义限制 $limit
的文件首先被复制到临时文件夹 $tempPath
,然后 CreateFromDirectory
以 $tempPath
作为参数执行。
最后 $tempPath
被删除,因为不再需要它。
请注意,在您 运行 对任何重要文件执行此操作之前,请先进行一些简单测试。在脚本的末尾,它将删除之前存档的文件。
在我自己的测试中它似乎有效,但我建议您也验证一下。
如果您使用 PowerShell v5.0 或更高版本,试试这个:
$sourcePath = "C:\Users\kim_p\Downloads"
$tempPath = "C:\DataBackup"
$limit = (Get-Date).AddDays(-10)
remove-item -Path $tempPath -Force -ErrorAction Ignore -Recurse
Get-ChildItem -Path $sourcePath -Recurse | Where {$_.PSISContainer -eq $true -or ($_.PSISContainer -eq $false -and $_.CreationTime -lt $limit) } | %{ Copy-Item $_.FullName -Destination $_.FullName.Replace($sourcePath, $tempPath) -Recurse -Force}
Get-ChildItem -Path $tempPath -Directory | %{Compress-Archive -Path $_.FullName -DestinationPath ($_.FullName + ".zip"); remove-item -Path $_.FullName -Recurse -Force }
我搜索并尝试过。我知道我不太擅长 powershell,但我开始觉得这是不可能的。
我想做的是移动我的 C:\Data 中 X 天前的所有内容并将它们存档。
听起来很简单..但是如果 C:\data\Subject 包含从 2015 年到当前的文件。我只想归档 X 天前的文件。所以新的存档路径将是 C:\DataBackup 并且在那里它将包含 C:\data 的所有文件夹,但其中唯一的文件将是 X 天前的..
这是我用来复制和存档 folders/files 的内容,但它没有考虑年龄。如果有,我会让它删除它从 [=21= 移动的文件] 因为它们现在备份在 C:\dataBackup
$sourcePath = "C:\Data"
$path = "C:\DataBackup"
$source = Get-ChildItem -Path $sourcePath -Directory
Add-Type -assembly "system.io.compression.filesystem"
Foreach ($s in $source)
{
$destination = Join-path -path $path -ChildPath "$($s.name).zip"
If(Test-path $destination) {Remove-item $destination}
[io.compression.zipfile]::CreateFromDirectory($s.fullname, $destination)}
您可以这样修改脚本:
$sourcePath = "C:\Users\kim_p\Downloads"
$path = "C:\DataBackup"
$tempPath = "C:\DataTemp"
# Any file younger than $limit will be excluded from the zip archive
$limit = (Get-Date).AddDays(-10)
$source = Get-ChildItem -Path $sourcePath -Directory
# Make sure the temporary folder doesn't exist
If(Test-path $tempPath) {Remove-item $tempPath -Recurse}
Add-Type -assembly "system.io.compression.filesystem"
Foreach ($s in $source)
{
$destination = Join-path -path $path -ChildPath "$($s.name).zip"
If(Test-path $destination) {Remove-item $destination}
# Create the temporary directory
New-Item $tempPath -Type Directory
# Copy files older than the date set in $limit to the temporary folder $tempPath
Get-ChildItem -Path $s.fullname -Recurse -Force | Where-Object { $_.CreationTime -lt $limit } | Copy-Item -Destination $tempPath
# Create the zip archive using the files copied to $tempPath
[io.compression.zipfile]::CreateFromDirectory($tempPath, $destination)
# Remove the temporary folder and all the files inside of it
Remove-item $tempPath -Recurse
# Remove the source files added to the archive
Get-ChildItem -Path $s.fullname -Recurse -Force | Where-Object { $_.CreationTime -lt $limit } | Remove-Item -Recurse -Force
}
我添加了一个名为 $tempPath
的文件夹,其中包含将由 CreateFromDirectory
添加到存档中的文件。早于某个定义限制 $limit
的文件首先被复制到临时文件夹 $tempPath
,然后 CreateFromDirectory
以 $tempPath
作为参数执行。
最后 $tempPath
被删除,因为不再需要它。
请注意,在您 运行 对任何重要文件执行此操作之前,请先进行一些简单测试。在脚本的末尾,它将删除之前存档的文件。
在我自己的测试中它似乎有效,但我建议您也验证一下。
如果您使用 PowerShell v5.0 或更高版本,试试这个:
$sourcePath = "C:\Users\kim_p\Downloads"
$tempPath = "C:\DataBackup"
$limit = (Get-Date).AddDays(-10)
remove-item -Path $tempPath -Force -ErrorAction Ignore -Recurse
Get-ChildItem -Path $sourcePath -Recurse | Where {$_.PSISContainer -eq $true -or ($_.PSISContainer -eq $false -and $_.CreationTime -lt $limit) } | %{ Copy-Item $_.FullName -Destination $_.FullName.Replace($sourcePath, $tempPath) -Recurse -Force}
Get-ChildItem -Path $tempPath -Directory | %{Compress-Archive -Path $_.FullName -DestinationPath ($_.FullName + ".zip"); remove-item -Path $_.FullName -Recurse -Force }