Powershell抓住额外的文件

时间:2015-04-17 16:30:42

标签: powershell robocopy

我有这个Powershell代码:

Function CheckFileList()
{
    $limit = (Get-Date).AddDays(-270)
    $input_path = gci '//blah/folder/' | sort -property LastWriteTime
    $output_file = 'c:\PowershellScripts\prune_results.txt'
    #Clear-Content $output_file
    $countf = 0
    $outputstr = ""

    $outputstr = $(Get-Date -format 'F') + " - Folders to be purged:`r`n"

    $input_path | Foreach-Object{
        if ( (Get-Item $_.FullName) -is [System.IO.DirectoryInfo] ) {
            if ( $_.LastWriteTime -le $limit ) {
                $source=$input_path + $_.Name
                $dest="\\server\otherfolder" + $_.Name
                $what=@("/MOVE")
                $options=@("/COPY:DAT /DCOPY:T")
                $cmdArgs = @("$source","$dest",$what,$options)
                "robocopy " + $cmdArgs >> $output_file
                #robocopy @cmdArgs
                #Move-Item $_.FullName \\server\otherfolder
                $outputstr = $outputstr + " (" + $_.LastWriteTime + ") `t" + $_.Name + "`r`n"
                $countf++
                $outputstr = $outputstr + "Folders [to be] purged: " + $countf + "`r`n`r`n"
                $outputstr >> $output_file
                Exit
            }
        }
    }

    #$outputstr = $outputstr + "Folders [to be] purged: " + $countf + "`r`n`r`n"
    #$outputstr >> $output_file

}

CheckFilelist

此代码仅用于显示命令的运行方式。它只有1个循环(在第一个循环后退出),所以它应该抓住1个文件夹。

但输出很大,似乎包括所有文件夹(1000+)而不是一个。它类似于:

robocopy file1.txt FOLDER1 FOLDER2 FOLDER3 FOLDER4 ........ \\server\otherfolder\FOLDER5

我在这里遗漏了什么吗?应将//blah/folder/上的文件夹移动到其他网络文件夹(\\server\otherfolder

1 个答案:

答案 0 :(得分:1)

您的问题的核心来自您填充$source

的方式
$source=$input_path + $_.Name

这是因为您定义$input_path

的方式
$input_path = gci '//blah/folder/' | sort -property LastWriteTime

你继续循环浏览所有项目,而只是真正寻找一个。还有其他方法可以得出相同的结论。 $input_path不是路径,而是" // blah / folder"

中的文件夹和文件的集合
$input_path = gci 'c:\temp' | Where-Object{($_.LastWriteTime -le $limit) -and ($_.PSIsContainer)}
$input_path | ForEach-Object{
    #... do things
    $_.FullName
    # Fullname is the complete path. 
}

关于Robocopy

如果你在robocopy文档中查看/MINAGE,我认为很多这种逻辑可能会变得多余

  

/ MINAGE:n 排除上次修改日期比n天或指定日期更新的文件。如果n小于1900,则n以天表示。否则,n是表示为YYYYMMDD的日期。

虽然在阅读了你的评论和问题之后,这很可能不是你想要的。

关于Move-Item

我看到你也在尝试。使用我们新的$input_path管道导入Move-Item时,这应该可以正常工作。如果您需要记录,可以使用ForEach-Object以允许在其他地方记录额外信息。

$input_path | Move-Item -Destination "\\server\otherfolder"
相关问题