Windows 容量指定ダミーファイルを複数作成するシェルスクリプト


windowsで負荷テストを行う際に使えるコマンドやシェルスクリプトをご紹介します。

環境

以下の環境での動作は確認しています。
Windows7 Enterprise × Powershell 5.0
Windows10 Pro × Powershell 5.1

Windowsでダミーファイル作成コマンド

構文

> fsutil file createnew {ファイル名} {ファイル容量(バイト)}

以下は、10バイトのファイルを作成する場合の例です。

> fsutil file createnew testfile 10

容量を指定した複数のダミーファイルを作成

複数のファイルを作成する場合には、シェルスクリプトを使うと便利です。
下記をコピーしてpowershellで実行すると、指定した容量の複数のダミーファイルを作成できます。

createFiles.ps1
Param(
    [Parameter(Mandatory=$true)][string]$path,
    [Parameter(Mandatory=$true)][int]$count,
    [Parameter(Mandatory=$true)][int]$size
)
# ファイル作成または上書き
for ($i = 0; $i -lt $count; $i++) {
    $filePath = $path + "\testFile" + $i
    if (Test-Path $filePath) {
        Remove-Item $filePath;
    }
    fsutil file createnew $filePath $size
}

実行コマンド

> .\createFiles.ps1 {フォルダパス} {数量} {容量}

例)C:\testに100バイトのファイルを10個作成する場合

> .\createFiles.ps1 C:\test 10 100