代码之家  ›  专栏  ›  技术社区  ›  Leigh Riffel

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

  •  224
  • Leigh Riffel  · 技术社区  · 16 年前

    与…在同一静脉中 Quickly create a large file on a Linux system , 我想在Windows系统上快速创建一个大文件。总的来说,我认为是5 GB。内容无关紧要。最好使用内置命令或短批处理文件,但如果没有其他简单的方法,我将接受应用程序。

    21 回复  |  直到 6 年前
        1
  •  364
  •   ivan_pozdeev RenanSS    7 年前
    fsutil file createnew <filename> <length>
    

    哪里 <length> 是字节的。

    fsutil 但需要管理权限。

        2
  •  34
  •   Joey Gumbo    7 年前

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

        3
  •  23
  •   gnat Nat Poor    7 年前

    我在寻找一种用数据生成大文件的方法,而不仅仅是稀疏文件。遇到 the below technique :

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

    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…

        4
  •  15
  •   Peter Mortensen icecrime    6 年前

    签出RDFC http://www.bertel.de/software/rdfc/index-en.html

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

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

        5
  •  13
  •   Samir Felix Kling    11 年前

    检查 Windows Server 2003 Resource Kit Tools . 有一个名为creatfil的实用程序。

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

    它与Solaris上的mkfile类似。

        6
  •  11
  •   f.ardelian    11 年前

    我需要一个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
    

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

        7
  •  6
  •   Peter Mortensen icecrime    6 年前

    用途:

    /*
    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;
    }
    
        8
  •  5
  •   Peter Mortensen icecrime    6 年前

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

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

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

    Enter image description here

    Enter image description here

        9
  •  4
  •   Peter Mortensen icecrime    6 年前

    PowerShell一行程序,用于在其中创建文件 C:\Temp 填充磁盘C:只留下10_MB:

    [io.file]::Create("C:\temp\bigblob.txt").SetLength((gwmi Win32_LogicalDisk -Filter "DeviceID='C:'").FreeSpace - 10MB).Close
    
        10
  •  4
  •   yosh m    6 年前

    我发现了一个很好的实用程序,可以在 https://github.com/acch/genfiles .

    它用随机数据填充目标文件,因此稀疏文件没有问题,而且就我的目的(测试压缩算法)而言,它提供了很好的白噪声级别。

        11
  •  3
  •   Peter Mortensen icecrime    6 年前

    除了编写完整的应用程序外,我们的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
    >>>
    
        12
  •  3
  •   Peter Mortensen icecrime    6 年前

    简单的ol'c…它在WindowsXX上的mingw gcc下构建,应该可以工作 在任何“通用”C平台上。

    它生成指定大小的空文件。结果文件不只是一个目录空间占用者条目,实际上占据指定的字节数。这很快,因为除了关闭前写入的字节外,没有实际写入。

    我的实例生成一个满是零的文件-这可能因平台而异;这 程序基本上为挂起的任何数据设置目录结构 周围。

    #include <stdio.h>
    #include <stdlib.h>
    
    FILE *file;
    
    int main(int argc, char **argv)
    {
        unsigned long  size;
    
        if(argc!=3)
        {
            printf("Error ... syntax: Fillerfile  size  Fname \n\n");
            exit(1);
        }
    
        size = atoi(&*argv[1]);
    
        printf("Creating %d byte file '%s'...\n", size, &*argv[2]);
    
        if(!(file = fopen(&*argv[2], "w+")))
        {
            printf("Error opening file %s!\n\n", &*argv[2]);
            exit(1);
        }
    
        fseek(file, size-1, SEEK_SET);
        fprintf(file, "%c", 0x00);
        fclose(file);
    }
    
        13
  •  3
  •   Peter Mortensen icecrime    6 年前

    我最近正在寻找一种方法来创建一个具有空间分配的大型虚拟文件。所有的解决方案看起来都很尴尬。最后我才开始 DISKPART Windows中的实用程序(自Windows Vista以来嵌入):

    DISKPART
    CREATE VDISK FILE="C:\test.vhd" MAXIMUM=20000 TYPE=FIXED
    

    在哪里? 最大值 是生成的文件大小,此处为20 GB。

        14
  •  2
  •   Leigh Riffel    16 年前

    我使用调试找到了一个解决方案 http://www.scribd.com/doc/445750/Create-a-Huge-File 但我不知道编写脚本的简单方法,而且它似乎无法创建大于1 GB的文件。

        15
  •  2
  •   CKuharski    9 年前

    …几秒钟内,1 MB文件dummy.txt。

    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
    

    请参见这里: http://www.windows-commandline.com/how-to-create-large-dummy-file/

        16
  •  2
  •   Community CDub    8 年前

    临时文件应存储在Windows临时文件夹中。基于 the answer from Rod 您可以使用下面的一行程序创建一个5GB的临时文件,该文件返回文件名

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

    说明:

    • [System.IO.Path]::GetTempFileName() 在Windows临时文件夹中生成具有随机扩展名的随机文件名
    • 管道用于将名称传递给 [System.IO.File]::Create($_) 它创建文件
    • 文件名设置为新创建的文件 .SetLength(5gb) . 我有点惊讶地发现,PowerShell支持 Byte Conversion 这真的很有帮助。
    • 文件句柄需要用关闭 .close 允许其他应用程序访问
    • ;$_ 返回文件名,并使用 | ? { $_ } 确保只返回文件名,而不返回空字符串 [系统.io.file]::创建($uuu)
        17
  •  1
  •   Peter Mortensen icecrime    6 年前

    python中的简单答案:如果需要创建一个大型的真实文本文件,我只使用了一个简单的 while 循环并能够在大约20秒内创建5_GB文件。我知道它很粗糙,但足够快。

    outfile = open("outfile.log", "a+")
    
    def write(outfile):
        outfile.write("hello world hello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello world"+"\n")
        return
    
    i=0
    while i < 1000000:
        write(outfile)
        i += 1
    outfile.close()
    
        18
  •  1
  •   Peter Mortensen icecrime    6 年前

    你可以试试这个C++代码:

        #include<stdlib.h>
        #include<iostream>
        #include<conio.h>
        #include<fstream>
        #using namespace std;
    
        int main()
        {
            int a;
            ofstream fcout ("big_file.txt");
            for(;;a += 1999999999){
                do{
                    fcout << a;
                }
                while(!a);
            }
        }
    

    可能需要一些时间来生成,这取决于您的CPU速度…

        19
  •  1
  •   yosh m    6 年前

    我找到的最简单的方法是这个免费的实用程序: http://www.mynikko.com/dummy/

    创建任意大小的虚拟文件,这些文件要么用空格填充,要么用不可压缩的内容填充(由您选择)。以下是截图:

    enter image description here

        20
  •  0
  •   Noctis Skytower    7 年前

    快速执行还是快速在键盘上键入?如果在Windows上使用python,可以尝试以下操作:

    cmd /k py -3 -c "with open(r'C:\Users\LRiffel\BigFile.bin', 'wb') as file: file.truncate(5 * 1 << 30)"
    
        21
  •  -1
  •   Alan Pallath    6 年前

    我做了一些补充 fsutil 方法如所选答案中所述。 这是为了创建具有许多不同扩展名和/或不同大小的文件。

    set file_list=avi bmp doc docm docx eps gif jpeg jpg key m4v mov mp4 mpg msg nsf odt pdf png pps ppsx ppt pptx rar rtf tif tiff txt wmv xls xlsb xlsm xlsx xps zip 7z
    set file_size= 1
    for %%f in (%file_list%) do (
    fsutil file createnew valid_%%f.%%f %file_size%
    ) > xxlogs.txt
    

    可以从中克隆代码 https://github.com/iamakidilam/bulkFileCreater.git