PowerShell 脚本 - Get-ChildItem [英] PowerShell Scripting - Get-ChildItem

查看:49
本文介绍了PowerShell 脚本 - Get-ChildItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个脚本,用于从服务器归档日志文件.除了 Get-ChildItem 的递归性与否,我的一切都很好...

I've written a script that will be used for archiving log files from a server. I'm in pretty good shape with everything but the recursiveness or not of Get-ChildItem...

我似乎遇到的问题是,当 Get-ChildItem 不是递归的并且 -Include 仅存在一个过滤器时,它会被忽略!或者,我做错了什么(可能).

The issue I seem to be having is that when Get-ChildItem is not recursive and -Include is present with only one filter, it is ignored! Or, I'm doing something wrong (likely).

我已经清理了一点输出...

I've cleaned up the output a little...

PS C:\foo> Get-childitem -path "c:\foo"

Name
----
bar1.doc
bar2.doc
bar3.doc
foo1.txt
foo2.txt
foo3.txt

PS C:\foo> Get-childitem -path "c:\foo" -Include *.txt
PS C:\foo> Get-childitem -path "c:\foo" -Include *.txt -recurse

Name
----
foo1.txt
foo2.txt
foo3.txt

呸???我有一个幻想,我所要做的就是分支到没有递归开关的脚本路径.(顺便说一句,是否可以可变地应用参数以避免重复的代码路径,其中唯一可变性是 cmdlet 的参数?)

Sooo??? I had a fantasy where all I had to do was branch to a path of the script that did not have the recurse switch. (By the way, is it possible to variably apply parameters so as to avoid duplicated code paths where the only variability is the parameters to a cmdlet?)

无论如何,除了我的 Get-ChildItem 问题之外,这是我的完整性脚本.

Anyway, here is my script for completeness, in addition to my issue with Get-ChildItem.

function MoveFiles()
{
    Get-ChildItem -Path $source -Recurse -Include $ext | where { $_.LastWriteTime -lt (Get-Date).AddDays(-$days) } | foreach {
        $SourceDirectory = $_.DirectoryName;
        $SourceFile = $_.FullName;
        $DestinationDirectory = $SourceDirectory -replace [regex]::Escape($source), $dest;
        $DestionationFile = $SourceFile -replace [regex]::Escape($source), $dest;

        if ($WhatIf){
            #Write-Host $SourceDirectory;
            #Write-Host $DestinationDirectory;
            Write-Host $SourceFile -NoNewline
            Write-Host " moved to " -NoNewline
            Write-Host $DestionationFile;
        }
        else{
            if ($DestinationDirectory)
            {
                if ( -not [System.IO.Directory]::Exists($DestinationDirectory)) {
                    [void](New-Item $DestinationDirectory -ItemType directory -Force);
                }
                Move-Item -Path $SourceFile -Destination $DestionationFile -Force;
            }
        }
    }
}

推荐答案

答案在命令的完整描述中 (get-help get-childitem -full):

The answer is in the full description of the command (get-help get-childitem -full):

Include 参数有效仅当命令包含递归参数或路径导致目录的内容,例如C:\Windows\*,其中通配符字符指定内容C:\Windows 目录.

The Include parameter is effective only when the command includes the Recurse parameter or the path leads to the contents of a directory, such as C:\Windows\*, where the wildcard character specifies the contents of the C:\Windows directory.

因此,以下内容无需递归即可工作.

So the following would work without recurse.

PS C:\foo> Get-childitem -path "c:\foo\*" -Include *.txt

这篇关于PowerShell 脚本 - Get-ChildItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆