在 Windows 系统上快速创建大文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/982659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:28:50  来源:igfitidea点击:

Quickly create large file on a Windows system

windowsbatch-file

提问by Leigh Riffel

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. The content doesn't matter. A built-in command or short batch file would be preferable, but I'll accept an application if there are no other easy ways.

在 Linux 系统上快速创建大文件一样,我想在 Windows 系统上快速创建一个大文件。总的来说,我在想 5 GB。内容无所谓。内置命令或短批处理文件会更可取,但如果没有其他简单的方法,我会接受应用程序。

回答by Patrick Cuff

fsutil file createnew <filename> <length>

where <length>is in bytes.

哪里<length>是字节。

fsutilrequires administrative privileges though.

fsutil虽然需要管理权限。

回答by Joey

You can use the Sysinternals Contigtool. It has a -nswitch which creates a new file of a given size. Unlike fsutil, it doesn't require administrative privileges.

您可以使用Sysinternals Contig工具。它有一个-n开关,可以创建一个给定大小的新文件。与 不同fsutil,它不需要管理权限。

回答by Giri

I was searching for a way to generate large files with data, not just sparse file. Came across the below technique:

我正在寻找一种方法来生成包含数据的大文件,而不仅仅是稀疏文件。遇到了以下技术

If you want to create a file with real data then you can use the below command line script.

echo "This is just a sample line appended to create a big file.. " > dummy.txt
for /L %i in (1,1,14) do type dummy.txt >> dummy.txt

(Run the above two commands one after another or you can add them to a batch file.)

The above commands create a 1 MB file dummy.txt within few seconds...

如果要使用真实数据创建文件,则可以使用以下命令行脚本。

echo "This is just a sample line appended to create a big file.. " > dummy.txt
for /L %i in (1,1,14) do type dummy.txt >> dummy.txt

(以上两条命令一个接一个运行,也可以添加到批处理文件中。)

上面的命令会在几秒钟内创建一个 1 MB 的文件 dummy.txt...

回答by ZXX

Check out RDFC http://www.bertel.de/software/rdfc/index-en.html

查看 RDFC http://www.bertel.de/software/rdfc/index-en.html

RDFC is probably not the fastest, but it does allocate data blocks. The absolutely fastest would have to use lower level API to just obtain cluster chains and put them into MFTwithout writing data.

RDFC 可能不是最快的,但它确实分配数据块。绝对最快的方法是使用较低级别的 API 来获取集群链并将它们放入MFT 中而不写入数据。

Beware that there's no silver bullet here - if "creation" returns instantly that means you got a sparse file which just fakes a large file, but you won't get data blocks/chains till you write into it. If you just read is you'd get very fast zeros which could make you believe that your drive all of the sudden got blazingly fast :-)

请注意,这里没有灵丹妙药——如果“创建”立即返回,则意味着您得到了一个稀疏文件,它只是伪造了一个大文件,但在写入之前您不会获得数据块/链。如果你刚刚读到的是,你会得到非常快的零,这可能会让你相信你的驱动器突然变得非常快:-)

回答by Byron Whitlock

Check the Windows Server 2003 Resource Kit Tools. There is a utility called Creatfil.

检查Windows Server 2003 资源工具包工具。有一个名为 Creatfil 的实用程序。

 CREATFIL.EXE
 -? : This message
 -FileName -- name of the new file
 -FileSize -- size of file in KBytes, default is 1024 KBytes

It is the similar to mkfile on Solaris.

它类似于 Solaris 上的 mkfile。

回答by f.ardelian

I needed a regular 10 GB file for testing, so I couldn't use fsutilbecause it creates sparse files (thanks @ZXX).

我需要一个常规的 10 GB 文件进行测试,所以我无法使用,fsutil因为它会创建稀疏文件(感谢 @ZXX)。

@echo off

:: Create file with 2 bytes
echo.>file-big.txt

:: Expand to 1 KB
for /L %%i in (1, 1, 9) do type file-big.txt>>file-big.txt

:: Expand to 1 MB
for /L %%i in (1, 1, 10) do type file-big.txt>>file-big.txt

:: Expand to 1 GB
for /L %%i in (1, 1, 10) do type file-big.txt>>file-big.txt

:: Expand to 4 GB
del file-4gb.txt
for /L %%i in (1, 1, 4) do type file-big.txt>>file-4gb.txt

del file-big.txt

I wanted to create a 10 GB file, but for some reason it only showed up as 4 GB, so I wanted to be safe and stopped at 4 GB. If you really want to be sure your file will be handled properly by the operating system and other applications, stop expanding it at 1 GB.

我想创建一个 10 GB 的文件,但由于某种原因它只显示为 4 GB,所以我想安全并停止在 4 GB。如果您真的想确保操作系统和其他应用程序正确处理您的文件,请停止将其扩展为 1 GB。

回答by shenku

Open up Windows Task Manager, find the biggest process you have running right click, and click on Create dump file.

打开 Windows 任务管理器,找到您运行的最大进程,右键单击,然后单击Create dump file

This will create a file relative to the size of the process in memory in your temporary folder.

这将在临时文件夹中创建一个与内存中进程大小相关的文件。

You can easily create a file sized in gigabytes.

您可以轻松创建以 GB 为单位大小的文件。

Enter image description here

在此处输入图片说明

Enter image description here

在此处输入图片说明

回答by Rod

PowerShell one-liner to create a file in C:\Tempto fill disk C: leaving only 10 MB:

PowerShell 单行创建一个文件C:\Temp来填充磁盘 C: 只留下 10 MB:

[io.file]::Create("C:\temp\bigblob.txt").SetLength((gwmi Win32_LogicalDisk -Filter "DeviceID='C:'").FreeSpace - 10MB).Close

回答by gimel

Short of writing a full application, us Python guys can achieve files of any size with four lines, same snippet on Windows and Linux (the os.stat()line is just a check):

无需编写完整的应用程序,我们的 Python 人员就可以使用四行代码实现任意大小的文件,Windows 和 Linux 上的代码片段相同(该os.stat()行只是一个检查):

>>> f = open('myfile.txt','w')
>>> f.seek(1024-1) # an example, pick any size
>>> f.write('\x00')
>>> f.close()
>>> os.stat('myfile.txt').st_size
1024L
>>>

回答by lkurts

Use:

用:

/*
Creates an empty file, which can take all of the disk
space. Just specify the desired file size on the
command line.
*/

#include <windows.h>
#include <stdlib.h>

int main (int argc, char* ARGV[])
{
    int size;
    size = atoi(ARGV[1]);
    const char* full = "fulldisk.dsk";
    HANDLE hf = CreateFile(full,
                           GENERIC_WRITE,
                           0,
                           0,
                           CREATE_ALWAYS,
                           0,
                           0);
    SetFilePointer(hf, size, 0, FILE_BEGIN);
    SetEndOfFile(hf);
    CloseHandle(hf);
    return 0;
}