2016-07-28 4 views
1

코드에 표시된대로 LastWriteTime이 $ lwt 인 특정 원본 폴더의 모든 파일을 반환하려면 ". *"확장명과 일치해야합니다. 사용자는 ".csv"와 같은 특정 확장자를 제공 할 수 있지만 스크립트가 제공되지 않으면 모든 파일을 검색합니다. 하지만 확장명을 ". *"과 일치시켜 모든 파일을 반환 할 수는 없습니다.Powershell 파일 확장자 일치 *

[CmdletBinding()] 
param (
[Parameter(Mandatory=$true)][string]$source, 
[Parameter(Mandatory=$true)][string]$destination, 
[string]$exten=".*", 
[int]$olderthandays = 30 
) 
$lwt = (get-date).AddDays(-$olderthandays) 

if(!(Test-Path $source)){ 
    Write-Output "Source directory does not exist." 
    exit 
} 

if(!(Test-Path $destination)){ 
    Write-Output "Source directory does not exist." 
    exit 
} 

if($olderthandays -lt 0){ 
    Write-Output "You can't provide a negative number of days. If you want everything deleted simply provide 0 for number of days." 
    exit 
} 

if(!$exten.StartsWith(".")){ 
    $exten = "."+$exten 
    $exten 
} 


try{ 
    Get-ChildItem $source -ErrorAction Stop | ?{$_.LastWriteTime -lt $lwt -AND $_.Extension -eq $exten} | foreach { 
     Write-Output $_.FullName 
     # Move-item $_.FullName $destination -ErrorAction Stop 
    } 
} 
catch{ 
    Write-Output "Something went wrong while moving items. Aborted operation." 
} 

어떻게 달성 할 수 있습니까?

+0

"확장명을 '. *'과 일치시키는 것은 무엇을 의미합니까? 그래서, "/Users/foo/Documents/bar.*"의 파일 이름이 일치합니까? 또는. *은 정규식에서 매우 의미가 있기 때문에 다른 것입니다. – RamenChef

+0

확장자가 .csv, .pdf 또는 그와 비슷한 내용을 말합니다. – Junaid

답변

1

파일의 Extension은 절대로 .*이 아닙니다.

당신은 시도 할 수 :

$exten = "*.$exten" 
Get-ChildItem $source -ErrorAction Stop -Filter $exten | ?{$_.LastWriteTime -lt $lwt} | foreach { ... } 
+0

감사합니다. 그거야. – Junaid

0
하위 항목으로 다시 확장 필터를 이동하고 를 사용

. 또는 *.

[CmdletBinding()] 
param 
(
    [Parameter(Mandatory=$true)][string]$source, 
    [Parameter(Mandatory=$true)][string]$destination, 
    [string]$exten="*.*", 
    [int]$olderthandays = 30 
) 
$lwt = (get-date).AddDays(-$olderthandays) 

if(!(Test-Path $source)){ 
    Write-Output "Source directory does not exist." 
    exit 
} 

if(!(Test-Path $destination)){ 
    Write-Output "Source directory does not exist." 
    exit 
} 

if($olderthandays -lt 0){ 
    Write-Output "You can't provide a negative number of days. If you want everything deleted simply provide 0 for number of days." 
    exit 
} 

if(!$exten.StartsWith('*.')){ 
    $exten = "*."+$exten 
    $exten 
} 


try{ 
    Get-ChildItem $source -Filter $exten -ErrorAction Stop | ?{$_.LastWriteTime -lt $lwt} | foreach { 
     Write-Output $_.FullName 
     # Move-item $_.FullName $destination -ErrorAction Stop 
    } 
} 
catch{ 
    Write-Output "Something went wrong while moving items. Aborted operation." 
}