2

I am working on a script that monitors the downloads folder for completed items and when found it begins processing for that item.

My current detection method tries to rename the subdirectory and if successful it assumes complete. For single files it attempts to move the file into a subdirectory named \filename\filename.ext and if successful assumes complete. This together with a list of known temp files.

While this method may be accurate on when the file/dir is indeed in use or not, it may not work well with some browsers or downloaders that do not lock files while waiting to resume. A version that also checks the date and processes only if xxdays old would not be ideal since all downloads would get the delay.

Anybody have a better idea?

Bricktop
  • 533
  • 3
  • 22
  • 1
    As for this --- "I am working on a script that monitors the downloads folder for completed items and when found it begins processing for that item." --- you are not saying, showing how you are doing this, and it may be the root cause for all this extra effort. Are you using your own custom code, or using the Bits Transfer module / cmdlets? Bit has params to indicate completion. - Get-Command -Name '*bits*' – postanote May 06 '19 at 23:43
  • Browsers do not lock files on download. The file is not valid / serialized enough to be opened by anything, and of course the browser does not test the download on complete.. Most downloads, create a temporary file during the download, then it gets it's name by virtue of how the download was asked for. What happened when the download fails or is corrupted for whatever reason? – postanote May 06 '19 at 23:49

1 Answers1

1

This post suggests that you can open a file for writing to see if it's locked:

$file = "\\xxxx\xxxxx\xxxxxxxx\test.log"
try { [IO.File]::OpenWrite($file).close();exit 0 } catch { exit 999}

I haven't noticed that browsers leave a file unlocked, but I have seen them choose an alternate name (*.part or *.partial) until download is complete. It may be worth using logic that checks the file extension until is something other than .part:

Push-Location 
do{
  Sleep 1
}while((Get-ChildItem (join-path $env:USERPROFILE 'Downloads\*.part*')).Length -gt 0) 
Rich Moss
  • 2,195
  • 1
  • 13
  • 18