2010-12-09 5 views
2

나는 아래 보이는 작동하는 것처럼 보이지만 성능에 영향을 줄 수 있다고 생각 될 때마다 오류를 인쇄합니다. 이 오류가 발생하는 이유는 무엇입니까?powershell 스크립트의 오류

오류 :

Move-Item : Cannot find path 'C:\Program Files (x86)\mailserver\mail\domain.com\user\inbox\201012090411577967.imap' because it does not exist. 

At C:\scripts\findfiles.ps1:27 char:21 
+ $list | foreach { mv <<<< $_.Path $newdir } 
    + CategoryInfo   : ObjectNotFound: (C:\Program File...0411577967.imap:String) [Move-Item], ItemNotFoundException 
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.MoveItemCommand 

파워 쉘 스크립트

$list = gci $startdir* -include "*.imap" -recurse | where { select-string -Path $_ -Pattern $pattern -Quiet } 

당신도 할 수 있습니다 :

# Prompt the user for a start directory 
$startdir=Read-Host "Enter a directory to search (without trailing slash)" 

# Define a variable for the new directory 
$newdir="$startdir\temp" 

# Make the temp directory 
if (!(Test-Path -path $newdir)) 
{ 
    New-Item $newdir -type directory 
} 

# Tell them we will write to the start director\temp 
write-host "Files will be moved to $newdir" 

# Prompt to a pattern to search for 
$pattern=Read-Host "Enter a pattern to search for" 

# Tell the user we are doing something 
write-host "Searching $startdir for `"$pattern`" then moving. Please wait...." 

# Generate a list of files containing a pattern 
$list = gci $startdir\* -include "*.imap" -recurse | select-string -pattern $pattern 


# Move files matching the pattern to temp 
$list | foreach { mv $_.Path $newdir } 

답변

2

Select-String은 파일 내에서 둘 이상의 일치 항목을 찾을 수 있습니다. 동일한 파일 내에서 더 많은 일치 항목을 찾는 것으로 의심되지만 소스가 더 이상 존재하지 않도록 이미 파일을 이동했습니다. Select-String에 -List 매개 변수를 사용하면 파일 당 하나의 일치 항목 만 얻을 수 있습니다.

$list = gci $startdir -r *.imap | select-string $pattern -List 
+0

정확하게 문제가있었습니다. 그것은 총체적으로 이해합니다. 나는 왜 내가 그것을 생각하지 않았는지 모르겠습니다. 정말 고맙습니다!! – Brad

0

가 이런 식으로 $ 목록을 생성 라인을 변경해보십시오 마지막 줄을 다음으로 변경하십시오.

$list | mv $newdir 
관련 문제