Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Bash has the feature of quoted string expansion, which will allow you to send special characters like so

#!/bin/sh
rtmpdump \
  -r rtmp://freeview.fms.visionip.tv/live \
  -y tvnetwork-hellenictv-sigma-hsslive-25f-4x3-SDh$'\r' \
  -o out.flv

In this case a literal carriage return being passed. The question is, how would you go about sending a literal carriage return with cmd.exe?

share|improve this question

1 Answer 1

up vote 3 down vote accepted

Windows CMD.EXE does not have a built in facility to work with special characters like linefeed or carriage return. But there are known hacks that can give you access to a few frequently needed characters.

CarriageReturn (CR) 0x0D

All that is needed is a file - any file. Preferably the file should be small so that the command runs faster. The contents of the file do not matter. It could even be empty!

The COPY /Z command prints a percent completion status without LF that leads with a CR repeatedly until it reaches 100%. A properly formed FOR statement can parse out the CR from the beginning of the line. You can then use the FOR variable in your statement.

D:\test>for /f "delims=1 " %A in ('copy /z test.txt nul') do @echo Good morning %A world!
 world!rning

In the above, " world!" over-writes the beginning of "Good morning" because of the CR contained in the %A variable.

The CR could be placed in an environment variable so that it can be used later on, but the CR will only be preserved if delayed expansion is used. Delayed expansion can be enabled by using cmd /v:on.

D:\test>cmd /v:on
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

D:\test>for /f "delims=1 " %A in ('copy /z test.txt nul') do set "CR=%A"

" \test>set "CR=

D:\test>echo Good morning !CR! world!
 world!rning

D:\test>echo Good morning %CR% world! does not preserve the CR
Good morning  world! does not preserve the CR


LineFeed (LF) 0x0A

An LF can be incorporated into a command directly by using line continuation and entering a blank line to represent the escaped LF, followed by the remainder of the line.

D:\test>echo hello^
More?
More? world!
hello
world!

The LF can easily be stored in a variable where it can be used later. You cannot simply use %LF%, but you can use ^%LF%%LF% to represent a single LF.

D:\test>set LF=^
More?
More?

D:\test>echo Hello^%LF%%LF%world!
Hello
world!

Or delayed expansion can be used to conveniently access the LF variable

D:\test>cmd /v:on
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

D:\test>echo Hello!LF!world!
Hello
world!

Or another variable can be defined that has the proper escape sequence built in so that the variable can be used directly with normal expansion.

D:\test>set \n=^^^%LF%%LF%^%LF%%LF%

D:\test>echo Hello%\n%world!
Hello
world!


BackSpace (BS) 0x08

The prompt can be set to (BS)(space)(BS) by using $H, and a single BS can be parsed out using a FOR loop. The BS character can be stored in an environment variable to be used later.

D:\test>for /f %A in ('prompt $h^&for %a in (1^) do rem') do @set "BS=%A"

D:\test>echo ab_%BS%cd
abcd
share|improve this answer
    
+1 for a better solution. Marking mine for deletion. –  Nicole Hamilton Sep 20 '12 at 3:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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