Get-Item无法找到Path

时间:2018-04-21 01:01:46

标签: powershell

我正在尝试通过PowerShell清理我的下载文件夹,所以我让它循环遍历文件夹,并将某些文件类型放在某些文件夹中。

我正在使用以下代码

#Create a directory for the PNG Pictures
$newPath = "C:\Users\mcmon\OneDrive\Pictures\PNG Pictures"
New-Item -ItemType Directory -Force -Path $newPath
Write-Host $newPath + " Created"

#Create a directory for Zip Files
$zipPath = "C:\Users\mcmon\downloads\Zip Files"
New-Item -ItemType Directory -Force -Path $zipPath
Write-Host $zipPath + " Created"

#.exe Files
$exePath = "C:\Users\mcmon\downloads\Exe"
New-Item -ItemType Directory -Force -Path $exePath
Write-Host $exePath + " Created"

#JPG Files
$jpgPath = "C:\Users\mcmon\OneDrive\Pictures\JPG"
New-Item -ItemType Directory -Force -Path $jpgPath
Write-Host $jpgPath + " Created"

#PDF Files
$pdfPath = "C:\Users\mcmon\downloads\PDF"
New-Item -ItemType Directory -Force -Path $pdfPath
Write-Host $pdfPath + " Created"

#Spreadsheets and Stuff
$xlsPath = "C:\Users\mcmon\downloads\Spreadsheets"
New-Item -ItemType Directory -Force -Path $xlsPath
Write-Host $xlsPath + " Created"

#Other
$miscPath = "C:\Users\mcmon\downloads\Misc"
New-Item -ItemType Directory -Force -Path $miscPath
Write-Host $miscPath + " Created"


foreach($file in Get-ChildItem "c:\users\mcmon\downloads")
{
    if($file -Like "*.png")
    {
        robocopy $file.DirectoryName $newPath $file.Name
        Remove-Item $file.FullName
        Write-Host $file.FullName + " Moved to $newPath"
    }

    if($file -Like "*.zip")
    {
        robocopy $file.DirectoryName $zipPath $file.Name
        Remove-Item $file.FullName
        Write-Host $file.FullName + " Moved to $zipPath"
    }

    if($file -Like "*.exe")
    {
        robocopy $file.DirectoryName $exePath $file.Name
        Remove-Item $file.FullName
        Write-Host $file.FullName + " Moved to $exePath"
    }

    if($file -Like "*.jpg" -Or $file -Like "*.jpeg")
    {
        robocopy $file.DirectoryName $jpgPath $file.Name
        Remove-Item $file.FullName
        Write-Host $file.FullName + " Moved to $jpgPath"
    }

    if($file -Like "*.pdf")
    {
        robocopy $file.DirectoryName $pdfPath $file.Name
        Remove-Item $file.FullName
        Write-Host $file.FullName + " Moved to $pdfPath"
    }

    if($file -Like "*.xls" -Or $file -Like "*.xlsx" -Or $file -Like "*.xlsm" -Or $file -Like "*.csv")
    {
        robocopy $file.DirectoryName $xlsPath $file.Name
        Remove-Item $file.FullName
        Write-Host $file.FullName + " Moved to $xlsPath"
    }

    if( -Not (Get-Item $file) -Is [System.IO.DirectoryInfo])
    {
        robocopy $file.DirectoryName $miscPath $file.Name
        Remove-Item $file.FullName
        Write-Host $file.FullName + " did not meet any criteria - Moved to Misc"
    }

    Write-Host "---File Reviewed ---"

}

Write-Host "--------------------------"
Write-Host "--------------------------"
Write-Host "Download Cleanup Complete!"
Write-Host "--------------------------"
Write-Host "--------------------------"

我遇到的问题是在查看$file是否为文件夹时。我在PowerShell中不断收到以下错误

Get-Item : Cannot find path 'C:\WINDOWS\system32\OBL.lg' because it does not exist. At C:\Users\mcmon\Desktop\Learning Shit\HelloWorld.ps1:81 char:12 + if( -Not (Get-Item $file) -Is [System.IO.DirectoryInfo]) + ~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (C:\WINDOWS\system32\OBL.lg:String) [Get-Item], ItemNotFoundException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand

0 个答案:

没有答案