簡體   English   中英

Powershell 無法僅將文件從一個目錄移動到另一個目錄?

[英]Powershell Unable to move only files from a one dire to another directory?

我正在嘗試僅將文件(其中很舊)從一個 source_dir 移動到另一個archiv_dir。 source_dir 包含一個子文件夾,我希望將其保留在那里並且只想移動文件。

我正在過濾最新文件並將舊文件移至存檔。 下面是文件夾結構和代碼

#Source Dir

Log_Sub1  #child dir

Log1.log
Log2.log
Log3.log
Log4.log
Log5.log

powershell

function Keep_Logs_And_Move_Files([int]$No_files_to_keep)
    {
        Write-Host "Count of keep files: " $No_files_to_keep
        # Check and create Archive dir if not exist

        Write-Host "Count of keep files: " $No_files_to_keep
        # Check and create Archive dir if not exist
        $source_dir="D:\Log_Test_Direcotories\Log2"
        $archiv_dir="D:\Log_Test_Direcotories\Log2_archiv"
        $count_of_Files= Get-ChildItem $source_dir | Measure-Object | Select-Object Count
        $existing_files= ls $source_dir

        IF ($count_of_Files.Count -gt $No_files_to_keep){

            # Get the number of latest files
            $files_to_keep=Get-ChildItem -Path $source_dir | Where-Object { -not $_.PsIsContainer } | Sort-Object LastWriteTime -Descending | Select-Object -first $No_files_to_keep

            #compare exsting all files with files_to_keep to get  excluded_files that need to be moved out
            $exclude_files=Compare-Object -ReferenceObject ($files_to_keep | Sort-Object ) -DifferenceObject ($existing_files | Sort-Object)

            #Filter for oly files not directory
            #$Filtered_files=gci -af $exclude_files

            #Write-Host "Filtered files $Filtered_files"

            #Move exclude_files to archive
            foreach($i in $exclude_files){
                Write-Host "Moving file $i..." 
                $i.InputObject | Move-Item -Destination $archiv_dir
            }

        } else{
            Write-Host "OK! Existing files are equal/lesser to the number required latest files!"

        }
    }

#Calling function
Keep_Logs_And_Move_Files 3

預期的:

只有被過濾(舊)的文件應該移動到檔案目錄。 這里我保留 3 個最新文件,子目錄應保留在同一源目錄中

實際的:

Child-dir(Log_Sub1) 也與舊文件一起移動到 arch 目錄。

有人可以幫忙嗎?

盡管我不太確定您想對子目錄中的文件做什么,但我認為您只想在源目錄中保持該目錄不變。

在這種情況下,function 應該適合你。 我已更改它的名稱以符合 PowerShell 中的動詞-名詞命名約定。

function Move-LogFiles {
    [CmdletBinding()]
    Param(
        [ValidateScript({Test-Path -Path $_ -PathType Container})]
        [string]$Source        = "D:\Log_Test_Directories\Log2", 
        [string]$Destination   = "D:\Log_Test_Directories\Log2_archiv",
        [int]$FilesToKeep      = 3
    )
    Write-Verbose "Count of keep files: $FilesToKeep"

    # Check and create Archive dir if not exist
    if (!(Test-Path -Path $Destination -PathType Container)) {
        New-Item -Path $Destination -ItemType Directory | Out-Null
    }

    $files= Get-ChildItem -Path $Source -Filter '*.log' -File | Sort-Object LastWriteTime -Descending

    if ($files.Count -gt $FilesToKeep) {
        for ($i = $FilesToKeep; $i -lt $files.Count; $i++) {
            Write-Verbose "Moving file $($files[$i].Name) to '$Destination'"
            $files[$i] | Move-Item -Destination $Destination -Force
        }
    } 
    else {
        Write-Verbose "OK! Existing files are equal/lesser to the number required latest files!"

    }
}

#Calling function
Move-LogFiles -FilesToKeep 3 -Verbose

希望有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM