0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

downloaded_sleep.ps1

Last updated at Posted at 2025-07-07

1. スクリプト概要

スクリプト名: downloaded_sleep.ps1
指定フォルダを監視してダウンロードが完了したらPCを休止状態にする

2. 処理と目的

処理の流れ
 1. 対象フォルダとファイルを指定する(現在はハードコーティングされている)
 2. 一定時間ごとに処理1のファイルの存在を確認する
 3. 処理2で存在を指定回数確認できなかったらパソコンを休止状態にする

目的
 寝る直前にファイルをダウンロード開始した際にダウンロードを完了するまで待つ手間をなくすため

3. 動作環境と要件

PowerShellのバージョン
7.0以上

OS
Windows10

必要なモジュール
特になし

必要な権限
特になし

その他の設定
ブラウザはFireFoxを想定しています。

4. 使用方法

基本的な実行方法
スクリプトコードを拡張子ps1で保存してPowershellで実行してください。
ファイルを保存する際は、文字コードをUTF8 BOM付にしてください。

パラメータ
なし

使用例

  1. コマンドラインでpwsh downloaded_sleep.ps1を実行

5. スクリプトコード

$host.UI.RawUI.WindowTitle = 'DLSleep'

#カーソルを表示しない
$host.UI.RawUI.CursorSize = 0

$searchDir = 'D:\Me Document\Downloaded Files'
$fileFilter = '*.part'
$startTime = Get-Date
$noPartCount = 0
$waitCount = 600
$waitViewCount = $host.UI.RawUI.WindowSize.Height - 10
$beepStartCount = 20

$beforeFileListText = ""
while($true){
  
  $fileList = ls -literal $searchDir -Filter $fileFilter -File | 
                  %{ gi -literal $_.FullName} |
                  Format-Table -auto -wrap -property Name,@{Name='length';expression={("{0:###,###,###,##0}" -f $_.Length)};Alignment='Right'},LastWriteTime
  $fileListText = $fileList | Out-String
  #echo "「${fileListText}」"
  
  if( ($fileList|Measure).count -eq 0 ){
    $waitPlusCounter = 10
  } elseif ($fileListText -eq $beforeFileListText){
    $waitPlusCounter = 1
  } else {
    $waitPlusCounter = 0
    
    #画面表示をクリア
    $basePos = $host.UI.RawUI.CursorPosition;$host.UI.RawUI.CursorPosition = New-Object System.Management.Automation.Host.Coordinates(0,($host.UI.RawUI.BufferSize.Height-1));$host.UI.RawUI.CursorPosition = $basePos;
    
    #いろいろな情報を表示する
    echo "検索日時:$(Get-Date)"
    echo "検索ファイル:${searchDir}\${fileFilter}"
    echo "待機回数:${noPartCount}/${waitCount}          "
    echo $fileList

  }
  #echo "existPart=${existPart}"
  
  if ( $waitPlusCounter -eq 0 ){
    $noPartCount = 0
  } else {
    $noPartCount += $waitPlusCounter
    if ( ( $noPartCount % [Math]::Truncate($waitCount/$waitViewCount) ) -eq 0 ){
      echo "noPartCount=${noPartCount}/${waitCount} $(Get-Date)"
    }
    if ( ($noPartCount -gt $beepStartCount) -and ((Get-Date).hour -gt 6) ){
      #音を鳴らす(6時以降のみ)
      [Console]::Beep(500+$noPartCount,500+$noPartCount)
    }
  }
  
  #カウンターがしきい値を超えたら待機を終了する
  if ( $noPartCount -ge $waitCount ){
    break
  }
  
  $beforeFileListText = $fileListText
  sleep 1
}


#待機が完了したのでPCを休止状態にする
$suspendtype = $true   #休止
# $suspendtype = $false  #Sleep

$signature = @"
[DllImport("powrprof.dll")]
public static extern bool SetSuspendState(bool Hibernate,bool ForceCritical,bool DisableWakeEvent);
"@
$func = Add-Type -memberDefinition $signature -namespace "Win32Functions" -name "SetSuspendStateFunction" -passThru
$func::SetSuspendState($suspendtype,$false,$false)

6. 注意事項と既知の問題

制約事項
ダウンロード中のフォルダやファイル拡張子はブラウザの設定に依存するため、環境によっては設定変更や動作不良の可能性があります

既知のバグ
もしバグを発見された場合は、コメントでご報告ください。

トラブルシューティング
・ps1ファイルのエンコーディングには注意してください。

7. 免責事項

保証はありません。使用は自己責任で行ってください。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?