Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cmd save " character to a file without new line

Tags:

batch-file

cmd

Is that possible save a special character " to a string without new line? I tried below codes ,but none of them working:

echo|set /p=""" >>C:\text.txt
echo|set /p=" >>C:\text.txt
echo|set /p=^" >>C:\text.txt
set /p=""" <nul >> C:\text.txt
set /p=" <nul >> C:\text.txt
set /p=^" <nul >> C:\text.txt
like image 223
momo Avatar asked Jan 31 '17 00:01

momo


3 Answers

You almost had it

<nul >"file.txt" set/p="""

but, as Squashman points, if you try to write to the root of your drive, you will need an elevated prompt

like image 51
MC ND Avatar answered Oct 25 '22 07:10

MC ND


Here is a slightly different method which does not require the redirection to be put in front of the set /P command:

set /P =""^" < nul >> "C:\text.txt"

Since the last of the three quotation marks is escaped like ^", the remaining command line portion does not appear quoted and therefore, the redirection operators < and >> are recognised.

like image 22
aschipfl Avatar answered Oct 25 '22 07:10

aschipfl


Not a one-liner in the command-prompt but as a batch-file the following works for me:

>>output.txt (
echo|set/p="""
)

Or if you want to hit return to write it use the following, very similar one:

>>output.txt (
set /p=""" < nul
)

One could as well establish it to be a kind of a function to use dynamically:

>>"%~dp1" (
echo|set/p="""
)

To use this one, you pass the filename as first command-line argument as the following:

addQuoteTo.bat "C:\my Path\toFile\file.txt"

Was an interesting thing to play around with :)

like image 34
geisterfurz007 Avatar answered Oct 25 '22 07:10

geisterfurz007