0

Greeting people of stack overflow! I have lately been trying to learn how to program using batch files and I have been running into problems with some of my tests. I am very new so excuse me if I make rookie mistakes, because, well, I am a rookie. I am trying to make a program that gets the user input and writes it to a text file. This is all fine and dandy until I need the user to enter a single number, no other letters, to a text file. I have a small, reproductable example here:

@echo off
rem saved in C:\Users\<username>\OneDrive\Desktop\Test\Test.bat
echo Enter a number
set /p num="Number: "
echo %num%>> Data.txt

However, when I enter a number, it doesn't write to the text file, however, if I type a string, it does.

aschipfl
  • 33,626
  • 12
  • 54
  • 99
Char Gamer
  • 41
  • 7
  • 1
    It thinks the number is a file stream you are trying to write to. Switch the redirection around. `>>Data.txt echo %num%` – Squashman Mar 24 '20 at 23:04
  • You are issuing a different command to what you think you are.`1>> data.txt` says to redirect filehandle 1 to data.txt, not the number 1. Only the digit before `>>` matters as filehandles are 0 to 9. 3 to 9 are undefined. __The answer is to put a space between any number and `>>`__. See https://winsourcecode.blogspot.com/2019/12/command-prompt-cheat-sheet.html for punctuation at the command prompt. –  Mar 24 '20 at 23:05
  • 1
    @Mark if you put a space after the number it will then include that space at the end of the line in the file. That may be undesirable. – Squashman Mar 24 '20 at 23:07
  • 1
    Your other option is to use parentheses: `(echo %num%)>>Data.txt`. Either of the two ways I have described will work fine. – Squashman Mar 24 '20 at 23:20
  • Thank you all I got it working now! – Char Gamer Mar 24 '20 at 23:44

0 Answers0