2010-12-09 15 views
2

PowerShellスクリプト(下記)が動作しているように見えますが、実行するたびにエラーが表示され、パフォーマンスに影響を与える可能性があります。なぜこのエラーが発生するのですか?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 

PowerShellスクリプト:

# 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パラメータを使用すると、ファイルごとに1つの一致が得られます。

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

これはまさに問題でした。それは完全に意味がある - 私はなぜ私がそれを考えなかったのか分からない。どうもありがとうございます!! – Brad

0

はこのような何かに$リストを作成する行を変更してみてください:

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

あなたがまたしたい場合があります最後の行を次のように変更します。

$list | mv $newdir 
関連する問題