LoginSignup
2
4

More than 5 years have passed since last update.

日付のフォルダ(ファイル)を連番で作成するPowerShellスクリプト

Posted at

前置き

テストデータ準備のために、日付のフォルダを連番で作りたい。
また、フォルダの更新日時も日付に合わせたい。
コマンドプロンプトでは扱いづらいため、PowerShellを使用する。

スクリプト

100日前から今日までの日付のフォルダを作成するスクリプト。

for( $i = -100; $i -le 0; $i++ )
{
    $Date = (Get-Date).AddDays($i).Date
    $Name = (Get-Date $Date -Format "yyyyMMdd")
    New-Item $Name -ItemType Directory -Force
    Set-ItemProperty $Name -Name LastWriteTime -Value $Date
}

New-Item -ItemType で Directory を指定。
既に存在している場合はエラーが出るため、-Force をつける。

Set-ItemProperty で 更新日時を修正。更新日時は $Date にする。

ファイルを作りたい場合

New-Item -ItemType を Directory から File に置き換えれば良い。

2
4
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
2
4