23

I have a unix command that does the following:

head -c 2048 > test.txt

basically its taking first 2kb of the test.txt file.

Can we do something similar in windows cmd prompt?

4
  • thank you guys for all your awesome replies..@sonjz I have a text file with one long string of hex dump..how can i use your code to extract only 2kb worth of data from it?? I tried your code but its still extracting the whole hex dump..thanks..i will upload the text file file for you..
    – plasma33
    Jan 3, 2015 at 5:41
  • please find the link here-> dropbox.com/s/h29mdg0t8df8i1e/test.txt?dl=0
    – plasma33
    Jan 3, 2015 at 5:52
  • @ sonjz: how to use "Get-FileHex" to extract 2kb of hex data using powershell..do you know about this function??
    – plasma33
    Jan 3, 2015 at 6:21
  • you want to extract byte array. The following code will yank 2KB and write to a new file: $byteArray = Get-Content binary.bin -Encoding Byte -TotalCount 2KB; [io.file]::WriteAllBytes('c:\binary.bin', $byteArray)
    – sonjz
    Jan 6, 2015 at 1:03

3 Answers 3

21

Simplifying this answer because of @chubbsondubs' comment.

-TotalCount will count lines if reading in text, so always force it to read the file as bytes, then the -TotalCount will only refer to bytes and you can get an account count.

Get-Content test.txt -Encoding byte -TotalCount 2KB | Set-Content test1.txt -Encoding byte

More information here: https://stackoverflow.com/questions/888063/powershell-to-get-the-first-x-mb-of-a-file

5
  • This gets the first 2048 lines not bytes of the file. TotalCount takes the number of lines you want. Jun 26, 2018 at 14:40
  • @chubbsondubs yup, i see the issue now, you always have to encode and decode in bytes, i'll update the answer
    – sonjz
    Jun 27, 2018 at 1:11
  • this is not the command prompt, its powershell
    – user32882
    Mar 9, 2019 at 4:54
  • 1
    Question was for "command line", both command prompt and PowerShell are command line shells for Windows. Command prompt being the archaic "stop using this" one.
    – sonjz
    Mar 10, 2019 at 22:07
  • This doesn't work in Powershell 6. In order for it to work, you must replace -Encoding byte with -AsByteStream
    – Omid
    Mar 18, 2020 at 19:59
3

From what I can tell you can't print out by size in a native way; there is the type command which will output a whole text file, but you can't specify how much you want to output.

There is also the more command, which will allow you to print out lines of a file. These are some of the flags from more /?:

/E      Enable extended features
/C      Clear screen before displaying page
/P      Expand FormFeed characters
/S      Squeeze multiple blank lines into a single line
/Tn     Expand tabs to n spaces (default 8)

        Switches can be present in the MORE environment
        variable.

+n      Start displaying the first file at line n

files   List of files to be displayed. Files in the list
        are separated by blanks.

If extended features are enabled, the following commands
are accepted at the -- More -- prompt:

P n     Display next n lines
S n     Skip next n lines
F       Display next file
Q       Quit
=       Show line number
?       Show help line
<space> Display next page
<ret>   Display next line

If neither of these work for you, you can alternatively install Cygwin and you can use cat or head.

4
  • cygwin also has the 'head' command if you choose to use that option Jan 3, 2015 at 3:41
  • @JeffClayton Shoot.. you're right, for some reason I was thinking cat instead of head
    – cutrightjm
    Jan 3, 2015 at 3:43
  • 'cat' is the unix command that matches up to the 'type' command in windows, since you were discussing 'type' it only makes sense. Jan 3, 2015 at 3:45
  • Question is asking to process bytes in file, not lines
    – Zimba
    Mar 25, 2021 at 23:18
2

To keep first 2048 bytes of test.txt:

FSUTIL file seteof test.txt 2048

To keep a copy of test.txt, make a copy of it first.
Much faster than piped powershell commands, as in the selected answer; works at OS file system level.

Tested in Win 10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.