Quickly create large file on a Windows system

前端 未结 23 2315
别那么骄傲
别那么骄傲 2020-12-07 06:35

In the same vein as Quickly create a large file on a Linux system, I\'d like to quickly create a large file on a Windows system. By large I\'m thinking 5 GB.

23条回答
  •  遥遥无期
    2020-12-07 07:21

    Temp files should be stored in the Windows Temp Folder. Based on the answer from Rod you can use the following one liner to create a 5 GB temp file which returns the filename

    [System.IO.Path]::GetTempFileName() | % { [System.IO.File]::Create($_).SetLength(5gb).Close;$_ } | ? { $_ }
    

    Explanation:

    • [System.IO.Path]::GetTempFileName() generates a random filename with random extension in the Windows Temp Folder
    • The Pipeline is used to pass the name to [System.IO.File]::Create($_) which creates the file
    • The file name is set to the newly created file with .SetLength(5gb). I was a bit surprised to discover, that PowerShell supports Byte Conversion, which is really helpful.
    • The file handle needs to be closed with .close to allow other applications to access it
    • With ;$_ the filename is returned and with | ? { $_ } it is ensured that only the filename is returned and not the empty string returned by [System.IO.File]::Create($_)

提交回复
热议问题