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?

【PowerShell】サイズを指定してファイルを沢山作る方法

Last updated at Posted at 2024-06-06

はじめに

以前にLinux環境での大きなサイズのファイルを沢山作る方法を記事としてまとめましたが、今回は同様の処理をPowerShellで実装する方法を記事としてまとめてみました。

(今回の方法はファイル作成が高速かどうかは分からないので、記事のタイトルから高速の文言を外しました)

作成したコード

  • fsutilコマンドを使ってファイルを作成する方法は、こちらのサイトを参考にしました。
    • 上記のリンク先のサイトではfsutilコマンドを使ったスクリプトが紹介されていますが、今回は関数としてまとめてみました。
  • 以下のCreateFiles関数ではデフォルト引数が設定されているため、デフォルトだと「10KBのファイルを100個作る」という設定となります。
    • 引数で指定する必要があるのは出力先フォルダパスのみとなります。
CreateFiles.ps1
function CreateFiles{
    param(
        [int]$fileCount = 100,
        [string]$fileSize = 10 * 1024, # 10KB
        [string]$outputPath
    )
    
    if (!(Test-Path -Path $outputPath)) {
        throw "出力先フォルダが存在しません。"
    }
    
    for($i=0; $i -lt $fileCount; $i++) {
        $fileName = Join-Path $outputPath "${i}.txt"
        fsutil file createnew $fileName ($fileSize) | out-null
    }
}
テストコード
# C:\testに10KBのファイルを100個作る。
CreateFiles -outputPath "C:\test"

# C:\testに10MB(10*1024*1024B)のファイルを10個作る。
CreateFiles -fileCount 10 -fileSize (10 * 1024 * 1024) -outputPath "C:\test"

注意点

  • こちらのページを見ると、「fsutilで作成されたファイルの内容はゼロデータのみ」と書かれています。
    • そのため、ファイルの読み込み速度の測定などに利用する際には、fsutilで作ったファイルを使うのは不適当だそうです。

参考URL

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?