Ben*_*ill 4 powershell get-childitem
我编写了一个脚本,用于从服务器归档日志文件.除了Get-ChildItem的递归与否之外,我的状态还不错......
我似乎遇到的问题是,当Get-ChildItem不是递归的并且-Include只有一个过滤器时,它会被忽略!或者,我做错了(可能).
我把输出清理了一下......
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
Run Code Online (Sandbox Code Playgroud)
SOOO ??? 我有一个幻想,我所要做的就是分支到没有递归开关的脚本路径.(顺便说一句,是否可以可变地应用参数,以避免重复的代码路径,其中唯一的可变性是cmdlet的参数?)
无论如何,除了我的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;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
hyt*_*ayr 10
答案在命令的完整描述中(get-help get-childitem -full):
仅当命令包含Recurse参数或路径指向目录内容(例如C:\ Windows\*)时,Include参数才有效,其中通配符指定C:\ Windows目录的内容.
所以下面的工作没有递归.
PS C:\foo> Get-childitem -path "c:\foo\*" -Include *.txt
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11319 次 |
| 最近记录: |