根据数据大小创建Powershell大文件
我正在尝试确定什么 Powershell 命令等效于以下 Linux 命令,用于在合理的时间内创建具有确切大小并填充给定文本输入的大文件。
由于某种原因自动关闭了这个问题,因此提出了新问题。
鉴于:
$ cat line.txt
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~ZZZZ
$ time yes `cat line.txt` | head -c 10GB > file.txt # create large file
real 0m59.741s
$ ls -lt file.txt
-rw-r--r--+ 1 k None 10000000000 Feb 2 16:28 file.txt
$ head -3 file.txt
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~ZZZZ
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~ZZZZ
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~ZZZZ
什么是最有效、最紧凑的 Powershell 命令,它允许我指定大小、文本内容并创建文件作为上面的 Linux 命令?谢谢!
回答
Fsutil 仍然存在。
在PS中,它是这样的:
$file = [System.IO.File]::Create("C:usersMeDesktoptest.txt") #input path here
$file.SetLength([double]10mb) #Change file size here
$file.Close()
它实际上只是使用原始数据类型。
如果您要继续更频繁地执行此操作,我建议您将其放入函数中:
#Syntax:
# Create-File [-Path] <string> [-Size] <string>
#Example:
# Create-File -Path c:Usersmetest.txt -Size 20mb
Function Create-File{
Param(
[Parameter(Mandatory=$True,Position=0)]
$path,
[Parameter(Mandatory=$True,Position=1)]
$size)
$file = [System.IO.File]::Create("$path")
$file.SetLength([double]$size)
$file.Close()
}
Fsutil使用类似fsutil file createnew filename filesize,基于此链接:here
编辑:猜你可以像这样使用powershell添加类型:
$file = new-object System.IO.FileStream c:usersmetest.txt, Create, ReadWrite
$file.SetLength(10MB)
$file.Close()