Powershell 复制和归档目录,仅复制和归档x天以前的文件
我搜索过,也试过。我知道我不太擅长powershell,但我开始觉得这是不可能的 我想做的是移动我的C:\数据中X天以前的所有内容并存档 听起来很容易。。但如果 C:\data\Subject包含从2015年到当前的文件。我只想归档X天以前的文件。因此,新的存档路径将是C:\DataBackup,其中将包含C:\data的所有文件夹,但其中只有X天的文件 这是我用来复制和归档文件夹/文件的方法,但没有考虑年龄。。如果它这样做了,我会让它删除它从C:data中移动的文件,因为它们现在在C:\dataBackup中被备份Powershell 复制和归档目录,仅复制和归档x天以前的文件,powershell,powershell-4.0,Powershell,Powershell 4.0,我搜索过,也试过。我知道我不太擅长powershell,但我开始觉得这是不可能的 我想做的是移动我的C:\数据中X天以前的所有内容并存档 听起来很容易。。但如果 C:\data\Subject包含从2015年到当前的文件。我只想归档X天以前的文件。因此,新的存档路径将是C:\DataBackup,其中将包含C:\data的所有文件夹,但其中只有X天的文件 这是我用来复制和归档文件夹/文件的方法,但没有考虑年龄。。如果它这样做了,我会让它删除它从C:data中移动的文件,因为它们现在在C:\dat
$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
,然后以$tempPath
作为参数执行CreateFromDirectory
最后,$tempPath
被删除,因为不再需要它
请注意,在对任何重要文件运行此操作之前,请执行一些简单的测试。在脚本结束时,它将删除先前存档的文件
在我自己的测试中,它似乎正在工作,但我建议您也验证这一点。您可以这样修改脚本:
$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
,然后以$tempPath
作为参数执行CreateFromDirectory
最后,$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 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 }
我的天啊,太神奇了。非常感谢你这么做。我有另一个带有临时目录的脚本,但不知道如何删除源文件并保留文件夹,我想我永远都不会!再次感谢。我的上帝,这太神奇了。非常感谢你这么做。我有另一个带有临时目录的脚本,但不知道如何删除源文件并保留文件夹,我想我永远都不会!再次感谢。