Invisible error, deleting a comment - makes everything work

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
BoQsc
Posts: 92
Joined: 30 Jun 2014 04:10

Invisible error, deleting a comment - makes everything work

#1 Post by BoQsc » 18 Mar 2018 11:56

Update:
  • removing only batch comment mentioned below - does not fix the code
  • removing the line and the comment mentioned below - does fix the code



Now by removing this comment, the code suddenly starts to work again.

Code: Select all

	::Hex code or GUID (L to show codes, Enter = 700):
Deleting this comment leads to a working code.
What kind of mistery is that? I have had such "invisible errors" in various places today.
And these invsible errors appeared in a code, which I coded in a completely new batch file, completely from sratch.
(however I can't note that here, as I didn't take it seriously before, again, just by removing some empty lines and that code started working again)








fdiskGuide.cmd

Code: Select all

@ECHO OFF & cd %~dp0
SETLOCAL EnableDelayedExpansion	


::Required to be run as administrator

:GainInformationFromDiskPart
	set "previousSelection=!currentSelection!"

cls

	::Always run with administrator privilegies.
	::Withouh administrator privilegies, diskpart relaunch in a seperate commandLine window.
	
	::Capture "DiskPart list disk" output using FOR loop
	echo Physical Drives connected:
	FOR /F "USEBACKQ tokens=1,2,3,4,5,6,7 skip=9" %%a IN (`
		
		^( echo list disk ^) ^| diskpart 
		
		
	`) DO (
			set "outputLine=%%a"
			set "diskSpace=0"
			IF not [!outputLine!]==[DISKPART^>] (
				echo  !outputLine! %%b ^| %%d%%e !selection[%%b]!
				IF [!selection[%%b]!]==[^<--] (
					set "selectedDiskCapacity=%%d%%e" & TITLE [%~nx0] Selected Physical Drive -^> %currentSelection% - !selectedDiskCapacity!
					echo                  [Repeat to confirm.]
				)
			)
			

			
			)   && 	if not [%%b]==[] set /A "countedOptions=%%b"
			
			::Reset previous selection, if it is out of bound.
			
			IF !currentSelection! GTR %countedOptions% (
				set "selection[!previousSelection!]="
				set "previousSelection="
		)
	

	
	::Clear previous selection
	set "selection[!previousSelection!]="
	
	::Choosing PhysicalDrive
	choice /C 0123456789 /M "Enter PhysicalDrive Number: "
		set /A "errorLevelOutput=%errorlevel% -1"

	::Set new selection  
	set "currentSelection=!errorLevelOutput!"
	set "selection[!errorLevelOutput!]=<--"
	IF [!previousSelection!]==[!currentSelection!] (
		cls
		
		call :partitionToolGdisk !currentSelection!
	)
	


	
call :GainInformationFromDiskPart !currentSelection!



:partitionToolGdisk
set "currentSelection=%~1"


::Physical drive examples 
::  \\.\physicaldrive0
::  \\.\physicaldrive1
::  \\.\physicaldrive2
::  1:
::  2:
::  3:

set "physicalDrive=!currentSelection!:"


::Execute commands inside gdisk32.exe, 
::on the selected physical drive.

(

	::create a new empty GUID partition table (GPT)
	echo o
	::This option deletes all partitions and creates a new protective MBR. Proceed? (Y/N)
	echo Y
	
	::add a new partition
	echo n
	::Partition number (1-128, default 1):
	echo.
	::First sector (34-15155166, default = 2048) or {+-}size{KMGTP}:
	echo.
	::Last sector (2048-15155166, default = 15155166) or {+-}size{KMGTP}:
	echo ^+50M
	::Current type is 'Microsoft basic data'
	::Hex code or GUID (L to show codes, Enter = 700):
	echo 700

	echo w
	echo Y
	
	

	
	
) | gdisk32.exe %physicalDrive%
echo.
pause
exit


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

Re: Invisible error, deleting a comment - makes everything work

#2 Post by Squashman » 18 Mar 2018 12:12

You are inside a code block. Using the double colons for comments will have issues with that. I would advise you use REM instead. There is a work around for using the double colon for comments inside a code block but I can't remember what it is.

pieh-ejdsch
Posts: 239
Joined: 04 Mar 2014 11:14
Location: germany

Re: Invisible error, deleting a comment - makes everything work

#3 Post by pieh-ejdsch » 18 Mar 2018 13:36

You can use this

Code: Select all

:: define LF as a Line Feed (newline) character
set ^"LF=^

^" Above empty line is required - do not remove
:: define a newline with line continuation
set ^"\n=^^^%LF%%LF%^%LF%%LF%^^"
set \=::%\n%
 :_
 %\%comment 1
 (%\%comment2
 echo ...
 )

BoQsc
Posts: 92
Joined: 30 Jun 2014 04:10

Re: Invisible error, deleting a comment - makes everything work

#4 Post by BoQsc » 18 Mar 2018 13:52

Squashman wrote:
18 Mar 2018 12:12
You are inside a code block. Using the double colons for comments will have issues with that. I would advise you use REM instead. There is a work around for using the double colon for comments inside a code block but I can't remember what it is.

I tried to use REM, still nothing works until I delete whole line .

REM Example that do not work, the line still crashes everything in a code block.

Code: Select all

	REM Hex code or GUID (L to show codes, Enter = 700):
Also I tried to use REM other way, without the uncommon symbols:

Code: Select all

REM sfjskdfjsdghjkfjffsjflak
Everything still crashes until I completely remove whole line.



Excerpt from Original code block - the same as the first post (for quick inspection)

Code: Select all

::Execute commands inside gdisk32.exe, 
::on the selected physical drive.

(

	::create a new empty GUID partition table (GPT)
	echo o
	::This option deletes all partitions and creates a new protective MBR. Proceed? (Y/N)
	echo Y
	
	::add a new partition
	echo n
	::Partition number (1-128, default 1):
	echo.
	::First sector (34-15155166, default = 2048) or {+-}size{KMGTP}:
	echo.
	::Last sector (2048-15155166, default = 15155166) or {+-}size{KMGTP}:
	echo ^+50M
	::Current type is 'Microsoft basic data'
	REM Hex code or GUID (L to show codes, Enter = 700):
	echo 700

	echo w
	echo Y
	
	

	
	
) | gdisk32.exe %physicalDrive%







pieh-ejdsch wrote:
18 Mar 2018 13:36
You can use this

Code: Select all

:: define LF as a Line Feed (newline) character
set ^"LF=^

^" Above empty line is required - do not remove
:: define a newline with line continuation
set ^"\n=^^^%LF%%LF%^%LF%%LF%^^"
set \=::%\n%
 :_
 %\%comment 1
 (%\%comment2
 echo ...
 )

Sorry, but i'm not even checking this code if it works, as it looks horrible and unecessary complicated.

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

Re: Invisible error, deleting a comment - makes everything work

#5 Post by Squashman » 18 Mar 2018 14:12

So you think you don't have to use REM in place of all the double colons?

BoQsc
Posts: 92
Joined: 30 Jun 2014 04:10

Re: Invisible error, deleting a comment - makes everything work

#6 Post by BoQsc » 18 Mar 2018 14:20

Squashman wrote:
18 Mar 2018 14:12
So you think you don't have to use REM in place of all the double colons?
Well I though that if the problem is with that only line - replacing it with REM could at least work .

However I have just tested and replaced every :: comment with a REM, but it's Still the same crash.

I keep wondering that maybe something that is inside comments (unexpected symbols ) that leads to a crash until I delete the whole line completely.

Code: Select all

@ECHO OFF & cd %~dp0
SETLOCAL EnableDelayedExpansion	


rem Required to be run as administrator

:GainInformationFromDiskPart
	set "previousSelection=!currentSelection!"

cls

	rem Always run with administrator privilegies.
	rem Withouh administrator privilegies, diskpart relaunch in a seperate commandLine window.
	
	rem Capture "DiskPart list disk" output using FOR loop
	echo Physical Drives connected:
	FOR /F "USEBACKQ tokens=1,2,3,4,5,6,7 skip=9" %%a IN (`
		
		^( echo list disk ^) ^| diskpart 
		
		
	`) DO (
			set "outputLine=%%a"
			set "diskSpace=0"
			IF not [!outputLine!]==[DISKPART^>] (
				echo  !outputLine! %%b ^| %%d%%e !selection[%%b]!
				IF [!selection[%%b]!]==[^<--] (
					set "selectedDiskCapacity=%%d%%e" & TITLE [%~nx0] Selected Physical Drive -^> %currentSelection% - !selectedDiskCapacity!
					echo                  [Repeat to confirm.]
				)
			)
			

			
			)   && 	if not [%%b]==[] set /A "countedOptions=%%b"
			
			rem Reset previous selection, if it is out of bound.
			
			IF !currentSelection! GTR %countedOptions% (
				set "selection[!previousSelection!]="
				set "previousSelection="
		)
	

	
	rem Clear previous selection
	set "selection[!previousSelection!]="
	
	rem Choosing PhysicalDrive
	choice /C 0123456789 /M "Enter PhysicalDrive Number: "
		set /A "errorLevelOutput=%errorlevel% -1"

	rem Set new selection  
	set "currentSelection=!errorLevelOutput!"
	set "selection[!errorLevelOutput!]=<--"
	IF [!previousSelection!]==[!currentSelection!] (
		cls
		
		call :partitionToolGdisk !currentSelection!
	)
	


	
call :GainInformationFromDiskPart !currentSelection!



:partitionToolGdisk
set "currentSelection=%~1"


rem Physical drive examples 
rem   \\.\physicaldrive0
rem   \\.\physicaldrive1
rem   \\.\physicaldrive2
rem   1:
rem   2:
rem   3:

set "physicalDrive=!currentSelection!:"


rem Execute commands inside gdisk32.exe, 
rem on the selected physical drive.

(

	rem create a new empty GUID partition table (GPT)
	echo o
	rem This option deletes all partitions and creates a new protective MBR. Proceed? (Y/N)
	echo Y
	
	rem add a new partition
	echo n
	rem Partition number (1-128, default 1):
	echo.
	rem First sector (34-15155166, default = 2048) or {+-}size{KMGTP}:
	echo.
	rem Last sector (2048-15155166, default = 15155166) or {+-}size{KMGTP}:
	echo ^+50M
	rem Current type is 'Microsoft basic data'
	rem Hex code or GUID (L to show codes, Enter = 700):
	echo 700

	echo w
	echo Y
	
	

	
	
) | gdisk32.exe %physicalDrive%
echo.
pause
exit

Last edited by BoQsc on 18 Mar 2018 14:24, edited 1 time in total.

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

Re: Invisible error, deleting a comment - makes everything work

#7 Post by Squashman » 18 Mar 2018 14:24

I find that hard to believe because I just tested that section of code with REM instead of double colon and it all worked for me.

BoQsc
Posts: 92
Joined: 30 Jun 2014 04:10

Re: Invisible error, deleting a comment - makes everything work

#8 Post by BoQsc » 18 Mar 2018 14:32

Squashman wrote:
18 Mar 2018 14:24
I find that hard to believe because I just tested that section of code with REM instead of double colon and it all worked for me.
I can assure, it does not work when running whole code. And I made every old comment into REM.
The code that was fully converted to comments was the whole code, the same that was inserted in the first post.
The code has no relationships with any other code. It is completely on his own except for one Partition Tool gdisk and diskpart.

I tried in a hope to rename executable .CMD to a .BAT extension from a despair. But nothing really giving me a working code except deleting that single line.

I'm thinking to very very carefully rewriting whole file, just to understand what is causing it, giving all the responsibility to a single empty line that leads everything to a crash.

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

Re: Invisible error, deleting a comment - makes everything work

#9 Post by Squashman » 18 Mar 2018 14:47

Well I am not a big fan of the syntax you are using for your FOR and IF commands.

Your if commands would be bullet proof if you enclosed your string comparisons with double quotes. You don't need to escape the <> if you do so. The quotes will protect spaces and special characters in the comparison.

Code: Select all

C:\Users\Squashman\Desktop>if "<>"=="<>" echo yes
yes

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Invisible error, deleting a comment - makes everything work

#10 Post by dbenham » 18 Mar 2018 15:24

You have multiple issues that create problems:
  • You are using :: comments within a parenthesized block. That doesn't work unless there is a valid command immediately after each comment, but you have 2 consecutive :: comments in one place, which cannot work.
  • Your comment has a ) in it. Normally this is not a problem within a parenthesized block, but the consecutive :: comments bring the ) into play, compounding the problem.
  • You are piping a parenthesized block, which is why you cannot substitute REM - see https://stackoverflow.com/q/8192318/1012053, and be sure to read all of jeb's answer. He explains why REM causes problems in this situation, as well as a possible solution.
I recommend using %= Comment =% style "remarks" within parenthesized block - they are "variables" that are guaranteed to expand into nothing :)
The one drawback is you cannot include % or : within variable "comments". So I removed all the :

Code: Select all

(

  %= create a new empty GUID partition table (GPT) =%
  echo o
  %= This option deletes all partitions and creates a new protective MBR. Proceed? (Y/N) =%
  echo Y
  
  %= add a new partition =%
  echo n
  %= Partition number (1-128, default 1) =%
  echo.
  %= First sector (34-15155166, default = 2048) or {+-}size{KMGTP} =%
  echo.
  %= Last sector (2048-15155166, default = 15155166) or {+-}size{KMGTP} =%
  echo ^+50M
  %= Current type is 'Microsoft basic data' =%
  %= Hex code or GUID (L to show codes, Enter = 700) =%
  echo 700

  echo w
  echo Y
  
) | gdisk32.exe %physicalDrive%
Dave Benham

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

Re: Invisible error, deleting a comment - makes everything work

#11 Post by penpen » 19 Mar 2018 05:04

Basing on your syntax - could it be that there is a missing ')'-character at the end of the following if command?
(Edit: The answer is "No." - the indents tricked me :oops: ; thanks to dave for seeing that.)

Code: Select all

IF !currentSelection! GTR %countedOptions% (
				set "selection[!previousSelection!]="
				set "previousSelection="
Although it is not recommended, you could use "::"-comments, if the next token is a valid command (as dave said).
If you have consecutive "::"-comments, then you could use the circumflex accent character ('^') to merge these lines; so that should also work:

Code: Select all

@echo off
setlocal EnableExtensions DisableDelayedExpansion
(

	::create a new empty GUID partition table (GPT)
	echo o
	::This option deletes all partitions and creates a new protective MBR. Proceed? (Y/N)
	echo Y
	
	::add a new partition
	echo n
	::Partition number (1-128, default 1):
	echo.
	::First sector (34-15155166, default = 2048) or {+-}size{KMGTP}:
	echo.
	::Last sector (2048-15155166, default = 15155166) or {+-}size{KMGTP}:
	echo ^+50M

	::Current type is 'Microsoft basic data'^
	::Hex code or GUID (L to show codes, Enter = 700):
	echo 700

	echo w
	echo Y
) | findstr "^"
goto :eof
penpen

Edit: Added an edit-note to the first paragraph.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Invisible error, deleting a comment - makes everything work

#12 Post by dbenham » 19 Mar 2018 07:53

penpen wrote:
19 Mar 2018 05:04
Basing on your syntax - could it be that there is a missing ')'-character at the end of the following if command?

Code: Select all

IF !currentSelection! GTR %countedOptions% (
				set "selection[!previousSelection!]="
				set "previousSelection="
Not when the OP tells us the code works perfectly if the :: comment is removed.
Also, I took the time to trace the parens, and despite the awkward indents, they all look balanced to me (assuming all comments are truly comments with trailing characters ignored).


penpen wrote:
19 Mar 2018 05:04
Although it is not recommended, you could use "::"-comments, if the next token is a valid command (as dave said).
There is more about use of labels within parens at viewtopic.php?f=3&t=3803&p=55405#p55405.


penpen wrote:
19 Mar 2018 05:04
If you have consecutive "::"-comments, then you could use the circumflex accent character ('^') to merge these lines; so that should also work:
Good idea - I like it :!: :D

At first I thought this would fail for the same reason that REM cannot be used within a piped parenthesized block. But then I realized that the :: comment is completely stripped from the command line that is sent to CMD /C.

Using REM fails

Code: Select all

(
  echo Line 1
  REM This fails because the REM hides the closing paren when packaged for CMD /C 
  echo Line 2
)  | findstr /n "^"
because the above becomes:

Code: Select all

C:\Windows\system32\cmd.exe  /S /D /c" ( echo Line 1 & REM This fails because the REM hides the closing paren when packaged for CMD /C & echo Line 2 )" | findstr /n "^"
But using :: works

Code: Select all

(
  echo Line 1
  :: This works because line continuation makes this comment one line ^
  :: and the entire comment is removed before packaging for CMD /C
  echo Line 2
) | findstr /n "^"
because the above becomes:

Code: Select all

C:\Windows\system32\cmd.exe  /S /D /c" ( echo Line 1 & echo Line 2 )" | findstr /n "^"

Dave Benham

pieh-ejdsch
Posts: 239
Joined: 04 Mar 2014 11:14
Location: germany

Re: Invisible error, deleting a comment - makes everything work

#13 Post by pieh-ejdsch » 19 Mar 2018 13:35

Well - thanks for these tips - then I have allowed myself to change this variable something.
It now also has both: simple one-line comments; bracketed comments and comments in parenthesis with pipe.
The follow-up command within parentheses must of course be included.

Code: Select all

 rem  create a variable to insert comment lines in the batch file,
 rem   which will be ignored when executing the batch
(set \=::^^^

 : )

 rem simple commentline 
%\% this & that is commented < NOT redirected

 rem comment within parenthesis
(
%\% read the line & print this )
echo testline 1
)

 rem comment and piped
(
%\% read the line & print, use pipe ( | ) into next command !
echo testline 2
) | find /n /v "" 

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

Re: Invisible error, deleting a comment - makes everything work

#14 Post by penpen » 19 Mar 2018 17:50

I nearly have forgotten, that the following rem-comment-style worked under Win XP (and also under my actual win10 64 bit - if i don't have made an error):

Code: Select all

@echo off
setlocal EnableExtensions EnableDelayedExpansion
set "ampersand=&"
(
	<nul set /p "=Hello"
	rem:Sample rem-comments.
	rem:No need to escape ampersand expansion: !ampersand!.
        rem:But percentage expansion results has to be escaped: ^^^%ampersand%.
        rem:Escaping two times: ^^^& ^^^| ^^^)
        rem:Or use doublequotes: "& | )"
        rem:No need to escape ([]{}^^=;'+,`~ and probably any other utf-character - but that is untested.
	echo  world^!
) | findstr "^"
goto :eof
penpen

Post Reply