简体   繁体   中英

powershell, how to mointor a directory and move files over to another folder

I am using powershell 3. need to monitor a folder, if there are any image files, move them over to antoher folder.

here's my code, i test it, its not working, couldn't figure out what need to be fixed.

#<BEGIN_SCRIPT>#

#<Set Path to be monitored>#
$searchPath = "F:\download\temp"
$torrentFolderPath = "Z:\"

#<Set Watch routine>#
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = $searchPath
$watcher.IncludeSubdirectories = $false
$watcher.EnableRaisingEvents = $true


$created = Register-ObjectEvent $watcher "Created" -Action {
   Copy-Item -Path $searchPath -Filter *.jpg -Destination $torrentFolderPath –Recurse
}


#<END_SCRIPT>#

UPDATE:

I got it partially working. still have one issue left. lets start with an empty folder. I download an image (1.jpg) to the folder, nothing moved to Z: drive. then I download another image (2.jpg) to the folder. 1.jpg will be moved to Z: drive. seems like the newly created one never get moved over.

$folder = "F:\\download\\temp"
$dest = "Z:\\"
$filter = "*.jpg"

$fsw = new-object System.IO.FileSystemWatcher $folder, $filter -Property @{
    IncludeSubDirectories=$false
    NotifyFilter = [System.IO.NotifyFilters]'FileName, LastWrite'
}

$onCreated = Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {

    Move-Item -Path F:\download\temp\*.jpg Z:\
}

You have not registered the NotifyFilter. That is why your code is not working.

here is a sample which registers the NotifyFilter and prints the file details which was created

$folder = "c:\\temp"
$filter = "*.txt"

$fsw = new-object System.IO.FileSystemWatcher $folder, $filter -Property @{
    IncludeSubDirectories=$false
    NotifyFilter = [System.IO.NotifyFilters]'FileName, LastWrite'
}

$onCreated = Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {
    $path = $Event.SourceEventArgs.FullPath
    $name = $Event.SourceVentArgs.Name
    $changeType = $Event.SourceEventArgs.ChangeType
    $timeStamp = $Event.TimeGenerated

    Write-Host $path
    Write-Host $name
    Write-Host $changeType
    Write-Host $timeStamp
}

Event action scripts run in a separate scope that only has access to global variables, so depending on your implementation you have have issues trying to use those variables in the action script. One way around without resorting to declaring global variables (bad mojo!) is to use an expandable string to create a script block with the variables expanded before you register the event:

$ActionScript = 
 [Scriptblock]::Create("Copy-Item -Path $searchPath -Filter *.jpg -Destination $torrentFolderPath –Recurse")

$created = Register-ObjectEvent $watcher "Created" -Action $ActionScript

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM