Batch File Problems

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Ranguna173
Posts: 104
Joined: 28 Jul 2011 17:32

Batch File Problems

#1 Post by Ranguna173 » 08 Jan 2013 12:35

Hey Guys.

So I'm making this logger batch which save a log everytime it opens and a part of the code seems to be wierd..
Here's the code:

Code: Select all

set av=false
set avv=false
if %av%==false (
if %avv%==true goto a
if exist E:\authorize (
::cls
set pw=false
set pwc=false
echo What's the password ?
set /p pw=
cd E:\authorize
< pw (
  set /p pwc=
  )
pause
if %pw%==%pwc% (
cd C:\Users\Admin\AppData\Roaming\WCMP\logs
< location (
  set /p loglocation=
  )
cd %loglocation%
< log.txt (
  set /p timedayll=
  )
pause
del log.txt
echo %timedayll%>log.txt
echo Access Authorized>>log.txt
)
)
set avv=false
)


Every directory exists and (I think) all variables are set too.

I don't know what's going on with this but the cmd window is always crashing with this output

Code: Select all

C:\>code.bat

C:\>set av=false

C:\>set avv=false
A sintaxe do comando está incorrecta (what is bettewn the "()" is not part of the output, that sentence probably means "The command's Sintax is incorrect").

C:\>if == (

C:\>


Could anyone please help me in this, I really don't understand what's happening.

If you need any other information about the code don't hesitate to ask.

Thanks for reading :)

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

Re: Batch File Problems

#2 Post by foxidrive » 08 Jan 2013 18:13

Here's the code:
[code]set av=false
set avv=false
if %av%==false (
if %avv%==true goto a
if exist E:\authorize (
::cls

You can't have a label within parenthesis because it generates an error.
Thus using :: also generates an error.


set pw=false
set pwc=false
echo What's the password ?
set /p pw=
cd E:\authorize

< pw (
set /p pwc=
)

Your code in the three lines above is screwy/wrong/impossible

pause
if %pw%==%pwc% (
cd C:\Users\Admin\AppData\Roaming\WCMP\logs

< location (
set /p loglocation=
)

Ditto, your code in the three lines above is screwy/wrong/impossible

cd %loglocation%
< log.txt (
set /p timedayll=
)

pause
del log.txt
echo %timedayll%>log.txt
echo Access Authorized>>log.txt
)
)
set avv=false
)

I don't think the parentheses are all matched either.

Ranguna173
Posts: 104
Joined: 28 Jul 2011 17:32

Re: Batch File Problems

#3 Post by Ranguna173 » 09 Jan 2013 08:33

Here's the code:
[code]
...


::cls

You can't have a label within parenthesis because it generates an error.
Thus using :: also generates an error.

Hahaha, :: isn't recognised as a lable it's a commentary command xD
CMD will ignore anything that is after ::
Even if you ware right, I removed the :: and the batch still didn't work

...

< pw (
set /p pwc=
)

Your code in the three lines above is screwy/wrong/impossible

You clearly have heard of getting lines from a file.. "pw" is a file, not a varieble or anything else, it's a file in E:\authorize
Go ahead, try the code, make a file with some letters on the first line and save it as anything you want, go to cmd and type that code in which "pw" is the name of the saved file and "pwc" is the variable containing the letters of the first line.

< location (
set /p loglocation=
)

Ditto, your code in the three lines above is screwy/wrong/impossible

Same thing as I said before.


...

...

)
)
set avv=false
)

I don't think the parentheses are all matched either.
The parentheses are all matched, three parentheses are left open in the code which are now closed.

Thanks for the reply but it looks like what you said isn't my problem.

Does anyone knows why my code isn't working, if so please help me.
Thanks again :)

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Batch File Problems

#4 Post by Ed Dyreen » 09 Jan 2013 08:54

foxidrive wrote:Here's the code:
set av=false
set avv=false
if %av%==false (
if %avv%==true goto a
if exist E:\authorize (
::cls
set pw=false
You can't have a label within parenthesis because it generates an error.
Thus using :: also generates an error.
Yes you can :D

Code: Select all

(
:: does not generates an error.
pause
)
(
: does not generates an error.
pause
)
ed

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

Re: Batch File Problems

#5 Post by Squashman » 09 Jan 2013 09:20

Just wondering why you would write the code like this.

Code: Select all

< pw (
set /p pwc=
)

That just seems slightly unreadable.

Why not simplify it.

Code: Select all

set /p pwc=<pw.txt


I guess everyone has their own unique way of writing scripts. But I try to write scripts so that the person who has to back me up understands the code.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Batch File Problems

#6 Post by Ed Dyreen » 09 Jan 2013 10:39

'
Corrected some syntax, if it still don't work you are dealing with semantic errors.

Code: Select all

@echo off &setlocal enableDelayedExpansion                      %= require delayed or for =%

set  "av=false"
set "avv=false"
if /i !av! == false (                                           %= case insensitive =%
     ::
     if /i !avv! == true goto :a                                %= case insensitive, XP compatible =%

     if exist "E:\authorize" (                                  %= style =%

          ::cls
          set    "pw=false"                                     %= style =%
          set /p "pw=What's the password ?:"                    %= style =%

          cd /D "E:\authorize"                                  %= require /D, style =%

          set         "pwc=false"                               %= style =%
          < pw set /p "pwc="                                    %= style =%
          pause

          if /i !pw! == !pwc! (                                 %= case insensitive, require delayed or for =%
               ::
               cd /D "C:\Users\Admin\AppData\Roaming\WCMP\logs" %= require /D, style =%

               < location set /p "loglocation="                 %= style =%
               cd /D "!loglocation!"                            %= require /D, style =%

               < log.txt set /p "timedayll="                    %= style =%
               pause

               del log.txt

               >log.txt (                                       %= style =%

                    (echo.!timedayll!)                          %= style, require delayed or for =%
                    (echo.Access Authorized)                    %= style =%
               )
          )

          set "avv=false"                                       %= style =%
     )
)

:a ()                                                           %= require label =%
pause
exit
Hope it helps,

ed

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

Re: Batch File Problems

#7 Post by Squashman » 09 Jan 2013 10:45

Ed, just beat me to it! Was adding the delayed expansion! Thanks for the indenting ED. That is much more readable.

This doesn't look like the whole batch file the OP is using either as they have a GOTO statement to a non-existent label.

Ranguna173
Posts: 104
Joined: 28 Jul 2011 17:32

Re: Batch File Problems

#8 Post by Ranguna173 » 09 Jan 2013 11:39

Squashman wrote:Just wondering why you would write the code like this.

Code: Select all

< pw (
set /p pwc=
)

That just seems slightly unreadable.

Why not simplify it.

Code: Select all

set /p pwc=<pw.txt


I guess everyone has their own unique way of writing scripts. But I try to write scripts so that the person who has to back me up understands the code.


I code it like that because I'm used to it and with my code you can divede the file into lines:

Code: Select all

< pw (
set /p line1=
set /p line2=
set /p line3=
...
)


From what I can see, in yours you put the whole file into a variable.
But the main reason is just because I'm used to it :P

Squashman wrote:Ed, just beat me to it! Was adding the delayed expansion! Thanks for the indenting ED. That is much more readable.

This doesn't look like the whole batch file the OP is using either as they have a GOTO statement to a non-existent label.

Yeah this is just part of the code as I said in the main post:
Ranguna173 wrote:Hey Guys.

So I'm making this logger batch which save a log everytime it opens and a part of the code seems to be wierd..
Here's the code:

Code: Select all


...



...

Code: Select all


...



...


The a lable exist and it's right at the biginning of the batch after the variable sets.
Puting up the whole code would take a lot of space for just that little part, 3557 letters and 215 line of old-school coding that would be a pain in the ass to read xD but if you want the whole code you just have to ask, I don't see the use of it now because my problem has already been fixed.

Thanks for the replys Squashman :D

Ed Dyreen wrote:'
Corrected some syntax, if it still don't work you are dealing with semantic errors.

Code: Select all


...

Hope it helps,

ed


It works :D :D
Thank you very much for replying, I still don't know what was happening before, I don't see what changed in terms of commands, you just coded your own batch on your own style based on mine not-working one and it worked lol.

Thanks again.
You helped me a lot :)


Thanks for everyone that replyed, my problem has been fixed and I don't need any further assistance in this batch. (for now)
Thanks.

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

Re: Batch File Problems

#9 Post by Squashman » 09 Jan 2013 11:50

Yes your set statement can stream to multiple lines but mine only does one line. It does not put the whole file into a variable. Not sure why you would even think that based on you already knowing you can stream multiple lines inside a code block.

Ed's code adds some fundamental changes that makes the batch file work. The Delayed Expansion is required for your batch file to work correctly.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Batch File Problems

#10 Post by Ed Dyreen » 09 Jan 2013 11:59

Ranguna173 wrote:From what I can see, in yours you put the whole file into a variable.
Just the first line.
Ranguna173 wrote:Thank you very much for replying, I still don't know what was happening before, I don't see what changed in terms of commands, you just coded your own batch on your own style based on mine not-working one and it worked lol.
Glad you got it working, took your batch, indented it, corrected syntax errors %= with comments =% to what changed.

Kind regards, :wink:

Ranguna173
Posts: 104
Joined: 28 Jul 2011 17:32

Re: Batch File Problems

#11 Post by Ranguna173 » 09 Jan 2013 12:04

Squashman wrote:Yes your set statement can stream to multiple lines but mine only does one line. It does not put the whole file into a variable. Not sure why you would even think that based on you already knowing you can stream multiple lines inside a code block.

Ed's code adds some fundamental changes that makes the batch file work. The Delayed Expansion is required for your batch file to work correctly.


hummm.. Could you rephrase bold text for me, I didn't get it. :P

Ed Dyreen wrote:
Ranguna173 wrote:From what I can see, in yours you put the whole file into a variable.
Just the first line :)

Squashman, already told me that in the previous post :)

Ed Dyreen wrote:
Ranguna173 wrote:
...

Glad you got it working, took your batch, indented it, corrected syntax errors %= with comments =% to what changed.

Kind regards, :wink:


Oh %=...=% are comments, I didn't know that, thank you

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Batch File Problems

#12 Post by abc0502 » 09 Jan 2013 12:34

This comment methode i didn't know about it too,
it's great, it works with the echo command too :o

can it be extended to comment in several lines like in C/C++ or in python ? :?:

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Batch File Problems

#13 Post by Ed Dyreen » 09 Jan 2013 13:28

abc0502 wrote:This comment methode i didn't know about it too,
it's great, it works with the echo command too :o

can it be extended to comment in several lines like in C/C++ or in python ? :?:
Thanks jeb, dave and tooComplex :D

Code: Select all

@echo off

echo.hello world ! &::%= ^
_^
Multiline comments are supported. ^
_^
Special characters work.<>;:%=* :) ^
_^
Be carefull with double quotes though, they need to be even ! "" ^
_^
Apart from a trailing caret, empty lines need at least one char to not break the multiline. ^
_^
=%

echo.hello world ! &::%= Double quotes" do not pose a problem .<>;:%=* in a single line though :) =%

pause
exit
&:: Usually not required but allows the use of nasty chars that would otherwise break the code.

The rules are too complex, I document some; :)
http://www.dostips.com/forum/viewtopic.php?p=8869#p8869
http://www.dostips.com/forum/viewtopic.php?p=18481#p18481

ed

Tags: comment, comments, multiline, multi line, multi-line

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Batch File Problems

#14 Post by abc0502 » 09 Jan 2013 14:11

thanks a lot Ed Dyreen, that was helpful, i was almost there :)
echo test %= comment 1 ^
comment 2 =%

it wasn't working but using the &:: didn't come a cross my mind :lol:
thanks :)

Post Reply