#author("2025-11-14T13:55:12+09:00","","")
[[PowerShell]]
#author("2025-11-14T13:56:30+09:00","","")
[[ソフトウェア開発>SoftwareEngineering]] / [[PowerShell>PowerShell]] / [[ExtractModifiedFiles>./]]

*ExtractModifiedFiles [#rb211f73]
#highlightjs([powershell])
 # ----------------------------------------------------------
 # インポート
 # ----------------------------------------------------------
 Add-Type -AssemblyName System.Drawing
 Add-Type -AssemblyName System.Windows.Forms
 
 
 # ----------------------------------------------------------
 # ユーザー設定
 # ----------------------------------------------------------
 $userId                    = 'UserName'
 $sourceFullFolderName      = ''
 $destinationFullFolderName = ''
 
 
 
 # ----------------------------------------------------------
 # 関数定義
 # ----------------------------------------------------------
 function New-Button {
     param (
         $Caption
     )
 
     $button = New-Object System.Windows.Forms.Button
     $button.Width  = 80
     $button.Height = 30
     $button.Text = $Caption
 
     return $button
 }
 
 
 
 function New-Label {
     param (
         $Caption
     )
 
     $label = New-Object System.Windows.Forms.Label
     $label.AutoSize = $true
     $label.Text = $Caption
     
     return $label
 }
 
 
 
 function New-TextBox {
     $textbox = New-Object System.Windows.Forms.TextBox
     return $textbox
 }
 
 
 
 function New-DateTimePicker {
     $picker = New-Object System.Windows.Forms.DateTimePicker
     return $picker
 }
 
 
 
 function Extract-ModifiedFiles {
     param (
          $Source
         ,$Destination
         ,$FolderName
         ,$MaxAge
     )
 
     $Destination = Join-Path $Destination $FolderName
     Write-Host "コピー先:${Destination}"
     if (!(Test-Path $Destination)) {
         New-Item -ItemType Directory -Path $Destination
     }
 
     Get-ChildItem -Path $Source -Recurse -File | Where-Object {
         $MaxAge -le $_.LastWriteTime
     } | ForEach-Object {
         $relativePath = $_.FullName.Substring($source.Length)
         $targetPath   = Join-Path $Destination $relativePath
 
         $targetDirectory = Split-Path $targetPath
         if (!(Test-Path $targetDirectory)) {
             New-Item -ItemType Directory -Path $targetDirectory -Force
         }
 
         Copy-Item -Path $_.FullName -Destination $targetPath -Force
     }
 
     Write-FileList "${Destination}"
 }
 
 
 
 function Write-FileList {
     param (
         $Path
     )
     $writingFullFileName = "${Path}\filelist.txt"
 
     Get-ChildItem -Path $Path -Recurse -File | ForEach-Object {
         $lastWriteTime = (Get-Date $_.LastWriteTime -Format "yyyy-MM-dd HH:mm:ss")
         $relativePath  = ($_.FullName).Replace($Path, ".")
 
         "${lastWriteTime} ${relativePath}" | Out-File $writingFullFileName -Append -Encoding utf8
     }
 }
 
 
 
 # ----------------------------------------------------------
 # メイン画面
 # ----------------------------------------------------------
 $form = New-Object System.Windows.Forms.Form
 $form.Text = "更新ファイル抽出"
 $form.Width  = 640
 $form.Height = 270
 $form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedDialog
 $form.MaximizeBox = $false
 
 
 
 # ----------------------------------------------------------
 # ユーザー
 # ----------------------------------------------------------
 $top = 20
 $userLabel = New-Label('ユーザー')
 $form.Controls.Add($userLabel)
 
 $userLabel.Location = New-Object System.Drawing.Point(20, $top)
 
 
 $userTextBox = New-TextBox
 $form.Controls.Add($userTextBox)
 
 $userTextBox.Location = New-Object System.Drawing.Point(100, $top)
 $userTextBox.Width  = 100
 $userTextBox.Text   = $userId
 
 
 # ----------------------------------------------------------
 # コピー元
 # ----------------------------------------------------------
 $top = 60
 $sourceFullFolderNameLabel = New-Label('コピー元')
 $form.Controls.Add($sourceFullFolderNameLabel)
 
 $sourceFullFolderNameLabel.Location = New-Object System.Drawing.Point(20, $top)
 
 $sourceFullFolderNameTextBox = New-TextBox
 $form.Controls.Add($sourceFullFolderNameTextBox)
 
 $sourceFullFolderNameTextBox.Location = New-Object System.Drawing.Point(100, $top)
 $sourceFullFolderNameTextBox.Width  = 500
 $sourceFullFolderNameTextBox.Height = 80
 $sourceFullFolderNameTextBox.Text = ''
 $sourceFullFolderNameTextBox.AllowDrop = $true
 
 $sourceFullFolderNameTextBox.Add_DragDrop({
     foreach ($filename in $_.Data.GetData([System.Windows.Forms.DataFormats]::FileDrop)) {
         if ([System.IO.Directory]::Exists($filename)) {
             $sourceFullFolderNameTextBox.Text = $filename
         }
     }
 })
 $sourceFullFolderNameTextBox.Add_DragOver({
     foreach ($filename in $_.Data.GetData([System.Windows.Forms.DataFormats]::FileDrop)) {
         if ([System.IO.Directory]::Exists($filename)) {
             $_.Effect = [System.Windows.Forms.DragDropEffects]::All
         }
     }
 })
 
 
 
 # ----------------------------------------------------------
 # コピー先
 # ----------------------------------------------------------
 $top = 100
 $destinationFullFolderNameLabel = New-Label('コピー元')
 $form.Controls.Add($destinationFullFolderNameLabel)
 
 $destinationFullFolderNameLabel.Location = New-Object System.Drawing.Point(20, $top)
 
 $destinationFullFolderNameTextBox = New-TextBox
 $form.Controls.Add($destinationFullFolderNameTextBox)
 
 $destinationFullFolderNameTextBox.Location = New-Object System.Drawing.Point(100, $top)
 $destinationFullFolderNameTextBox.Width  = 500
 $destinationFullFolderNameTextBox.Height = 80
 $destinationFullFolderNameTextBox.Text = ''
 $destinationFullFolderNameTextBox.AllowDrop = $true
 
 $destinationFullFolderNameTextBox.Add_DragDrop({
     foreach ($filename in $_.Data.GetData([System.Windows.Forms.DataFormats]::FileDrop)) {
         if ([System.IO.Directory]::Exists($filename)) {
             $destinationFullFolderNameTextBox.Text = $filename
         }
     }
 })
 $destinationFullFolderNameTextBox.Add_DragOver({
     foreach ($filename in $_.Data.GetData([System.Windows.Forms.DataFormats]::FileDrop)) {
         if ([System.IO.Directory]::Exists($filename)) {
             $_.Effect = [System.Windows.Forms.DragDropEffects]::All
         }
     }
 })
 
 
 
 # ----------------------------------------------------------
 # 抽出日付
 # ----------------------------------------------------------
 $top = 140
 $maxAgeLabel = New-Label('抽出日')
 $form.Controls.Add($maxAgeLabel)
 
 $maxAgeLabel.Location = New-Object System.Drawing.Point(20, $top)
 
 $fromMaxAgePicker = New-DateTimePicker
 $form.Controls.Add($fromMaxAgePicker)
 
 $fromMaxAgePicker.Location = New-Object System.Drawing.Point(100, $top)
 $fromMaxAgePicker.Value = Get-Date
 $fromMaxAgePicker.Format = 8
 $fromMaxAgePicker.CustomFormat = 'yyyy-MM-dd HH:mm:ss'
 
 
 $maxAge2Label = New-Label('~')
 $form.Controls.Add($maxAge2Label)
 
 $maxAge2Label.Location = New-Object System.Drawing.Point(305, $top)
 $maxAge2Label.AutoSize = $true
 
 
 $toMaxAgePicker = New-DateTimePicker
 $form.Controls.Add($toMaxAgePicker)
 
 $toMaxAgePicker.Location = New-Object System.Drawing.Point(340, $top)
 $toMaxAgePicker.Enabled = $false
 $toMaxAgePicker.Value = (Get-Date -Hour 23 -Minute 59 -Second 59 -Millisecond 999)
 $toMaxAgePicker.Format = 8
 $toMaxAgePicker.CustomFormat = 'yyyy-MM-dd HH:mm:ss'
 
 
 
 # ----------------------------------------------------------
 # 実行ボタン
 # ----------------------------------------------------------
 $top = 180
 $executeButton = New-Button('実行')
 $form.Controls.Add($executeButton)
 
 $executeButton.Location = New-Object System.Drawing.Point(520, $top)
 
 $executeButton.Add_Click({
     $source = $sourceFullFolderNameTextBox.Text
     $destination = $destinationFullFolderNameTextBox.Text
     $user = $userTextBox.Text
 
     $fromday = $fromMaxAgePicker.Value.ToString('yyyyMMdd')
     $today   = $toMaxAgePicker.Value.ToString('yyyyMMdd')
     $currentDateTime = Get-Date -Format 'yyyyMMddTHHmmss'
 
     $maxAge = $fromMaxAgePicker.Value
     $fromDateTime = $fromMaxAgePicker.Value.ToString('yyyyMMddTHHmmss')
     $toDateTime   = $toMaxAgePicker.Value.ToString('yyyyMMddTHHmmss')
 
     $folderName = "${currentDateTime}_${user}_(${fromDateTime}-${toDateTime})"
 
     Extract-ModifiedFiles $source $destination $folderName $maxAge
     explorer "${destination}\${folderName}"
 })
 
 
 
 # ----------------------------------------------------------
 # 画面表示
 # ----------------------------------------------------------
 $form.ShowDialog()

トップ   差分 履歴 リロード   一覧 検索 最終更新   ヘルプ   最終更新のRSS