LoginSignup
2
1

More than 5 years have passed since last update.

PowerShellでバイト計算

Last updated at Posted at 2019-04-24

言いたいこと

KBやMBなどのデータ量の単位を利用した計算が可能
(PowerShell 5.1で確認したところKB~PBまで)

PS v:\test> 1KB
1024
PS v:\test> 1KB*0.5
512
PS v:\test> 1MB
1048576
PS v:\test> 1MB*2
2097152
PS v:\test> 1PB
1125899906842624
PS v:\test> 1EB
1EB : 用語 '1EB' は、コマンドレット、関数、スクリプト ファイル、または操作可能なプログラムの名前として認識されません。
名前が正しく記述されていることを確認し、パスが含まれている場合はそのパスが正しいことを確認してから、再試行してください
。
発生場所 行:1 文字:1
+ 1EB
+ ~~~
    + CategoryInfo          : ObjectNotFound: (1EB:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

ダミーファイル作成

接頭辞付の数値の直接指定はエラーになる

PS v:\test> fsutil file createnew test1mb.txt 1MB
使用法 : fsutil file createNew <ファイル名> <長さ>
   例 : fsutil file createNew C:\testfile.txt 1000
PS v:\test>

一度変数に代入してから作成 ⇒ 成功

PS v:\test> $size = 1MB
PS v:\test> fsutil file createnew test1mb.txt $size
ファイル v:\test\test1mb.txt が作成されました
PS v:\test>

サイズ確認(1024*1024=1048576) ⇒ OK

PS v:\test> Get-ChildItem


    ディレクトリ: v:\test


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       2019/04/24     21:47        1048576 test1mb.txt


PS v:\test>

ワンライナーだと↓のように。
要素数1(値:1048576)の連番のオブジェクト(追って説明)を処理している。

PS v:\test> 1MB | % {fsutil file createnew test1mb.txt $_ }
ファイル v:\test\test1mb.txt が作成されました
PS v:\test> dir


    ディレクトリ: v:\test


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       2019/04/24     22:27        1048576 test1mb.txt


PS v:\test>

連番

PowerShellではこんな感じ↓で連番を簡単に作成可能だが、

PS v:\test> 1..10
1
2
3
4
5
6
7
8
9
10
PS v:\test>

接頭辞付の数値の直接指定でもOK

PS v:\test> 1..1KB
1
2
3
4
5
(中略)
1022
1023
1024
PS v:\test>

パイプでつないで利用OK

PS v:\test> 1..1KB | % {New-Item ("test$_.txt")}


    ディレクトリ: v:\test


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       2019/04/24     22:40              0 test1.txt
-a----       2019/04/24     22:40              0 test2.txt
-a----       2019/04/24     22:40              0 test3.txt
-a----       2019/04/24     22:40              0 test4.txt
-a----       2019/04/24     22:40              0 test5.txt
(中略)
-a----       2019/04/24     22:40              0 test1020.txt
-a----       2019/04/24     22:40              0 test1021.txt
-a----       2019/04/24     22:40              0 test1022.txt
-a----       2019/04/24     22:40              0 test1023.txt
-a----       2019/04/24     22:40              0 test1024.txt


PS v:\test>
2
1
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
1