小编erl*_*mar的帖子

使用随机数据创建文件的最快方法

我需要创建一个填充了随机数据的指定大小的文件.我不能使用任何第三方工具来实现这一点,所以我所拥有的只是所有Powershell命令.

我在这里的工作适用于大小从1 KB到30 KB的小文件.当文件变大时,它不能很好地扩展.

function makeFile([String]$filename, [int]$SizeInKb) {
    $str  = ""
    $size = 29 * $SizeInKb
    if (-not (Test-Path $filename)) {
        New-Item $filename -ItemType File | Out-Null
        for ($i=1; $i -le $size; $i++) {
            # A GUID is 36 characters long
            # We will create a string 29*36 (1044) characters in length and
            # multiply it with $SizeInKb
            $str += [guid]::NewGuid()
        }
        write $str to a file barring the last ($SizeInKb * 20) + 2 characters. 
        $strip_length = ($SizeInKb * …
Run Code Online (Sandbox Code Playgroud)

powershell

5
推荐指数
1
解决办法
2275
查看次数

标签 统计

powershell ×1