Need help debugging a code chunk

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Haxs4Life
Posts: 13
Joined: 02 Jan 2015 23:43
Contact:

Need help debugging a code chunk

#1 Post by Haxs4Life » 03 Jan 2015 00:04

Alright so I am making a batch RPG game and at the beginning I want it to say Created by: such and such, ya know? I made this with ASCII and Uni-coding so I couldn't just put it in echo, so I used the TYPE command and typed it into DOS from the .txt file, now I know this works because I've already tested this on its own separately. Here's my problem, i'll post the code below and basically post a walk-through of what I actually want the code to be doing below that.

Code:

    cd "%userprofile%\documents\Ogre Slayer"
    if exits "Type Files" goto FileTransfer
    if not exist "Type Files" goto FileInput
    :FileInput
    md "Type Files"
    :FileTransfer
    cd "%userprofile%\documents\Ogre Slayer\Type Files"
    if exists "Haxs4Life.txt" goto TxtInput
    if not exists "Haxs4Life.txt" goto TxtTransfer
    :TxtTransfer
    cd "%userprofile%\desktop\Ogre Slayer\Files"
    if exists "Files" goto TxtTransfer1
    if not exists "Files" goto TxtTransfer2
    :TxtTransfer1
    move /-y "%userprofile%\desktop\Ogre Slayer\Files\Haxs4Life.txt""C:\Users\Austin\Documents\Ogre Slayer\Type Files"
    goto TxtInput
    :TxtTransfer2
    cd "%userprofile%\downloads\Ogre Slayer"
    if exists "Ogre Slayer" goto TxtTransfer3
    if not exists "Ogre Slayer" goto TransferError
    :TxtTransfer3
    move /-y "%userprofile%\downloads\Ogre Slayer\Files\Haxs4Life.txt""C:\Users\Austin\Documents\Ogre Slayer\Type Files"
    goto TxtInput
    :TransferError
    @echo off
    cls
    color c4
    echo Warning! Warning! Warning! Warning!
    Echo An error has been detected!
    echo Please read the READ ME!! file in your downloaded file for more info!
    echo.
    echo Closing game now!...
    pause >nul
    exit
    :TxtInput
    cd "%userprofile%\documents\Ogre Slayer\Type Files"
    type Haxs4Life.txt
    echo.
    pause >nul

Step by Step breakdown process of what is supposed to be going on.

    1.) So I want the bat file to look in the Ogre Slayer folder in documents first
    2.) Its going to look for a folder labeled Type Files, if it exists it will go to the FileTransfer process.
    3.) If it does not exist it will go to FileInput
    4.) At FileInput it should be creating the Type Files folder in the Ogre Slayer folder located in documents.
    5.) At FileTransfer it will look for the .txt file labeled Haxs4Life.txt in the Type Files folder.
    6.) If its located it will go to TxtInput(which works), if its not found it will go to the TxtTransfer Process.
    7.) At the TxtTransfer process it will look for a folder called Files within the Ogre Slayer folder located on the Desktop instead.
    8.) If it exists it will go to TxtTransfer1, if not then it just does the same thing at TxtTransfer2 just with the download folder instead.
    9.) At TxtTransfer1 i want it to move a .txt file from the specified path to the other, which I don't think is setup correctly here.
    10.) From there it finally goes to TxtInput which i know works fine, if all else fails it goes to the error process also, which works fine.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Need help debugging a code chunk

#2 Post by foxidrive » 03 Jan 2015 03:05

It's more useful to add how it fails and also the error messages.

But on a very brief glance there needs to be a space between the two ""

Code: Select all

\Haxs4Life.txt""C:\Users\

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: Need help debugging a code chunk

#3 Post by miskox » 03 Jan 2015 03:44

Haxs4Life wrote:if exits "Type Files" goto FileTransfer
if exists "Haxs4Life.txt" goto TxtInput
if not exists "Haxs4Life.txt" goto TxtTransfer


You have: EXITs, exists, exists (should be exist everywhere). Start with that.

Saso

Haxs4Life
Posts: 13
Joined: 02 Jan 2015 23:43
Contact:

Re: Need help debugging a code chunk

#4 Post by Haxs4Life » 03 Jan 2015 13:50

foxidrive wrote:It's more useful to add how it fails and also the error messages.

But on a very brief glance there needs to be a space between the two ""

Code: Select all

\Haxs4Life.txt""C:\Users\


Ok I seperated the quotes on both move commands and all it does is just close the batch file right when i get to this part of the code, the error it flashes on screen right before it closes is: Type "Files" was unexpected at this time

miskox wrote:
Haxs4Life wrote:if exits "Type Files" goto FileTransfer
if exists "Haxs4Life.txt" goto TxtInput
if not exists "Haxs4Life.txt" goto TxtTransfer


You have: EXITs, exists, exists (should be exist everywhere). Start with that.

Saso

Thanks! I didn't realize that, that was probably a problem XD

Its still doing the same thing though sadly :/

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Need help debugging a code chunk

#5 Post by foxidrive » 03 Jan 2015 13:57

Haxs4Life wrote:Its still doing the same thing though sadly :/


If you have changed the code then paste the new version into a reply.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Need help debugging a code chunk

#6 Post by Squashman » 03 Jan 2015 14:14

Definitely see some flawed logic in some of your code. You are checking if FILES exists in FILES. Is there really a file named FILES in the folder named FILES?

Code: Select all

:TxtTransfer
cd "%userprofile%\desktop\Ogre Slayer\Files"
if exist "Files" goto TxtTransfer1
if not exist "Files" goto TxtTransfer2


You do the same thing for Ogre Slayer. Is there a file named Oger Slayer in the Oger Slayer folder?

Code: Select all

:TxtTransfer2
cd "%userprofile%\downloads\Ogre Slayer"
if exist "Ogre Slayer" goto TxtTransfer3
if not exist "Ogre Slayer" goto TransferError


Not sure why you use the goto command when the label is the next line of code.

Some of your code could be rewritten so that it flows better and is more readable.

Haxs4Life
Posts: 13
Joined: 02 Jan 2015 23:43
Contact:

Re: Need help debugging a code chunk

#7 Post by Haxs4Life » 04 Jan 2015 01:40

Squashman wrote:Definitely see some flawed logic in some of your code. You are checking if FILES exists in FILES. Is there really a file named FILES in the folder named FILES?

Code: Select all

:TxtTransfer
cd "%userprofile%\desktop\Ogre Slayer\Files"
if exist "Files" goto TxtTransfer1
if not exist "Files" goto TxtTransfer2


You do the same thing for Ogre Slayer. Is there a file named Oger Slayer in the Oger Slayer folder?

Code: Select all

:TxtTransfer2
cd "%userprofile%\downloads\Ogre Slayer"
if exist "Ogre Slayer" goto TxtTransfer3
if not exist "Ogre Slayer" goto TransferError


Not sure why you use the goto command when the label is the next line of code.

Some of your code could be rewritten so that it flows better and is more readable.

Ya sorry about that, i wrote this code in a few hurried minutes so its a bit sloppy. Ill go through and make it more smooth later on, im just trying to get it working so i can move on to working on the actual RPG again xP

foxidrive wrote:
Haxs4Life wrote:Its still doing the same thing though sadly :/


If you have changed the code then paste the new version into a reply.

Anyways, here is my new code:

Code: Select all

:Intro
Title Ogre Slayer                                                Created By: Haxs4Life
color 06
cls
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo                            ----------------------------
echo.
echo                             An Original RPG Batch Game
echo.
echo                              Created and Designed By
echo.
echo                            ----------------------------
echo.
pause
cls
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
cd "%userprofile%\documents\Ogre Slayer"
if exists "Type Files" goto FileTransfer
if not exist "Type Files" goto FileInput

:FileInput
cd "%userprofile%\documents\Ogre Slayer"
md "Type Files"

:FileTransfer
cd "%userprofile%\documents\Ogre Slayer\Type Files"
if exists "Haxs4Life.txt" goto TxtInput
if not exists "Haxs4Life.txt" goto TxtTransfer

:TxtTransfer
cd "%userprofile%\desktop\Ogre Slayer\Files"
if exists "Haxs4Life.txt" goto TxtTransfer1
if not exists "Haxs4Life.txt" goto TxtTransfer2

:TxtTransfer1
move /-y "%userprofile%\desktop\Ogre Slayer\Files\Haxs4Life.txt" "C:\Users\Austin\Documents\Ogre Slayer\Type Files"
goto TxtInput

:TxtTransfer2
cd "%userprofile%\downloads\Ogre Slayer\Files"
if exists "Haxs4Life.txt" goto TxtTransfer3
if not exists "Haxs4Life.txt" goto TransferError

:TxtTransfer3
move /-y "%userprofile%\downloads\Ogre Slayer\Files\Haxs4Life.txt" "C:\Users\Austin\Documents\Ogre Slayer\Type Files"
goto TxtInput

:TransferError
@echo off
cls
title ERROR ERROR ERROR ERROR ERROR ERROR ERROR
color c4
echo.
echo ERROR: The Haxs4Life.txt file cannot be found!
echo.
echo Please read the READ ME!!.txt file in the Ogre Slayer folder for more info!
echo.
echo Closing game now!...
ping -n 3 localhost
exit

:TxtInput
cd "%userprofile%\documents\Ogre Slayer\Type Files"
type Haxs4Life.txt
echo.
pause >nul


Its a little better, but still same error: "Type Files" was unexpected at this time"

Since its an error dealing with "Type Files" im pretty sure its something wrong with the first part right here:
cd "%userprofile%\documents\Ogre Slayer"
if exists "Type Files" goto FileTransfer
if not exist "Type Files" goto FileInput

But im not sure whats wrong with it though :/

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Need help debugging a code chunk

#8 Post by Compo » 04 Jan 2015 06:25

Here's a quick untested re-write

Code: Select all

@If Not Exist "%UserProfile%\Documents\Ogre Slayer\" Exit/B
@Echo Off & SetLocal EnableExtensions DisableDelayedExpansion
Title Ogre Slayer Created By: DosTips Community
Color 06
Cls
Echo(&Echo(&Echo(&Echo(&Echo(&Echo(&Echo(&Echo(&Echo(--------------------------
Echo(&Echo(An Original RPG Batch Game&Echo(&Echo(Created and Designed By&Echo(
Echo(--------------------------&Echo(&Pause
Cls
Echo(&Echo(&Echo(&Echo(&Echo(&Echo(&Echo(&Echo(

Set "UPD=%UserProfile%\D"
Set "O_S=\Ogre Slayer\"
Set "FHL=Files\Haxs4Life.txt"
If Exist "%UPD%ocuments%O_S%Type %FHL%" GoTo :TxtInput
If Not Exist "%UPD%esktop%O_S%%FHL%" (
   If Not Exist "%UPD%ownloads%O_S%%FHL%" (GoTo :TransferError) Else (
      GoTo :TxtTransfer2))

:TxtTransfer1
If Not Exist "%UPD%ocuments%O_S%Type Files\" MD "%UPD%ocuments%O_S%Type Files"
Move "%UPD%esktop%O_S%%FHL%" "%UPD%ocuments%O_S%Type Files"
GoTo :TxtInput

:TxtTransfer2
If Not Exist "%UPD%ocuments%O_S%Type Files\" MD "%UPD%ocuments%O_S%Type Files"
Move "%UPD%ownloads%O_S%%FHL%" "%UPD%ocuments%O_S%Type Files"
GoTo :TxtInput

:TransferError
Title ERROR ERROR ERROR ERROR ERROR ERROR ERROR
Color C4
Cls
Echo(&Echo(ERROR: The Haxs4Life.txt file cannot be found!&Echo(
Echo(Please read the READ ME!!.txt file in the Ogre Slayer folder for more info
Echo(&Echo(Closing game now!...
Ping -n 3 127.0.0.1 1>Nul
Exit/B

:TxtInput
Type "%UPD%ocuments%O_S%Type %FHL%"
Echo(&Pause>Nul
Does it help?

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Need help debugging a code chunk

#9 Post by Squashman » 04 Jan 2015 10:15

Haxs4Life wrote:Ya sorry about that, i wrote this code in a few hurried minutes so its a bit sloppy. Ill go through and make it more smooth later on, im just trying to get it working so i can move on to working on the actual RPG again xP

It is more than sloppy. It is just plain lazy. You haven't even bothered to fix any of the problems we have already found for you. We don't come here to waste time. We come here to help people learn and we get satisfaction of the end result. If you are just going to waste everyone's time then don't bother posting any more code. Please USE CODE TAGS when posting code.

Haxs4Life
Posts: 13
Joined: 02 Jan 2015 23:43
Contact:

Re: Need help debugging a code chunk

#10 Post by Haxs4Life » 04 Jan 2015 10:50

Compo wrote:Here's a quick untested re-write

Code: Select all

@If Not Exist "%UserProfile%\Documents\Ogre Slayer\" Exit/B
@Echo Off & SetLocal EnableExtensions DisableDelayedExpansion
Title Ogre Slayer Created By: DosTips Community
Color 06
Cls
Echo(&Echo(&Echo(&Echo(&Echo(&Echo(&Echo(&Echo(&Echo(--------------------------
Echo(&Echo(An Original RPG Batch Game&Echo(&Echo(Created and Designed By&Echo(
Echo(--------------------------&Echo(&Pause
Cls
Echo(&Echo(&Echo(&Echo(&Echo(&Echo(&Echo(&Echo(

Set "UPD=%UserProfile%\D"
Set "O_S=\Ogre Slayer\"
Set "FHL=Files\Haxs4Life.txt"
If Exist "%UPD%ocuments%O_S%Type %FHL%" GoTo :TxtInput
If Not Exist "%UPD%esktop%O_S%%FHL%" (
   If Not Exist "%UPD%ownloads%O_S%%FHL%" (GoTo :TransferError) Else (
      GoTo :TxtTransfer2))

:TxtTransfer1
If Not Exist "%UPD%ocuments%O_S%Type Files\" MD "%UPD%ocuments%O_S%Type Files"
Move "%UPD%esktop%O_S%%FHL%" "%UPD%ocuments%O_S%Type Files"
GoTo :TxtInput

:TxtTransfer2
If Not Exist "%UPD%ocuments%O_S%Type Files\" MD "%UPD%ocuments%O_S%Type Files"
Move "%UPD%ownloads%O_S%%FHL%" "%UPD%ocuments%O_S%Type Files"
GoTo :TxtInput

:TransferError
Title ERROR ERROR ERROR ERROR ERROR ERROR ERROR
Color C4
Cls
Echo(&Echo(ERROR: The Haxs4Life.txt file cannot be found!&Echo(
Echo(Please read the READ ME!!.txt file in the Ogre Slayer folder for more info
Echo(&Echo(Closing game now!...
Ping -n 3 127.0.0.1 1>Nul
Exit/B

:TxtInput
Type "%UPD%ocuments%O_S%Type %FHL%"
Echo(&Pause>Nul
Does it help?

With a few changes this works perfect! Thank you so much!
Squashman wrote:
Haxs4Life wrote:Ya sorry about that, i wrote this code in a few hurried minutes so its a bit sloppy. Ill go through and make it more smooth later on, im just trying to get it working so i can move on to working on the actual RPG again xP

It is more than sloppy. It is just plain lazy. You haven't even bothered to fix any of the problems we have already found for you. We don't come here to waste time. We come here to help people learn and we get satisfaction of the end result. If you are just going to waste everyone's time then don't bother posting any more code. Please USE CODE TAGS when posting code.

WOOW..I asked for help not some dick nagging. I am a new beginning coder so you don't have to talk crap about my code! I'm sorry i'm not some perfect coder as you bud. I'm asking this so I can learn how to get better at coding. If your going to just make disrespectful comments about my code then just shut up! I already know its bad, I don't need to hear crap about it, I need to know how to make it better instead of how its so bad! -.-
Sorry for ranting...but people like THIS make me mad :/

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Need help debugging a code chunk

#11 Post by Squashman » 04 Jan 2015 11:46

We did tell you how to make it better and you totally ignored our suggestions with your 2nd code post. If you were in our position what would you say? How can you learn if you just ignore our suggestions.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Need help debugging a code chunk

#12 Post by Compo » 04 Jan 2015 11:56

Haxs4Life wrote:With a few changes this works perfect! Thank you so much!

Since my script didn't make any changes from your original intent, (other than one Title), would you mind posting your changes!

As you say you are hoping to learn, here is the breakdown of how I went about the task:
  • What is the task - typing a text file called Haxs4Life.txt
  • Does the file to be typed exist in the required location
    • Yes - go to the typing of the file
    • No - Does the file exist in either of the two other locations
      • No - go to error message
      • Yes - Move the file from said location to required location, (create required location if it doesn't exist), and type it

Haxs4Life
Posts: 13
Joined: 02 Jan 2015 23:43
Contact:

Re: Need help debugging a code chunk

#13 Post by Haxs4Life » 04 Jan 2015 14:54

Squashman wrote:We did tell you how to make it better and you totally ignored our suggestions with your 2nd code post. If you were in our position what would you say? How can you learn if you just ignore our suggestions.

@Squashman , i did make the changes you guys suggested. I separated my commas and said what the error message said and how it appeared, stated by foxidrive. I changed EXITS to EXISTS, stated by miskox. And I reposted the changed code, asked by foxidrive. I also changed the mistakes you brought up to me, i changed the file directories since i was looking for "Files" in the folder "Files" that was a huge problem and thank you very much for helping me with that...But i did change all those mistakes, so how about looking through my code and seeing that i did make these changes before rambling of crap about my code :/

Compo wrote:
Haxs4Life wrote:With a few changes this works perfect! Thank you so much!

Since my script didn't make any changes from your original intent, (other than one Title), would you mind posting your changes!

As you say you are hoping to learn, here is the breakdown of how I went about the task:
  • What is the task - typing a text file called Haxs4Life.txt
  • Does the file to be typed exist in the required location
    • Yes - go to the typing of the file
    • No - Does the file exist in either of the two other locations
      • No - go to error message
      • Yes - Move the file from said location to required location, (create required location if it doesn't exist), and type it

Sorry I didn't really mean to say changes @Compo , I didn't change your code I just added a little bit more in to make the text flash in and out a few times before continuing on :P (The text typed in with the TYPE command)

Thanks everyone for helping me out, code is debugged and working perfectly. Forum closed :P

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Need help debugging a code chunk

#14 Post by Squashman » 04 Jan 2015 16:55

Please look at what miskox posted and then look at the 2nd set of code you posted. Maybe that will clear up the confusion.

Post Reply