Linuxにはtouchコマンドがあります。
- アクセス時刻と更新時刻を現在(あるいは任意)の時刻に変更
- ファイルがない場合は空ファイルの生成
Windowsで、バッチでやろうというのを見かけるのですが苦しいのでPowerShellでサクリと。
guitarrapc/PowerShellUtil - LinuxCommand | GitHub
MS-DOSコマンドで更新時間の更新
このような感じで出来ます。
Updating the Date and Time Stamps on Files
COPY /B EXAMPLE +,,
PowerShellで更新時間の更新
PowerShellならもっと自在に行えます。 この場合は、 Set-ItemPropertyが適当でしょうか。
Set-ItemProperty -Path D:\test.log -Name LastAccessTime -Value $(Get-Date)
これで現在時刻にd:\text.logの更新時刻が修正されます。
touch Cmdlet を作成してみる
Linuxのコマンド概要を参考に、touchコマンドレットを作成してみます。
function touch{ [CmdletBinding()] param( [parameter( position = 0, mandatory = 1, ValueFromPipeline = 1, ValueFromPipelineByPropertyName = 1 )] [string]$path, [parameter( position = 1, mandatory = 0, ValueFromPipeline = 1, ValueFromPipelineByPropertyName = 1 )] [datetime]$date = $(Get-Date), [parameter( position = 2, mandatory = 0, HelpMessage = "Change Last AccessTime only" )] [switch]$access, [parameter( position = 3, mandatory = 0, HelpMessage = "Do not create file if not exist" )] [switch]$nocreate, [parameter( position = 4, mandatory = 0, HelpMessage = "Change Last WriteTime only" )] [switch]$modify, [parameter( position = 5, mandatory = 0, HelpMessage = "LastAccessTime reference file" )] [string]$reference ) if (-not(Test-Path $path)) { if ((!$nocreate)) { New-Item -Path $path -ItemType file -Force } } else { try { if ($reference) { $date = (Get-ItemProperty -Path $reference).LastAccessTime } if ($access) { Get-ChildItem $path | %{Set-ItemProperty -path $_.FullName -Name LastAccessTime -Value $date -Force -ErrorAction Stop} } if ($modify) { Get-ChildItem $path | %{Set-ItemProperty -path $_.FullName -Name LastWriteTime -Value $date -Force -ErrorAction Stop} } if (!$access -and !$modify) { Get-ChildItem $path | %{Set-ItemProperty -path $_.FullName -Name LastAccessTime -Value $date -Force -ErrorAction Stop} Get-ChildItem $path | %{Set-ItemProperty -path $_.FullName -Name LastWriteTime -Value $date -Force -ErrorAction Stop} } } catch { throw $_ } finally { Get-ChildItem $path | %{Get-ItemProperty -Path $_.FullName | select Fullname, LastAccessTime, LastWriteTime} } } }
使用方法
そのままなのですが一応。
ファイルが存在しない場合に新規空ファイルを作成
PS> touch -path d:\test\hoge.log Directory: D:\test Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 2013/08/19 9:54 0 hoge.log
ファイルが存在しない場合でも新規ファイルを作らない場合は、-nocreateスイッチを付けます。
touch -path d:\test\hoge.log -nocreate
ファイルが存在した状態でそのまま実行すると、LastWriteTimeとLastAccessTimeが更新します。
PS> touch -path d:\test\hoge.log FullName LastAccessTime LastWriteTime -------- -------------- ------------- D:\test\hoge.log 2013/08/19 10:00:01 2013/08/19 10:00:01
-modifyと -accessスイッチを付けると明示的にLastWriteTimeとLastAccessTimeの両方が更新されます。
PS> touch -path d:\test\hoge.log -modify -access FullName LastAccessTime LastWriteTime -------- -------------- ------------- D:\test\hoge.log 2013/08/19 10:00:02 2013/08/19 10:00:02
-accessスイッチのみを付けると、 LastAccessTimeが更新されます。
PS> touch -path d:\test\hoge.log -access FullName LastAccessTime LastWriteTime -------- -------------- ------------- D:\test\hoge.log 2013/08/19 10:00:59 2013/08/19 10:00:01
-modifyスイッチのみを付けると、 LastWriteTimeが更新されます。
PS> touch -path d:\test\hoge.log -modify FullName LastAccessTime LastWriteTime -------- -------------- ------------- D:\test\hoge.log 2013/08/19 10:00:59 2013/08/19 10:01:53
-referenceパラメータに、参照元のファイルを指定すると、そのファイルの内容に沿って更新します。 更新内容は、accessやmodifyスイッチに依存します。
PS> touch -path d:\test\hoge.log -modify -access -reference C:\bootmgr FullName LastAccessTime LastWriteTime -------- -------------- ------------- D:\test\hoge.log 2012/07/26 20:47:56 2012/07/26 20:47:56
pathに * を指定すれば、そのフォルダのアイテム全てが更新されます。
PS> touch -path d:\test\* -modify -access -reference C:\bootmgr FullName LastAccessTime LastWriteTime -------- -------------- ------------- D:\test\hoge.log 2012/07/26 20:47:56 2012/07/26 20:47:56 D:\test\hogefuga.log 2012/07/26 20:47:56 2012/07/26 20:47:56
まとめ
本家touchと同一かと言われるとどうなのという感じですが、おおよそこういった感じでしょうか。