error writing to file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
chronogod
Posts: 5
Joined: 29 Apr 2017 13:27

error writing to file

#1 Post by chronogod » 29 Apr 2017 13:35

Basically what I'm doing is making a demo game using batch.

What's happening is that instead of writing the %char% to lets say "b3.txt" it will write it to "b3" as a plain file. Could someone help explain why this is happening and what I can do to sove this?

title charreg
set /p char=<char.txt
set /p cx=<cx.txt
set /p cy=<cy.txt

if %cy% == 0 set fy=-
if %cy% == 1 set fy=a
if %cy% == 2 set fy=b
if %cy% == 3 set fy=c
if %cy% == 4 set fy=d
if %cy% == 5 set fy=e
if %cy% == 6 set fy=f

@echo %char% >%fy%%cx%.txt

penpen
Expert
Posts: 1997
Joined: 23 Jun 2013 06:15
Location: Germany

Re: error writing to file

#2 Post by penpen » 29 Apr 2017 18:38

You didn't provide the content of the files ("char.txt",) "cx.txt" and "cy.txt"; but my guess is, that the first line is (without doublequotes) is:
- "cx.txt": "2"
- "cy.txt": "3 "

You could check this by just removing the '@' character from the last line, and execute the resulting batch within an cmd window.


Beside this, you may use extensions (although activated on most systems by default), and use doublequotes around filenames, set assignments (and possibly for if comparators), and put redirections in front of the command, which is the recommended way:

Code: Select all

setlocal enableExtensions
title charreg
<"char.txt" set /p "char="
<"cx.txt" set /p "cx="
<"cy.txt" set /p "cy="

if "%cy%" == "0" set "fy=-"
if "%cy%" == "1" set "fy=a"
if "%cy%" == "2" set "fy=b"
if "%cy%" == "3" set "fy=c"
if "%cy%" == "4" set "fy=d"
if "%cy%" == "5" set "fy=e"
if "%cy%" == "6" set "fy=f"

>"%fy%%cx%.txt" echo(%char%

Beside this, please use [code][/code] tags around your sources in your posts:
This looks nicer, and it makes it easier to copy the source by using "Select all".


penpen

chronogod
Posts: 5
Joined: 29 Apr 2017 13:27

Re: error writing to file

#3 Post by chronogod » 30 Apr 2017 08:18

I think that the code's working but I got another problem now.

I think that the problem is that I can't use a space (in movementlog) when writing characters to cx.txt and cy.txt. I'm pretty sure this is the issue because the charreg is writing %char% to 3.txt or b.txt depending on which direction I move.

Bellow are the 6 programs used for the game at the moment.

If you need to replicate the sequence, make a char.txt and put "i" in it

Controller

Code: Select all

@echo off
title Controller
goto title

:title
echo use wasd to move, n for confirm, m to deny, k for menu,
echo change controller color by typing in red, blue, black,
echo or green
timeout /t 5 >nul
goto main

:main
cls
echo  w
echo asd    k
echo      nm
set /p button=

if %button% == a goto left
if %button% == s goto down
if %button% == w goto up
if %button% == d goto right
if %button% == n goto confirm
if %button% == m goto deny
if %button% == k goto start
if %button% == red color cf & goto main
if %button% == blue color 9f & goto main
if %button% == green color af & goto main
if %button% == black color 0f & goto main
if not %button% == '' echo not a control & timeout /t 2 >nul & goto main
goto main

:left
@echo 1 >control.txt
goto main

:down
@echo 2 >control.txt
goto main

:up
@echo 3 >control.txt
goto main

:right
@echo 4 >control.txt
goto main

:confirm
@echo 5 >control.txt
goto main

:deny
@echo 6 >control.txt
goto main

:start
@echo 7 >control.txt
goto main


screen

Code: Select all

@echo off
title screen
cls
goto main

:condtime
timeout /t 1 >nul
set /p update=<update.txt
if %update% == 0 goto main
if %update% == 1 goto condtime
if not %update% == '' echo UPDATE VALUE ERROR & timeout /t 5 >nul & goto condtime

:main
set /p a1=<a1.txt
set /p a2=<a2.txt
set /p a3=<a3.txt
set /p a4=<a4.txt
set /p a5=<a5.txt

set /p b1=<b1.txt
set /p b2=<b2.txt
set /p b3=<b3.txt
set /p b4=<b4.txt
set /p b5=<b5.txt

set /p c1=<c1.txt
set /p c2=<c2.txt
set /p c3=<c3.txt
set /p c4=<c4.txt
set /p c5=<c5.txt

set /p d1=<d1.txt
set /p d2=<d2.txt
set /p d3=<d3.txt
set /p d4=<d4.txt
set /p d5=<d5.txt

set /p e1=<e1.txt
set /p e2=<e2.txt
set /p e3=<e3.txt
set /p e4=<e4.txt
set /p e5=<e5.txt

set /p back=<backcol.txt
set /p col=<col.txt

goto screen

:screen
cls
color %back%%col%
echo %a1%%a2%%a3%%a4%%a5%
echo %b1%%b2%%b3%%b4%%b5%
echo %c1%%c2%%c3%%c4%%c5%
echo %d1%%d2%%d3%%d4%%d5%
echo %e1%%e2%%e3%%e4%%e5%
@echo 1 >update.txt
goto condtime


movementlog

Code: Select all

@echo off
title movementlog
:main
set /p control=<control.txt
if %control% == 0 goto null
if %control% == 1 goto left
if %control% == 2 goto up
if %control% == 3 goto down
if %control% == 4 goto right

:null
timeout /t 1 >nul
goto main

:left
set /p cx=<cx.txt
set /a x=%cx%
set /a xn=%x% - 1
set /a cx=%xn%
@echo %cx% >cx.txt
@echo 0 >control.txt
goto main

:up
set /p cy=<cy.txt
set /a y=%cy%
set /a yn=%y% - 1
set /a cy=%yn%
@echo %cy% >cy.txt
@echo 0 >control.txt
goto main

:down
set /p cy=<cy.txt
set /a y=%cy%
set /a yn=%y% + 1
set /a cy=%yn%
@echo %cy% >cy.txt
@echo 0 >control.txt
goto main

:right
set /p cx=<cx.txt
set /a x=%cx%
set /a xn=%x% + 1
set /a cx=%xn%
@echo %cx% >cx.txt
@echo 0 >control.txt
goto main


mapassembler

Code: Select all

@echo off
:main
title mapassembler
timeout /t 1 >nul
set /p work=<update.txt
set /p map=<mapvalue.txt

if %work% == 0 goto remain
if %work% == 1 goto fix

:remain
goto main

:fix
call m%map%.bat
call charreg.bat
@echo 0 >update.txt
goto main


map 0

Code: Select all

@echo 0 >mapvalue.txt
@echo 0 >backcol.txt
@echo f >col.txt
@echo . >a1.txt
@echo . >a2.txt
@echo . >a3.txt
@echo . >a4.txt
@echo . >a5.txt
@echo . >b1.txt
@echo w >b2.txt
@echo . >b3.txt
@echo . >b4.txt
@echo . >b5.txt
@echo . >c1.txt
@echo . >c2.txt
@echo . >c3.txt
@echo w >c4.txt
@echo . >c5.txt
@echo . >d1.txt
@echo . >d2.txt
@echo w >d3.txt
@echo . >d4.txt
@echo . >d5.txt
@echo . >e1.txt
@echo . >e2.txt
@echo . >e3.txt
@echo . >e4.txt
@echo w >e5.txt


charreg

Code: Select all

setlocal enableExtensions
title charreg
<"char.txt" set /p "char="
<"cx.txt" set /p "cx="
<"cy.txt" set /p "cy="

if "%cy%" == "0" set "fy=-"
if "%cy%" == "1" set "fy=a"
if "%cy%" == "2" set "fy=b"
if "%cy%" == "3" set "fy=c"
if "%cy%" == "4" set "fy=d"
if "%cy%" == "5" set "fy=e"
if "%cy%" == "6" set "fy=f"

>"%fy%%cx%.txt" echo %char%

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: error writing to file

#4 Post by Aacini » 30 Apr 2017 08:48

Perhaps this suggestion may seems complex, but I don't see any reason to not use the tools that Batch files provide to solve problems.

You want to map a number in cy variable into a letter in fy one. However, the mapping is a very simple and direct one. As a matter of fact, the mapping consist in take one character from "-abcdef" string accordingly to the position given by cy variable, that precisely starts at zero. This way, you just need to first expand the value of %cy% variable and then use such a value to extract one character from the string in a second expansion that must happen after the first one (that is, a "Delayed Expansion"). The Delayed Expansion is achieved enclosing a variable in exclamation marks instead of percent signs, but such a feature must be first enabled in a SETLOCAL command:

Code: Select all

@echo off
title charreg
setlocal EnableDelayedExpansion

set /p "char=" < "char.txt"
set /p "cx=" < "cx.txt"
set /p "cy=" < "cy.txt"

set "string=-abcdef"

set "fy=!string:~%cy%,1!"

> "%fy%%cx%.txt" echo %char%

You may even eliminate the fy auxiliary variable and create the output file in just one line:

Code: Select all

> "!string:~%cy%,1!%cx%.txt" echo %char%

Antonio

chronogod
Posts: 5
Joined: 29 Apr 2017 13:27

Re: error writing to file

#5 Post by chronogod » 01 May 2017 21:12

alright, I got everything working. Thanks for the help guys

Post Reply