Inserting comment within a broken line - is it possible? [WORKAROUND]

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
DOSadnie
Posts: 143
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Inserting comment within a broken line - is it possible? [WORKAROUND]

#1 Post by DOSadnie » 02 Jul 2023 11:57

I have broke my A-OK working last line of a certain big script into

Code: Select all

robocopy "%sd%" "%dd%\%folder_name%" /E ^
 /XF "%sd%\desktop.ini"
and it still works

But if I try adding

Code: Select all

:: My comment
or

Code: Select all

REM My comment
in-between those two lines then the whole script breaks


So it there a way to comment between them, i.e. in a separate line, or it is just plan impossible?
Last edited by DOSadnie on 11 Jul 2023 05:28, edited 2 times in total.

OJBakker
Expert
Posts: 88
Joined: 12 Aug 2011 13:57

Re: Inserting comment within a broken line - is it possible?

#2 Post by OJBakker » 02 Jul 2023 14:12

Not a real comment but you can hide your comment in a non-existent variable followed by the line-continuation caret.
The my comment part must still be valid cmd syntax.

Code: Select all

@echo on
cls
dir c:\ ^
 %==my comment==% ^
/one

pause
An other option is to use a null character to hide your comment from cmd.
Everything on the line following a null character including the CRLF that ends the line will be completely ignored by cmd.
You will need an additional line-continuation caret at the line following your comment.

Code: Select all

@echo on
cls
dir c:\^
 X(replace X with character x"00") my comment or whatever you want to write in this line
^
 /one

pause

DOSadnie
Posts: 143
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Re: Inserting comment within a broken line - is it possible?

#3 Post by DOSadnie » 03 Jul 2023 03:43

These do seem like nice tricks. But such lines will not be clearly visible - thus do only half the job


With the first approach I could start such comment with something standing out like >>█<< sign and thus arriving at something like

Code: Select all

robocopy "%sd%" "%dd%\%folder_name%" /E ^
 %==███ MY COMMENT==% ^
 /XF "%sd%\desktop.ini"
However apparently I cannot use

Code: Select all

 %==███:: MY COMMENT==% ^
to make it more obvious to what it is as it breaks the script- so I will think if I prefer it over making e.g.

Code: Select all

::
::
::
as an intro to the line with the actual comment, which [when forced] will be placed over a block consisting of a broken line; because there is also a possibility that signs other than >>:<< in such line can brake the script


As for the second approach, this

Code: Select all

robocopy "%sd%" "%dd%\%folder_name%" /E ^
 MY COMMENT
^
 /XF "%sd%\desktop.ini"
renders my script half-workihg, because it only creates a backup of the folder structure omitting all of the files

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

Re: Inserting comment within a broken line - is it possible?

#4 Post by miskox » 03 Jul 2023 10:47

Maybe you could write comments like this:

Code: Select all

robocopy "%sd%" "%dd%\%folder_name%" /E /XF "%sd%\desktop.ini"
REM        |             |            |  |         |
REM        |             |            |  |         \- comment 1
REM        |             |            |  \----------- comment 2
REM        |             |            \-------------- comment 3
REM        |             \-- comment 4 (not right justifed)
REM        \-- comment 5 (not right justifed for example)
Saso

DOSadnie
Posts: 143
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Re: Inserting comment within a broken line - is it possible?

#5 Post by DOSadnie » 04 Jul 2023 03:42

Now that is a neat workaround - but also time consuming, the more elements from a single line a user wants to comment on

Thank you both for you help

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

Re: Inserting comment within a broken line - is it possible?

#6 Post by miskox » 04 Jul 2023 04:18

DOSadnie wrote:
03 Jul 2023 03:43
renders my script half-workihg, because it only creates a backup of the folder structure omitting all of the files
I think the reason why it will not work is this:

Code: Select all

some_command /a /b /c /d
will work. But if I add a comment:

Code: Select all

some_command /a /b^
REM some comment
/c /d
equals to:

Code: Select all

some_command /a /b REM some comment /c /d
some_command will see this new parameter as invalid.

Saso

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

Re: Inserting comment within a broken line - is it possible?

#7 Post by miskox » 04 Jul 2023 04:19

DOSadnie wrote:
04 Jul 2023 03:42
...but also time consuming...
I guess not because you usually do it only once.

Saso

LaverneReeves
Posts: 1
Joined: 05 Jul 2023 01:24

Re: Inserting comment within a broken line - is it possible?

#8 Post by LaverneReeves » 05 Jul 2023 01:27

In your case, when you add the comment line using either :: or REM, the caret is interpreted as an escape character, causing the script to break.

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

Re: Inserting comment within a broken line - is it possible?

#9 Post by miskox » 09 Jul 2023 22:56

Here is a test:

Code: Select all

@echo off
echo ---------- sort by extension
dir /b /oe
echo ---------- sort by extension does not work
dir /b
REM comment
/oe
echo ----------end

Code: Select all

c:\temp>test.cmd
---------- sort by extension
test.cmd
3.pdf
2.pdf
1.pdf
---------- sort by extension does not work
1.pdf
2.pdf
3.pdf
test.cmd
'/oe' is not recognized as an internal or external command,
operable program or batch file.
----------end
c:\temp>
And what could happen is that if your qualifier/parameter is a valid command it will run:

Code: Select all

some_program.exe these are parameters
and now let's insert a comment:

Code: Select all

some_program.exe these are^
REM comment
parameters
If there is a program named 'parameters' it will run.

Saso

Post Reply