0

Im trying to monitor a directory and its subdirectory for new files. I want to look at the file extension and move it to its appropriate folder based on the file extension. For example, If i have a folder "C:\users\dave\desktop\test" and i download an avi file to that folder, id like the script to see it and move the file to a C:\movies automatically. Or if i download an entire folder of mp3 files, i'd like it to move that entire folder to C:\music automatically. Right now it moves just the mp3 files into C:\music, but I cant figure out how to move the parent folder too.

Here is what i have written so far: Keep in mind, I'm new to powershell!

if (!(Test-Path -path C:\music))
{
   New-Item C:\music\ -type directory
}
if (!(Test-Path -path C:\movies))

{
   New-Item C:\movies\ -type directory
}


$PickupDirectory = Get-ChildItem -recurse -Path  "C:\users\Dave\desktop\test"
$folder = 'C:\users\Dave\desktop\test'
$watcher = New-Object System.IO.FileSystemWatcher $folder
$watcher.IncludeSubdirectories = $true
$watcher.EnableRaisingEvents = $true
$watcher



Register-ObjectEvent $watcher -EventName Changed -SourceIdentifier 'Watcher' -Action { 




foreach ($file in $PickupDirectory)
{
    if ($file.Extension.Equals('.avi'))
    {

        Write-Host $file.FullName #Output file fullname to screen
        Write-Host $Destination   #Output Full Destination path to screen

    move-Item $file.FullName -destination c:\movies
}

 if ($file.Extension.Equals('.mp3'))
   {

    Write-Host $file.FullName #Output file fullname to screen
    Write-Host $Destination   #Output Full Destination path to screen

    move-Item $file.FullName -destination c:\music
}


if ($file.Extension.Equals(".mp4"))
    {

    Write-Host $file.FullName #Output file fullname to screen
    Write-Host $Destination   #Output Full Destination path to screen

    move-Item $file.FullName -destination c:\movies
}
}
}
}

For some reason, it does not move the files. Ive gotten it to see changes, but not move files.

Please Help!

  • Check out this app -- [DropIt](http://dropit.sourceforge.net) that does this. – Andy Arismendi Apr 27 '13 at 11:58
  • Thank you Andy, however, I have to create this for an assignment. My programming knowledge has gotten me this far, but my lack of scripting knowledge has brought me to a halt. – OOoNiMrOdoOO Apr 27 '13 at 13:43

1 Answers1

0

The Action scripblock of events runs in a different runspace, and the variables in your main script aren't visible there.

Try moving this line:

$PickupDirectory = Get-ChildItem -recurse -Path  "C:\users\Dave\desktop\test"

into your action script block and see if that helps.

I'd also trade that stack of If statements for a Switch, but that's just personal preference.

One way to add that parent path

 $parent = ($file.fullname).split('\')[-2]
 [void][IO.Directory]::CreateDirectory("c:\music\$parent")
 move-Item $file.FullName -destination c:\music\$parent
mjolinor
  • 66,130
  • 7
  • 114
  • 135
  • That is amazing! thanks mjolinor! Any idea how I would get the parent directory to move with it? so I dont have a folder with a bunch of mixed mp3 files? – OOoNiMrOdoOO Apr 27 '13 at 16:01
  • hmm, I just ended up with a black file named after the parent folder. it created a file with the folder name, not a folder. – OOoNiMrOdoOO Apr 27 '13 at 16:47
  • That sounds like it might be a consequence of your Get-Childitem not filtering out the directories. – mjolinor Apr 27 '13 at 16:52
  • is there something wrong with the code: $PickupDirectory = Get-ChildItem -recurse -Path "C:\users\Dave\desktop\test" ? – OOoNiMrOdoOO Apr 27 '13 at 16:58
  • Actually, I think it's because move-item won't automatically create the target directories if they don't already exist, so you'll have to do that in advance of the move. Updated the code. – mjolinor Apr 27 '13 at 17:07
  • You are right. You have helped me tremendously! Thank you so much! I should have realized that. But I'm running this with windows task scheduler in the background. There are other features I'd like to include such as deleting the folders that remain. Which i couldnt get to work either. But, you have helped me so much I don't want to keep bugging ya. – OOoNiMrOdoOO Apr 27 '13 at 17:12
  • Keep at it, and you'll get it sorted out :) – mjolinor Apr 27 '13 at 17:15