2010-12-09 4 views
2

J'ai un script PowerShell (ci-dessous) qui semble fonctionner, mais imprime une erreur chaque fois qu'il s'exécute, ce qui peut avoir un impact sur ses performances. Pourquoi ai-je cette erreur?Erreur avec le script PowerShell

L'erreur:

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 

Le script 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 } 

Répondre

2

Select-String peut trouver plusieurs correspondances dans un fichier. Je suspecte qu'il trouve plus de correspondances dans le même dossier mais vous avez déjà déplacé le dossier ainsi la source n'existe plus. Utilisez le paramètre -List sur Select-String pour obtenir une seule correspondance par fichier.

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

C'était précisément le problème! Cela a un sens total - je ne sais pas pourquoi je n'y ai pas pensé. Merci beaucoup!! – Brad

0

Essayez de changer la ligne qui génère la liste de $ à quelque chose comme ceci:

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

Vous pouvez également change la dernière ligne à:

$list | mv $newdir 
Questions connexes