Debugging / syntax checksing tools for batch

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Yanta
Posts: 46
Joined: 01 Sep 2019 07:08

Re: Debugging / syntax checksing tools for batch

#16 Post by Yanta » 16 May 2020 19:29

Eureka! wrote:
16 May 2020 06:10
Yanta wrote:
15 May 2020 23:52
Doesn't matter that I put there, it will always be the first non-remark after the label.
So, if you replace this line:

Code: Select all

IF /I NOT "%DoGame:~0,1%"=="Y" Echo %time% Not installing games >>C:\%USERDOMAIN%.PostInstall.Log 2>&1
with:

Code: Select all

echo 123
pause
Your script fails?
Absolutely. See above reply. It says "The syntax of the command is invalid", exactly at that line.

In fact, up until 2 days ago that line wasn't there. I added it above the next line when I was trying to prove that is the exact point at which it was failing.

btw: The purpose of the substring is to avoid the situations where the test fails because one or more spaces found their way into the end of the variable.
Last edited by Yanta on 16 May 2020 19:45, edited 4 times in total.

Yanta
Posts: 46
Joined: 01 Sep 2019 07:08

Re: Debugging / syntax checksing tools for batch

#17 Post by Yanta » 16 May 2020 19:38

ShadowThief wrote:
16 May 2020 06:53
I wrote a script to echo a number after each line in your script. After getting to

Code: Select all

if /I "%DoGame:~0,1%"=="Y" (
it wouldn't progress past that. Since I know that code in parentheses gets parsed as a single block, I removed the if statement entirely. The script then continued on until it reached the GZDoom installer section. Again, I removed the offending if statements, and then it continued until it reached the line

Code: Select all

IF /I "%Menu:~0,1%"=="N" (
. At that point, I tried setting the %Menu% and %DoQL% variables, and the script completed successfully.

Also, it didn't break anything here, but you will almost certainly run into issues by having labels inside of an if block like you have here.
Even if those labels are not referenced? ie. There are no "GOTO" to the labels within the if block?

What perplexes me is this worked perfectly 3 weeks ago. I added the following sections of code and it hasn't worked since. Took the new section of code out and it still breaks.

Code: Select all

Rem		PS3 Joystick Controller software
		IF EXIST "%SRC%\PostInstall\%USERNAME%\SCP\" (
			Echo %time% Copying PS3 controller software >>C:\%USERDOMAIN%.PostInstall.Log 2>&1
			xcopy "%SRC%\PostInstall\%USERNAME%\SCP" "%PROGRAMFILES%\SCP" /e/q/y/c/k/i/h/x >>C:\%USERDOMAIN%.PostInstall.Log 2>&1
		)

Code: Select all

:L220	
Rem	*********************************************************************************************************************************************
Rem	* If drive archives exist, restore them																										*
Rem	*********************************************************************************************************************************************	
	IF EXIST "D:\" IF EXIST "%SRC%\PostInstall\%USERNAME%\DDrive.rar" (
		Echo Restoring D:\ drive contents
		Echo %time% Restoring D:\ Drive contents >>C:\%USERDOMAIN%.PostInstall.Log 2>&1
		%SRC%\PostInstall\unrar x -y -iddp "%SRC%\PostInstall\%USERNAME%\DDrive.rar" D:\ >>C:\%USERDOMAIN%.PostInstall.Log 2>&1
	)
	IF EXIST "E:\" IF EXIST "%SRC%\PostInstall\%USERNAME%\EDrive.rar" (
		Echo Restoring e:\ drive contents
		Echo %time% Restoring H:\ Drive contents >>C:\%USERDOMAIN%.PostInstall.Log 2>&1
		%SRC%\PostInstall\unrar x -y -iddp "%SRC%\PostInstall\%USERNAME%\EDrive.rar" E:\ >>C:\%USERDOMAIN%.PostInstall.Log 2>&1
	)
	IF EXIST "F:\" IF EXIST "%SRC%\PostInstall\%USERNAME%\FDrive.rar" (
		Echo Restoring F:\ drive contents
		Echo %time% Restoring H:\ Drive contents >>C:\%USERDOMAIN%.PostInstall.Log 2>&1
		%SRC%\PostInstall\unrar x -y -iddp "%SRC%\PostInstall\%USERNAME%\FDrive.rar" F:\ >>C:\%USERDOMAIN%.PostInstall.Log 2>&1
	)
	IF EXIST "G:\" IF EXIST "%SRC%\PostInstall\%USERNAME%\GDrive.rar" (
		Echo Restoring G:\ drive contents
		Echo %time% Restoring G:\ Drive contents >>C:\%USERDOMAIN%.PostInstall.Log 2>&1
		%SRC%\PostInstall\unrar x -y -iddp "%SRC%\PostInstall\%USERNAME%\GDrive.rar" G:\ >>C:\%USERDOMAIN%.PostInstall.Log 2>&1
	)
	IF EXIST "H:\" IF EXIST "%SRC%\PostInstall\%USERNAME%\HDrive.rar" (
		Echo Restoring H:\ drive contents
		Echo %time% Restoring H:\ Drive contents >>C:\%USERDOMAIN%.PostInstall.Log 2>&1
		%SRC%\PostInstall\unrar x -y -iddp "%SRC%\PostInstall\%USERNAME%\HDrive.rar" H:\ >>C:\%USERDOMAIN%.PostInstall.Log 2>&1
	)
Anyway, I removed the labels. They were not referenced anywhere, only used to document the script (I have a readme.txt which says what each label refers to).

It didn't change anything.

You say you removed "offending" if statements. Surely nested if's are supported? I know they are because as I said, three weeks ago this script was about 30 lines shorter and it worked perfectly.

The top of the script also writes all variables to the log file so I can check they are not null. There are no issues with undefined variables that I can find.

Syntactically, these are equivalent

Code: Select all

IF "%XYZ"=="Y" (
    IF %ABC%"==" (
        ...
    )
)

Code: Select all

IF "%XYZ%"=="Y" IF "%ABC%=="Y" (
    ...
)
EDIT: I just went through the entire script and there was only one other label within an if block, and it also was not referenced anywhere. Removed it, reran and same results.

Yanta
Posts: 46
Joined: 01 Sep 2019 07:08

Re: Debugging / syntax checksing tools for batch

#18 Post by Yanta » 16 May 2020 21:08

I've just eliminated every GOTO in the script except the one to push the script to the end if it's not run from valid installation media.
That means adding more compound IF statements.
Ran it again.
Fails in EXACTLY the same place. The first non-remark statement after :L510.

ShadowThief
Expert
Posts: 986
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Debugging / syntax checksing tools for batch

#19 Post by ShadowThief » 16 May 2020 22:01

No, I mean I removed any if statements that impeded my progress in debugging. Literally the only issue I could find was that the variables weren't set. There's nothing wrong with nested if statements.

Yanta
Posts: 46
Joined: 01 Sep 2019 07:08

Re: Debugging / syntax checksing tools for batch

#20 Post by Yanta » 17 May 2020 03:57

ShadowThief wrote:
16 May 2020 22:01
No, I mean I removed any if statements that impeded my progress in debugging. Literally the only issue I could find was that the variables weren't set. There's nothing wrong with nested if statements.
All good. There actually weren't any variables not set.
I do actually have a not defined section at the top. The problem was I only posted a tiny section of the code.

Anyway I HAVE FOUND THE PROBLEM.

Many moons ago I purchased a program called Surething Disk Labeller. This developers released a patch for the version I am using. I've never bothered to upgrade to the latest version because of the cost - the existing version suits my needs. Why is this relevant?

This is the section of code..

Code: Select all

Echo %time% Installing SureThing Disk Labeller v7 >>C:\%USERDOMAIN%.PostInstall.Log 2>&1
		"%SRC%\PostInstall\%USERNAME%\SureThing\stdl7gold-enu.exe" /SP- /VERYSILENT /NOICONS /NORESTART /LOG="%SRC%\PostInstall\Logs\Surething.log"
		IF EXIST "%PROGRAMFILES(X86)%\SureThing Disc Labeler 7\stdl.exe" IF EXIST "%SRC%\PostInstall\%USERNAME%\SureThing\stdl7goldpatch-enu.exe" (
			Echo %time% Patching SureThing Disk Labeller v7 >>C:\%USERDOMAIN%.PostInstall.Log 2>&1
			"%SRC%\PostInstall\%USERNAME%\SureThing\stdl7goldpatch-enu.exe" /SP- /VERYSILENT /NOICONS /NORESTART /LOG="%SRC%\PostInstall\Logs\Stdlpatch.log"
		)
stdl7goldpatch-enu.exe was crashing with error 0x3, and issue with registering a DLL. After reading some loosely related posts on duckduckgo I found that a common thread was VC Runtime 2010 was not installed. Now, one would think if a program was dependent on a VC runtime it would install it, but since I'm doing silent installs perhaps I'm bypassing that logic somehow. Or maybe, the dependency changed between STDL7 and the patch being released.

Whatever the case, the DLL registration error seemed to be corrupting things big time. For some unknown reason when it got past the remarks after :L510 it just bricked.

So, I moved the installation of SureThing to after VC 2010 runtime was installed and viola! It works.

I know, it's bizarre and potentially unbelievable, but truthfully, that is exactly what happened.

Now, the undefined... The script sets variables at the start for each user, as per previous post. It then sets some global defaults, then checks if a bunch of variables are undefined, and does exactly as suggested, sets them to whatever is needed.

Code: Select all

Rem	Check if everything is set. Set to defaults if not
	IF NOT DEFINED DoQL set "DoQL=Y"
	IF NOT DEFINED Menu set "Menu=Y"
	IF NOT DEFINED DoGPO set "DoGPO=Y"
	IF NOT DEFINED pFile set "pFile=D:"
	IF NOT DEFINED KeyPC set "KeyPC=N"
	IF NOT DEFINED DoGame set "DoGame=N"
	IF NOT DEFINED DoApps set "DoApps=Y"
	IF NOT DEFINED DoKodi set "DoKodi=N"
	IF NOT DEFINED DoCool set "DoCool=Y"	
	IF NOT DEFINED FFoxESR set "FFoxESR=Y"	
	IF NOT DEFINED FireFox set "FireFox=%APPDATA%\Mozilla\FireFox\Profiles"
	IF NOT DEFINED DoSteam set "DoSteam=N"
	IF NOT DEFINED DataDrv set "DataDrv=C:"
	IF NOT DEFINED LogPath set "LogPath=None"
	IF NOT DEFINED OpenVPN set "OpenVPN=N"
	IF NOT DEFINED Macrium set "Macrium=Y"
	IF NOT DEFINED TempPath set "TempPath=None"
	IF NOT DEFINED UserPath set "UserPath=%USERPROFILE%"	
	IF NOT DEFINED SteamDrive set "SteamDrive=D:"
Last edited by Yanta on 17 May 2020 04:18, edited 2 times in total.

Eureka!
Posts: 102
Joined: 25 Jul 2019 18:25

Re: Debugging / syntax checksing tools for batch

#21 Post by Eureka! » 17 May 2020 04:06

EDIT:

Never mind; you solved it already ..
Last edited by Eureka! on 17 May 2020 04:26, edited 2 times in total.

Yanta
Posts: 46
Joined: 01 Sep 2019 07:08

Re: Debugging / syntax checksing tools for batch

#22 Post by Yanta » 17 May 2020 04:16

The parenthesis was easy. I don't use ( or ) in comments if I can avoid it. So using notepad++ I counted the number of ( and ). 810 each, Now 819 each doe to removal of GOTOs
Quotes are a little more difficult, so I just kept reading the script from start to end over and over, looking for " mismatches.
Both of those were my first thoughts too :)

I errr... neglected to make a backup of the script before I moved the install of Surething. I'll try and put it back where it was and try as you suggest.

I gather that 123 is literally what you want, not Echo 123.

Yanta
Posts: 46
Joined: 01 Sep 2019 07:08

Re: Debugging / syntax checking tools for batch

#23 Post by Yanta » 14 Sep 2020 05:23

Just thought I'd update on this.
My script is now 8,000 lines and still growing.
I've found a free easy to use IDE that includes a debugger from https://sourceforge.net/projects/batch-compiler/
Does lots more than just syntax check and debug scripts, but they are the only features I need atm.
Since I've been using it I haven't had any issues with unbalanced (,) or "

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

Re: Debugging / syntax checksing tools for batch

#24 Post by Compo » 14 Sep 2020 08:44

Based upon the initial code, I've seen, i.e. that posted here , there are several noteworthy things I spotted.

Your biggest issue, is that you're using labels inside your parenthesised blocks.

Additionally
  • "%SRC%\PostInstall\Firefox Setup 76.0.1" should probably have an extension.
  • IF NOT EXIST "%APPDATA%\Mozilla" and the line beneath it IF NOT EXIST "%APPDATA%\Mozilla\FireFox", should both use trailing backslashes, as they're directories, not files.
  • IF EXIST "%SRC%\PostIntall\%USERNAME%\Quick Launch\Forefox.lnk" ( has a spelling error, which is matched on the line beneath it copy "%SRC%\PostIntall\%USERNAME%\Quick Launch\Forefox.lnk".
  • Start "Windows 7 Games" /d "%SRC%\PostInstall\" "W7GW10.exe" should really read, Start "Windows 7 Games" /d "%SRC%\PostInstall" "%SRC%\PostInstall\W7GW10.exe"
  • "%SRC%\PostInstall\%USERNAME%\RuneScape-Setup" /s should really read "%SRC%\PostInstall\%USERNAME%\RuneScape-Setup.exe" /s
  • copy "%SRC%\PostInstall\%USERNAME%\jagex_cl_runescape_LIVE.dat" "%USERPROFILE%" and copy "%SRC%\PostInstall\%USERNAME%\jagexappletviewer.preferences" "%USERPROFILE%" unlike all of your other
  • copy commands, do not use the /y option!
  • You need to fix your quoting on your last REG ADD command, /d "C:\Users\Phillip\AppData\Local\HearthstoneDeckTracker\Update.exe" --processStart "HearthstoneDeckTracker.exe", nested doublequotes require escaping.
Yanta wrote:
17 May 2020 04:16
The parenthesis was easy. I don't use ( or ) in comments if I can avoid it.
But you used them here, Rem Microsoft Games folder (Some games go in local Eg W7G4W10, some go in roaming Eg Rise of nations), which I'd recommend that you replace or remove

Yanta
Posts: 46
Joined: 01 Sep 2019 07:08

Re: Debugging / syntax checking tools for batch

#25 Post by Yanta » 17 Sep 2020 04:29

Indeed. Both of those were pointed out some time ago. And I removed them all a long time ago. It's possible I missed something, but they don't cause the debugger to fail or warn so it's easy to miss them in a long script. I shall check again.

EDIT: Nope. All labels that were within ( ) blocks had already been replaced with Rem :Lnnn so I can still search them.
May I ask some questions related to your suggestions, please?
  • I've added the exe after firefox installer.
  • Fixed missing backslashes on Mozilla and Firefox folders.
  • Fixed misspelling of Firefox.
  • All 50+ of my Start /wait's are of the format of Start /wait "title" /d "path" "command/program". What is the downside of doing it this way?
  • Not one of the exe files I run has the extension on them. You spotted that with the firefox install. I shall go back and fix that. There are about 100 of them. Is this just an etiquette thing? I am not having any problems.. Or is it to do with the old com/exe/bat issue (a com would be executed before the exe etc)...
  • Not sure if you are saying Copy doesn't support /y or that I just should not use it. Unfortunately, not using it often hangs up the script as the output is written to the log file, the prompt to overwrite is written to the log file. But I don't want the script prompting for overwrite anyway. You've put an exclamation on that so I gather you feel very strongly about that. Can you please explain why this is not to be used?
  • The auto start entry was removed from the script a long time ago. It's now a reg import, exactly as you have shown it, as that is how the HSDT installer creates it in the registry.
Thank you for the great feedback.

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

Re: Debugging / syntax checking tools for batch

#26 Post by Compo » 17 Sep 2020 05:49

Yanta wrote:
17 Sep 2020 04:29
Indeed. Both of those were pointed out some time ago. And I removed them all a long time ago. It's possible I missed something, but they don't cause the debugger to fail or warn so it's easy to miss them in a long script. I shall check again.

EDIT: Nope. All labels that were within ( ) blocks had already been replaced with Rem :Lnnn so I can still search them.
Without wishing to come over a argumentative, you posted some code here, it is the only example of that code, and I've formulated a response based upon it, there is no indication that those things were fixed. It would therefore help were you to provide the latest version of the code, if you wanted to get advice on changes you have, or have not, made, since your initial posting.
Yanta wrote:
17 Sep 2020 04:29
May I ask some questions related to your suggestions, please?
  • Not one of the exe files I run has the extension on them. You spotted that with the firefox install. I shall go back and fix that. There are about 100 of them. Is this just an etiquette thing? I am not having any problems.. Or is it to do with the old com/exe/bat issue (a com would be executed before the exe etc)...
I wouldn't call it an etiquette thing, but you should be aware that those files only run without an extension because each of the values listed under the easily modified, %PATHEXT% variable are appended to each in order, (COM before EXE, as you mentioned). So my point is that you shouldn't be relying upon that variable existing, and being unmodified.
Yanta wrote:
17 Sep 2020 04:29
  • Not sure if you are saying Copy doesn't support /y or that I just should not use it. Unfortunately, not using it often hangs up the script as the output is written to the log file, the prompt to overwrite is written to the log file. But I don't want the script prompting for overwrite anyway. You've put an exclamation on that so I gather you feel very strongly about that. Can you please explain why this is not to be used?
I was merely pointing out that all of your other entries included the overwrite `/Y` option, except for those. I didn't know whether or not you wanted to overwrite in those cases, and thought I'd mention it, as you may have forgotten to, as opposed to decided not to, include it.

Post Reply