2

I have a batch file that does the following to write to a file:

echo @echo off> start.bat
echo cd %USERPROFILE%\Desktop\BukkitServer>> start.bat
echo java -Xmx1024M -jar craftbukkit.jar -o true>> start.bat

The only problem is that it adds one blank line to the end of the file "start.bat". I am wondering how to remove that empty line from within the same batch file.

leo60228
  • 754
  • 11
  • 21
  • possible duplicate of [Windows batch: echo without new line](http://stackoverflow.com/questions/7105433/windows-batch-echo-without-new-line) – rpax May 21 '14 at 20:02
  • 1
    What is the purpose of this dynamic generation (vs. keeping a static copy around)? The main difference I can see is that since the first script evaluates `%USERPROFILE%`, the second script could stop working if the actual value of `%USERPROFILE%` changes later. – nobody May 21 '14 at 20:52
  • it doesn't add a blank line. It only ends the last line with - as every other line. What's your problem with this? – Stephan May 22 '14 at 16:57
  • Are you satisfied by the given answer? If so, please mark it as accepted (see http://stackoverflow.com/help/someone-answers) or leave a comment to tell us what is wrong. – xav May 22 '14 at 17:23

1 Answers1

3

Here it is:

echo @echo off> start.bat
echo cd %USERPROFILE%\Desktop\BukkitServer>> start.bat
echo|set /p="java -Xmx1024M -jar craftbukkit.jar -o true">> start.bat

I have simply replaced the last echo with echo|set /p= to avoid the new line at the end.

Or you could also use, for the last line:

<nul set /p =java -Xmx1024M -jar craftbukkit.jar -o true>> start.bat

You will get the same result here but it seems to be more robust for other cases (see https://stackoverflow.com/a/7105690/1149528)

Community
  • 1
  • 1
xav
  • 5,452
  • 7
  • 48
  • 57
  • Why this downvote? Doesn't this code work? I've tested it and it works for me (Win7)... – xav May 21 '14 at 20:08
  • works for me on my MBP - and with no comments I'm balancing out the down vote output: `@echo off cd %USERPROFILE%DesktopBukkitServer` – pherris May 21 '14 at 20:10
  • It replaces the new line with a space when I do this. – leo60228 May 21 '14 at 20:19
  • @snuggles08 You're right, I have fixed my answer: you should use quotes (") around your last line: `/p="....">>` – xav May 21 '14 at 20:22
  • @snuggles08 Are you satisfied by the given answer? If so, please mark it as accepted (see http://stackoverflow.com/help/someone-answers) or leave a comment to tell us what is wrong. – xav May 22 '14 at 17:23
  • @xav Done! (insert random filler text here so I can post) – leo60228 May 23 '14 at 22:26