22

How do I resize a file from the command prompt (or in a batch file)?

  • It needs to treat the file as binary. The file format is irrelevant and arbitrary.

  • Extending the file should pad it with zeros.

  • Shrinking the file should truncate the file.

In other words:

How do you duplicate the functionality of SetEndOfFile in the command prompt?

2
  • You could try diskpart.
    – jpaugh
    Commented Sep 1, 2017 at 19:40
  • To extend the file, do you want to pad it with zeros (ASCII) or nulls (bytes)?
    – Zimba
    Commented Aug 16, 2023 at 4:29

5 Answers 5

12

I don't believe the functionality exists natively in the DOS shell, I suspect you need to use another program to get the job done. Here are a few possibilities:

  1. There is a tool called Trunc which claims to do exactly what you are after:

Trunc is a Windows command-line app to truncate files to a specified size. Also usable to enlarge a file.

You'll have to try it out to find out exactly how the file is getting padded out (e.g. with zeros or garbage) but otherwise this seems to fit the bill of what you are asking for.

  1. fsutil is a Windows XP and above system tool that appears to have some functionality that might help. Specifically the fsutil file subcommand appears to be able to create, set length and also zero fill files.

Specifically:

fsutil file createnew "C:\temp\myfile" 1024

Will create a zero filled 1024 byte file.

To change the size of an existing file:

fsutil file seteof filename length
  1. UVHD is a hex editor that appears to have the functions you are asking for. The only problem is that it only works under Windows/SFU (Services for Unix) and not DOS. What that actually means to an end user I don't actually know, but I have a vague suspicion that SFU was dropped in Vista and Win 7.
8
  • 1
    Ah okay. Thanks, trunc does what I need. Oh, and a note: SFU (or rather, SUA -- "Subsystem for Unix-Based Applications", aka Interix) wasn't dropped in Windows 7; it's just now an optional component in the Ultimate version, unfortuntately. :\
    – user541686
    Commented Jun 20, 2011 at 12:47
  • Ah, fair enough, I couldn't find much in the way of information on SUA/SFU though to be honest I didn't look too closely. I've also found another way to create arbitrary length files using fsutil and edited my answer, but as I noted I cannot see a valid way to truncate the files.
    – Mokubai
    Commented Jun 20, 2011 at 13:03
  • By the way, I thought I'd point something out: I believe the valid data length is the virtual length of the file on the disk, not the physical length. If the file is compressed, for example, it could actually be smaller -- you'd need the compressed size of the file.
    – user541686
    Commented Jun 22, 2011 at 0:45
  • 2
    But fsutil does have a command to set the EOF, fsutil file seteof filename length, I checked and this works as expected (I found this on ss64.com/nt/fsutil.html)
    – lukeuser
    Commented Nov 11, 2020 at 20:33
  • @lukeuser I suspect that when this answer was written 9 years ago the command may not have existed. docs.microsoft.com/en-us/windows-server/administration/… seems to suggest that there is something in the fsutil file has some applicability only to Windows 8 and above. I know it existed in Win7, but I do not remember seeing that particular command when I researched this all those years back.
    – Mokubai
    Commented Nov 11, 2020 at 20:47
9

To set "SetEndOfFile in the command prompt":

FSUTIL file seteof <filename> <truncated size>

To increase (existing)file size (padding nulls):

FSUTIL file seteof <filename> <new size>

Works at file system level, much faster than copying required file contents into a new file.

0
5
copy /Y nul: file.txt

For setting to 0 bytes only, of course.

2
  • 4
    This really doesn't seem to answer the question. Commented Dec 19, 2012 at 2:51
  • 1
    +1 I agree that it's not the right answer but it's exactly what I came here looking for - how to truncate a file from the Windows command line. Commented Feb 3, 2016 at 14:01
2

If you have GnuWin32 installed, then you can use dd to do it.

dd if=nul of=/the/file/to/truncate bs=1 count=0 seek=the_file_size oflag=append
1
  • dd potential never ceases to amaze me, command works but without if=nul.
    – too
    Commented Aug 22, 2017 at 7:55
2

try my BSD truncate build for Windows.

EDIT: it is not working with files >2GB.

Alternatives that works well with big files:

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .