作業日記@HatenaBlog

各種の作業メモ

Powershell で連番ファイルを DL する方法

単純なスクリプト

PS > for ( $i = 1; $i -lt 100; $i++ )
>>> {
>>> $a = "http://example.com/EXAPLE_DIR/"
>>> $a = $a + $i.tostring("000") + ".jpg"
>>> $b = "D:\hoge_dir\" + $i.tostring("000") + ".jpg"
>>> wget $a -o $b
>>> }

スクリプトファイル

テキストエディタで以下を入力し、hoge.ps1として適当なファイル名で保存する。

# アセンブリの読み込み
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

# フォームの作成
$form = New-Object System.Windows.Forms.Form 
$form.Text = "ダウンロード先のURL"
$form.Size = New-Object System.Drawing.Size(260,180) 

# OKボタンの設定
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(40,100)
$OKButton.Size = New-Object System.Drawing.Size(75,30)
$OKButton.Text = "OK"
$OKButton.DialogResult = "OK"

# キャンセルボタンの設定
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(130,100)
$CancelButton.Size = New-Object System.Drawing.Size(75,30)
$CancelButton.Text = "Cancel"
$CancelButton.DialogResult = "Cancel"

# ラベルの設定
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,30) 
$label.Size = New-Object System.Drawing.Size(250,20) 
$label.Text = "ダウンロード先のURLを入力する"

# 入力ボックスの設定
$textBox = New-Object System.Windows.Forms.TextBox 
$textBox.Location = New-Object System.Drawing.Point(10,70) 
$textBox.Size = New-Object System.Drawing.Size(225,50) 

# キーとボタンの関係
$form.AcceptButton = $OKButton
$form.CancelButton = $CancelButton

# ボタン等をフォームに追加
$form.Controls.Add($OKButton)
$form.Controls.Add($CancelButton)
$form.Controls.Add($label) 
$form.Controls.Add($textBox)

# フォームを表示させ、その結果を受け取る
$result = $form.ShowDialog()

# 結果による処理分岐
if ($result -eq "OK")
{
    $my_url = $textBox.Text
} elseif ($result -eq "Cancel")
{
    exit
}

###
###
###
# アセンブリの読み込み
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

# フォームの作成
$form = New-Object System.Windows.Forms.Form 
$form.Text = "ファイル数"
$form.Size = New-Object System.Drawing.Size(260,180) 

# OKボタンの設定
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(40,100)
$OKButton.Size = New-Object System.Drawing.Size(75,30)
$OKButton.Text = "OK"
$OKButton.DialogResult = "OK"

# キャンセルボタンの設定
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(130,100)
$CancelButton.Size = New-Object System.Drawing.Size(75,30)
$CancelButton.Text = "Cancel"
$CancelButton.DialogResult = "Cancel"

# ラベルの設定
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,30) 
$label.Size = New-Object System.Drawing.Size(250,20) 
$label.Text = "ファイル数を入力する"

# 入力ボックスの設定
$textBox = New-Object System.Windows.Forms.TextBox 
$textBox.Location = New-Object System.Drawing.Point(10,70) 
$textBox.Size = New-Object System.Drawing.Size(225,50) 

# キーとボタンの関係
$form.AcceptButton = $OKButton
$form.CancelButton = $CancelButton

# ボタン等をフォームに追加
$form.Controls.Add($OKButton)
$form.Controls.Add($CancelButton)
$form.Controls.Add($label) 
$form.Controls.Add($textBox)

# フォームを表示させ、その結果を受け取る
$result = $form.ShowDialog()

# 結果による処理分岐
if ($result -eq "OK")
{
    $my_files = $textBox.Text
} elseif ($result -eq "Cancel")
{
    exit
}

###
###
###
$my_folder = [Environment]::GetFolderPath('Desktop') + "\" + (Get-Date).ToString("yyyyMMdd-HHmm")
New-Item $my_folder -itemType Directory

###
for ( $i = 1; $i -lt $my_files; $i++ )
{
    $get_target = $my_url + $i + ".jpg"
    $dl_target = $my_folder +"/" + $i.tostring("000") + ".jpg"

    Invoke-WebRequest -Uri $get_target -OutFile $dl_target
}