Do not expand Comments

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Docfxit
Posts: 132
Joined: 12 Nov 2015 12:42

Do not expand Comments

#1 Post by Docfxit » 29 May 2024 20:36

I would like to show a comment that is not expanded and the expanded version of the command.
I have a batch file with commands:

Code: Select all

@Echo Off

Echo ^%^%~dp0^%^% = %~dp0
Echo %cd%
Echo %~d0
Echo %~p0
Echo %~fn0
Echo %~dpn0.txt
Echo %~nx0
Echo %~fs0
@cmd /k
I tried with the first line but it doesn't show the first %.
The output looks like:

Code: Select all

~dp0 = C:\Dnload\
C:\Dnload
C:
\Dnload\
C:\Dnload\FolderOptionsText.bat
C:\Dnload\FolderOptionsText.txt
FolderOptionsText.bat
C:\Dnload\FOLDER~1.BAT
C:\Dnload>
I'd like to comment all the lines in a similar manor.

Thank you,

shodan
Posts: 89
Joined: 01 May 2023 01:49

Re: Do not expand Comments

#2 Post by shodan » 30 May 2024 00:23

Like this ?

Code: Select all

C:\Users\user\BatchfileFrameWork\lib\experimental>variablecommenting.bat
%~dp0 = C:\Users\user\BatchfileFrameWork\lib\experimental\
%cd% = C:\Users\user\BatchfileFrameWork\lib\experimental
%~d0 = C:
%~p0 = \Users\user\BatchfileFrameWork\lib\experimental\
%~fn0 = C:\Users\user\BatchfileFrameWork\lib\experimental\variablecommenting.bat
%~dpn0.txt = C:\Users\user\BatchfileFrameWork\lib\experimental\variablecommenting.txt
%~nx0 = variablecommenting.bat
%~fs0 = C:\Users\user\BATCHF~1\lib\EXPERI~1\VARIAB~1.BAT
@cmd /k

Code: Select all

@Echo Off

Echo %%~dp0 = %~dp0
Echo %%cd%% = %cd%
Echo %%~d0 = %~d0
Echo %%~p0 = %~p0
Echo %%~fn0 = %~fn0
Echo %%~dpn0.txt = %~dpn0.txt
Echo %%~nx0 = %~nx0
Echo %%~fs0 = %~fs0
echo @cmd /k

IcarusLives
Posts: 175
Joined: 17 Jan 2016 23:55

Re: Do not expand Comments

#3 Post by IcarusLives » 30 May 2024 05:07

In batch scripting, comments are lines of text that are not executed as commands. They are used to explain and document the code, making it easier to understand and maintain. In batch scripts, comments can be created using the REM command.

Code: Select all

REM This is a comment
REM this is also a comment
echo Hello World

Docfxit
Posts: 132
Joined: 12 Nov 2015 12:42

Re: Do not expand Comments

#4 Post by Docfxit » 30 May 2024 09:55

shodan: That is really great. That is exactly what I wanted.
That's super.

Thank you very much...

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

Re: Do not expand Comments

#5 Post by Squashman » 30 May 2024 11:10

You were literally directed to read this website
https://www.robvanderwoude.com/escapechars.php
in your last thread from two months ago
viewtopic.php?f=3&t=11182&p=69806#p69801

Which literally shows you 4 lines down how to escape a percent symbol.
All DOS versions interpret certain characters before executing a command.
Some well know examples are the percent sign ( % ), and the redirection symbols ( < | > ).

Windows 95/98 and NT, and OS/2 too, also interpret double quotes ( " ) and ampersands ( & ), as shown in the Conditional Execution page.

In batch files, the percent sign may be "escaped" by using a double percent sign ( %% ).
That way, a single percent sign will be used as literal within the command line, instead of being further interpreted.
We also have it in the dostips library.
https://www.dostips.com/DtCodeSnippets. ... ets.Escape

shodan
Posts: 89
Joined: 01 May 2023 01:49

Re: Do not expand Comments

#6 Post by shodan » 30 May 2024 15:21

This sort of question is also easily answered by chatgpt

https://chatgpt.com/share/da842c3a-90e3 ... 6f2b367e66

Example AI discussion

USER

I would like to show a comment that is not expanded and the expanded version of the command.
I have a batch file with commands:

Code: Select all

@Echo Off

Echo ^%^%~dp0^%^% = %~dp0
Echo %cd%
Echo %~d0
Echo %~p0
Echo %~fn0
Echo %~dpn0.txt
Echo %~nx0
Echo %~fs0
@cmd /k
I tried with the first line but it doesn't show the first %.
The output looks like:

Code: Select all

~dp0 = C:\Dnload\
C:\Dnload
C:
\Dnload\
C:\Dnload\FolderOptionsText.bat
C:\Dnload\FolderOptionsText.txt
FolderOptionsText.bat
C:\Dnload\FOLDER~1.BAT
C:\Dnload>
I'd like to comment all the lines in a similar manor.

Thank you,

chatgpt

To show both the raw, unexpanded batch file commands and their expanded versions as comments, you can use REM to add comments next to each line. This way, you preserve the unexpanded commands for reference. Here is an example of how you can achieve this:

Code: Select all

@Echo Off

REM Echo ^%^%~dp0^%^% = %~dp0
Echo %%~dp0 = %~dp0
REM ^-- Outputs the drive and path of the batch file, e.g., C:\Dnload\

REM Echo %cd%
Echo %cd%
REM ^-- Outputs the current directory, e.g., C:\Dnload

REM Echo %~d0
Echo %~d0
REM ^-- Outputs the drive letter of the batch file, e.g., C:

REM Echo %~p0
Echo %~p0
REM ^-- Outputs the path of the batch file, e.g., \Dnload\

REM Echo %~fn0
Echo %~fn0
REM ^-- Outputs the fully qualified name of the batch file, e.g., C:\Dnload\FolderOptionsText.bat

REM Echo %~dpn0.txt
Echo %~dpn0.txt
REM ^-- Outputs the drive, path, and name of the batch file with a .txt extension, e.g., C:\Dnload\FolderOptionsText.txt

REM Echo %~nx0
Echo %~nx0
REM ^-- Outputs the name and extension of the batch file, e.g., FolderOptionsText.bat

REM Echo %~fs0
Echo %~fs0
REM ^-- Outputs the full path name in 8.3 format, e.g., C:\Dnload\FOLDER~1.BAT

@cmd /k
By using REM for comments, you can include the original, unexpanded batch file commands and describe their purpose or the expected output right next to them. This way, anyone reading the file can understand both the command and its effect.

USER

No I mean something like this

Code: Select all

C:\Users\user\BatchfileFrameWork\lib\experimental>variablecommenting.bat
%~dp0 = C:\Users\user\BatchfileFrameWork\lib\experimental\
%cd% = C:\Users\user\BatchfileFrameWork\lib\experimental
%~d0 = C:
%~p0 = \Users\user\BatchfileFrameWork\lib\experimental\
%~fn0 = C:\Users\user\BatchfileFrameWork\lib\experimental\variablecommenting.bat
%~dpn0.txt = C:\Users\user\BatchfileFrameWork\lib\experimental\variablecommenting.txt
%~nx0 = variablecommenting.bat
%~fs0 = C:\Users\user\BATCHF~1\lib\EXPERI~1\VARIAB~1.BAT
@cmd /k
chatgpt

Ah, I see what you're aiming for. You want to display the variable with its syntax (e.g., %~dp0) and its expanded value together in the output. To achieve this, you can use double %% for the variable part in an Echo command to display the actual variable syntax, and then follow it with the expanded variable. Here’s how you can do it:

Code: Select all

@Echo Off

Echo %%~dp0 = %~dp0
Echo %%cd%% = %cd%
Echo %%~d0 = %~d0
Echo %%~p0 = %~p0
Echo %%~fn0 = %~fn0
Echo %%~dpn0.txt = %~dpn0.txt
Echo %%~nx0 = %~nx0
Echo %%~fs0 = %~fs0

@cmd /k
In this batch file, each Echo command includes double %% for the variable part that you want to display literally, followed by the actual expanded value of the variable. When you run this batch file, the output will look like:

Code: Select all

C:\Users\user\BatchfileFrameWork\lib\experimental>variablecommenting.bat
%~dp0 = C:\Users\user\BatchfileFrameWork\lib\experimental\
%cd% = C:\Users\user\BatchfileFrameWork\lib\experimental
%~d0 = C:
%~p0 = \Users\user\BatchfileFrameWork\lib\experimental\
%~fn0 = C:\Users\user\BatchfileFrameWork\lib\experimental\variablecommenting.bat
%~dpn0.txt = C:\Users\user\BatchfileFrameWork\lib\experimental\variablecommenting.txt
%~nx0 = variablecommenting.bat
%~fs0 = C:\Users\user\BATCHF~1\lib\EXPERI~1\VARIAB~1.BAT
@cmd /k
This method allows you to clearly see both the variable syntax and its expanded value in the output.

end of AI discussion

Post Reply