0

I'm attempting to clean up my Downloads folder via PowerShell, so I have it looping through the folder, and placing certain file types in certain folders.

I'm using the following code

#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 "--------------------------"

The issue I'm having is when checking to see if the $file is a Folder or not. I keep getting the following error in 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

jDave1984
  • 896
  • 4
  • 13
  • 43
  • In short: Use `$file.FullName`, which _shouldn't_ be necessary, but currently is - see the linked question's answers and https://github.com/PowerShell/PowerShell/issues/6057 – mklement0 Apr 21 '18 at 02:27
  • 1
    Aha! Thanks, and sorry for the repost. Side-note: New to Powershell, but loving its capabilities – jDave1984 Apr 21 '18 at 14:37
  • Glad to hear it; warts such as this one notwithstanding, there's a lot to love. – mklement0 Apr 21 '18 at 14:47
  • I'm coming from C# so there's a syntax learning curve, but fun so far – jDave1984 Apr 21 '18 at 15:46

0 Answers0