我试图移动今天(1天)(昨天)创建的所有文件。我不知道什么是错的,因为当我设置$ which_day为-1时我得到了没有找到的文件'但是当我将$更改为0时,所有文件都会被移动。
###############PAR#############
$which_day = -1
$filter = "*.*"
$path = "E:\log_test"
$path_destination = "E:\yesterday_log"
$save_log = 1
$path_log_folder = "E:\yesterday_log"
$logname = "$path_log_folder\MOVED_FILEsLog_$(get-date -Format "yyyy-MM-dd_HHmmss").txt"
######################################
$limit_from = (Get-date (Get-date -Format d)).AddDays($which_day)
$limit_to = (Get-date (Get-date -Format d)).AddDays($which_day + 1)
$files_to_copy = Get-ChildItem -Path $path -include $filter -Recurse | Where {$_.CreationTime -ge $limit_from -and $_.CreationTime -lt $limit_to}
$files_to_copy | Select Name, Directory, LastWriteTime
Write-Host $files_to_copy.Count "files were found"
$ID=1
$OK=0
$NOK=0
Foreach ($file_to_copy in $files_to_copy){
$file_path = $file_to_copy.FullName
$file_path = $file_path.Replace($path,"")
$destination_file_path = $path_destination + $file_path
$destination_file_path = $destination_file_path.Replace($file_to_copy.Name, "")
$destination_file_path = $destination_file_path.Substring(0,$destination_file_path.Length-1)
$destination_file_path
if (!(Test-Path -Path $destination_file_path)){New-Item $destination_file_path -Type Directory}
Move-Item -Path $file_to_copy -Destination $destination_file_path -force
if (Test-Path $file_to_copy) #check if moved
{
$NOK++
"$ID WARNING: File $file_to_copy NOT MOVED" | Out-File -FilePath $logname -Append
}
else {
$OK++
"$ID INFO: File $file_to_copy MOVED OK to $destination_file_path" | Out-File -FilePath $logname -Append
}
$ID++
}
$Quantity = $files_to_copy.Count
If ($save_log = 1)
{
"***************************************" | Out-File -FilePath $logname -Append #SUMMARY
"$Quantity files were Found" | Out-File -FilePath $logname -Append #ALL FILES
"$OK files OK MOVED" | Out-File -FilePath $logname -Append #MOVED FILES
"$NOK files NOT MOVED" | Out-File -FilePath $logname -Append #UNMOVED FILES
}
更新
PS E:\> . .\yesterday.ps1
0 files were found
Out-File : Could not find a part of the path 'E:\yesterday_log\MOVED_FILEsLog_2015-04-22_020130.txt'.
At E:\yesterday.ps1:46 char:49
+ "***************************************" | Out-File -FilePath $logname -App ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OpenError: (:) [Out-File], DirectoryNotFoundException
+ FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.OutFileCommand
Out-File : Could not find a part of the path 'E:\yesterday_log\MOVED_FILEsLog_2015-04-22_020130.txt'.
At E:\yesterday.ps1:47 char:36
+ "$Quantity files were Found" | Out-File -FilePath $logname -Append #ALL FILE ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OpenError: (:) [Out-File], DirectoryNotFoundException
+ FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.OutFileCommand
Out-File : Could not find a part of the path 'E:\yesterday_log\MOVED_FILEsLog_2015-04-22_020130.txt'.
At E:\yesterday.ps1:48 char:28
+ "$OK files OK MOVED" | Out-File -FilePath $logname -Append #MOVED FILES
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OpenError: (:) [Out-File], DirectoryNotFoundException
+ FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.OutFileCommand
Out-File : Could not find a part of the path 'E:\yesterday_log\MOVED_FILEsLog_2015-04-22_020130.txt'.
At E:\yesterday.ps1:49 char:30
+ "$NOK files NOT MOVED" | Out-File -FilePath $logname -Append #UNMOVED FILES
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OpenError: (:) [Out-File], DirectoryNotFoundException
+ FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.OutFileCommand
PS E:\> cd .\log_test
PS E:\log_test> dir
Directory: E:\log_test