2

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

@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>) character?

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 or JSscript 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.

4 Answers 4

4

A single line feed can be written to a file by the use of the prompt command

@echo off
setlocal

(set prompt=^
%= Don't delete this line =%
)

cmd /d /k < nul > single-linefeed.txt
Sign up to request clarification or add additional context in comments.

4 Comments

Your solution destroys the command line prompt and must be used from a batch file - it cannot be used in the command line console because of the empty line (yes the latter was not a requirement). The following command does not have these drawbacks: echo(|(pause>nul & findstr "^")>single-linefeed.txt
I added a setlocal to restore the prompt. Btw. findstr is also an exe like makecab and could be blocked
True, however blocking findstr would break many batch files but nobody seems to miss the arcane makecab. Anyway, your solution uses only internal commands so it gets an upvote. Can you explain how the cmd.exe terminates despite the /K switch ?
cmd.exe /K terminates by an EXIT or in case of input redirection, when the input file ends.
3

The following code writes 1 (LF) line-feed character (ASCII 10 or 0x0A) to the file out.txt:

echo(|(pause>nul & findstr "^")>out.txt

This code can be used in the command line console as well as inside a batch file, because it does not require a line break <CR><LF> after the ^ character, like in the accepted answer's code, which can be used only inside a batch file.

On the downside, this code uses one native external command (the findstr.exe) while the accepted answer uses only internal commands.

Comments

1

This code does what you want, and uses pure internal Batch commands:

@echo off

copy NUL /B CtrlZ.txt /A > NUL
for /F "delims=" %%z in (CtrlZ.txt) do echo ^
%Don't remove this line%
%%z > temp.txt
copy temp.txt /A out.txt /B > NUL

First, we generate a temp file with <LF><Ctrl-Z> character pair via an echo command. Then, just copy such an ASCII temp file to a binary one, so any characters from the Ctrl-Z on are discarded...

Comments

0

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 10 to it as an argument, then the file 10.chr would be created, containing only a linefeed character.

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

5 Comments

The code for line-feed is 0x0a or 10 ...not 13. The makecab is locked down by AppLocker.
Well without a complete list of everything that is locked down by AppLocker, it's not going to be possible to answer the question because you'll just keep going "actually, I can't use this solution either."
Not really. I found that the following is much simpler and works: cmd /D /U /C "echo("|(pause>nul & pause>nul & findstr "^") >out.txt . Does this work for you, too ?
Yeah, cmd /D /U /C "echo("|(pause>nul & pause>nul & findstr "^") >out.txt works.
Better yet: echo(|(pause>nul & findstr "^")>out.txt .Please add it to your answer so I can accept it.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.