How to write a line feed to a file?

How to write a line feed to a file?

The following Windows batch file sets the contents of the out.txt file to two ASCII characters, namely: 0x58 and 0x0A (X, [LF]).

@echo off
SETLOCAL
SET LF=^


set /p=X^%LF%%LF%<nul>out.txt

Note the mandatory empty lines after the statement SET LF=^

How to change this batch file so it sets the contents of the out.txt file to only one ASCII character, namely the: 0x0A ([LF]) ?

RESTRICTIONS:
This question relates only to the internal commands implemented in cmd.exe (not PowerShell !) and only to the native external commands installed in standard Windows installations >= Windows 7 x64 (no 3rd party tools, no debug.exe). This question is not about how to accomplish this goal with scripting languages such as VBscript nor how to write a program in C, C#, Python or any language other than the Windows batch language (if you can call it that). Assume a restricted user without administrative rights, so neither fsutil nor certutil work.

Answer

Generation of single ASCII characters was discussed in this thread on DosTips several years ago, and they ultimately developed this solution that takes advantage of makecab:

REM This code creates one single byte. Parameter: <int>0-255
REM Teamwork of carlos, penpen, aGerman, dbenham
REM Tested under Win2000, XP, Win7, Win8

@echo off

set "options=/d compress=off /d reserveperdatablocksize=26"
if %~1 neq 26  (type nul >%~1.tmp
makecab %options% /d reserveperfoldersize=%~1 %~1.tmp %~1.chr >nul
type %~1.chr | (
(for /l %%N in (1 1 38) do pause)>nul&findstr "^">%~1.tmp)
>nul copy /y %~1.tmp /a %~1.chr /b
del %~1.tmp
) else (copy /y nul + nul /a 26.chr /a >nul)

In your case, you'd take this script and pass 13 to it as an argument, then the file 13.chr would be created, containing only a linefeed character.

On a semi-related note, regular users are absolutely able to use certutil.

Enjoyed this question?

Check out more content on our blog or follow us on social media.

Browse more questions