Dinty
Dinty

Reputation: 642

Windows batch file command creates file but does not write to it

I am creating a messenger with batch files and I am trying to store a default color setting. I have stored other settings before and have had no problem but this time it creates the file but leaves it blank.

Here is an example:

echo Enter the colour you want...
set /p defaultcolor=
if "%defaultcolor%" == "default" @echo 7> "\\user-computer\users\user\documents\codes+stuff\messenger\userdata\%username%\default colour.dll"
if "%defaultcolor%" == "blue" @echo 1> "\\user-computer\users\user\documents\codes+stuff\messenger\userdata\%username%\default colour.dll"
if "%defaultcolor%" == "green" @echo 2> "\\user-computer\users\user\documents\codes+stuff\messenger\userdata\%username%\default colour.dll"
if "%defaultcolor%" == "aqua" @echo 3> "\\user-computer\users\user\documents\codes+stuff\messenger\userdata\%username%\default colour.dll"

If anybody can spot the problem I will greatly appreciate it.

Upvotes: 0

Views: 75

Answers (4)

Stephan
Stephan

Reputation: 56180

some other methods:

echo 5 >file.txt 

Note: there is a space after the 5, which is written to the file too.

>file.txt echo 5

Note: most robust solution imho.

(echo 5)>file.txt

Upvotes: 2

Compo
Compo

Reputation: 38623

You could probably shorten it a little:

Set/P "defaultcolor=Enter the colour you want... "
(
    If /I "%defaultcolor%"=="default" Echo 7
    If /I "%defaultcolor%"=="blue" Echo 1
    If /I "%defaultcolor%"=="green" Echo 2
    If /I "%defaultcolor%"=="aqua" Echo 3
)>"\\user-computer\users\user\documents\codes+stuff\messenger\userdata\%username%\default colour.dll"

Note that .dll files don't usually contain standard text.

Upvotes: 3

Stavr00
Stavr00

Reputation: 3314

You need a space between your number and the redirect symbol.

@ECHO 2> file

is interpreted as

Redirect the STDERR output of ECHO to file

@ECHO 2 > file

is interpreted as

Write 2 to file

Note you can also use the caret escape as follows

echo Enter the colour you want...
set /p defaultcolor=
if "%defaultcolor%" == "default" @echo ^7> "\\user-computer\users\user\documents\codes+stuff\messenger\userdata\%username%\default colour.dll"
if "%defaultcolor%" == "blue" @echo ^1> "\\user-computer\users\user\documents\codes+stuff\messenger\userdata\%username%\default colour.dll"
if "%defaultcolor%" == "green" @echo ^2> "\\user-computer\users\user\documents\codes+stuff\messenger\userdata\%username%\default colour.dll"
if "%defaultcolor%" == "aqua" @echo ^3> "\\user-computer\users\user\documents\codes+stuff\messenger\userdata\%username%\default colour.dll"

Upvotes: 3

Harry
Harry

Reputation: 1263

remove the @ in front of your echos and put a space before the >

Upvotes: 1

Related Questions