【发布时间】:2021-04-13 05:52:15
【问题描述】:
我正在编写一个脚本来创建每日备份(任务计划)
首先我复制文件夹“source_folder”并在“bkp”文件夹中重命名所有带有时间戳的文件,当在“source_folder”中添加新文件时只需要复制最后一个文件并重命名(我尝试使用LastModified或 LastAccessTime 但当我再次运行脚本时(第二天),如果在 soruce_folder 中没有创建其他文件,则最后一个文件将被复制 有什么建议吗?
$sourceFiles = Get-ChildItem -Path $source -Recurse
$bkpFiles = Get-ChildItem -Path $bkp -Recurse
$syncMode = 1
if(!(Test-Path $bkp)) {
Copy-Item -Path $source -Destination $bkp -Force -Recurse
Write-Host "created new folder"
$files = get-ChildItem -File -Recurse -Path $bkp
foreach($file in $files){
# Copy files to the backup directory
$newfilename = $file.FullName +"_"+ (Get-Date -Format yyyy-MM-dd-hhmmss)
Rename-Item -path $file.FullName -NewName $newfilename
}
}
elseif ((Test-Path $bkp ) -eq 1) {
$timestamp1 = (Get-Date -Format yyyy-MM-dd-hhmmss)
$timestamp = "_" + $timestamp1
@(Get-ChildItem $source -Filter *.*| Sort LastAccessTime -Descending)[0] | % {
Copy-Item -path $_.FullName -destination $("$bkp\$_$timestamp") -force
}
Write-Host "most recent files added"
}
【问题讨论】:
标签: powershell backup