Will all versions of Windows ignore a lone caret on the command line?

Microsoft Windows
Post Reply
Rekrul
Posts: 51
Joined: 2021-Aug-15, 11:29 pm

Will all versions of Windows ignore a lone caret on the command line?

Post by Rekrul »

As the title says, will all versions of Windows ignore a lone caret on the command line, not physically next to anything else?

The reason I want to know is that the caret is the only thing I could think of to use as a placeholder for a missing command on a command line.

Let's say that you have a script that lets you set different options for a command line that will be executed. Without setting a flag and then executing different versions of the command line based on that flag, how do you omit an option from the command line? And if you have multiple different options that can be included or omitted, things can get pretty messy.

You could put all the possible options into a single variable to include on the command line, but that can also get messy as options are changed and it has to figure out which ones to include or omit. And what if some options need to be placed in different parts of the command line, like after a filename?

My solution was to make each option/parameter its own variable which could then be placed anywhere on the command line, however that still left the problem of what to do if an option needs to be omitted. In other words, it shouldn't be there at all. I first thought of using a single space, but Batch won't let you define a variable as just a space, or even multiple spaces.

So I tried using a single caret, and it seems to work fine. Every command that I tried ignored it. For example the following works in a script (but not in immediate mode);

Code: Select all

set confirm=/p
del %confirm% *.jpg

REM Caret doubled to escape it
set confirm=^^
del %confirm% *.jpg
Now I'm wondering if this is true on every version of Windows from XP to Win11?
User avatar
Simon Sheppard
Posts: 187
Joined: 2021-Jul-10, 7:46 pm
Contact:

Re: Will all versions of Windows ignore a lone caret on the command line?

Post by Simon Sheppard »

If you have a caret at the end of a line, that can have the effect of escaping the CR at the end of the line, which may have undesirable effects depending what is on the next line.

It involves writing more code, but I would prefer to test for empty values and then branch to a different subroutine accordingly.

Optional parameters are something that PowerShell makes much easier to handle.
Rekrul
Posts: 51
Joined: 2021-Aug-15, 11:29 pm

Re: Will all versions of Windows ignore a lone caret on the command line?

Post by Rekrul »

Simon Sheppard wrote: 2022-Dec-19, 12:26 am If you have a caret at the end of a line, that can have the effect of escaping the CR at the end of the line, which may have undesirable effects depending what is on the next line.
I'll just have to be careful that it's never at the end of a line.
Simon Sheppard wrote: 2022-Dec-19, 12:26 amIt involves writing more code, but I would prefer to test for empty values and then branch to a different subroutine accordingly.
Whenever possible, I like to have one subroutine that can be tweaked with parameters, rather than having different versions of the same routine. Later I might go back and make a change that I want to apply to all instances, but if there are 3-4 versions of the routine, I might forget to change one of them. If there's only routine, then I don't have to worry about making sure I change multiple copies of it.

Plus, even with just three parameters that can be combined or omitted, you would have eight possible combinations to account for: None, A, B, C, AB, AC, BC, ABC. It just gets worse from there. Placing each parameter into a string/variable that can be replaced with a do-nothing placeholder if that parameter needs to be omitted, seems like the most logical way to handle it.
Simon Sheppard wrote: 2022-Dec-19, 12:26 amOptional parameters are something that PowerShell makes much easier to handle.
Besides not knowing anything about Powershell, I don't even have it. I still have an old system with an old version of Windows, and while Powershell was available for it, all pre-Win7 downloads seem to have been scrubbed from Microsoft's site. And of course, any purported downloads anywhere else all try to link back to the official Microsoft download site.
User avatar
Manna5
Posts: 14
Joined: 2021-Aug-03, 7:54 am
Contact:

Re: Will all versions of Windows ignore a lone caret on the command line?

Post by Manna5 »

BTW you can assign a space to variable, using quotes.

Code: Select all

SET "SPACE= "
SS##66
Posts: 28
Joined: 2023-Jan-24, 4:51 pm

Re: Will all versions of Windows ignore a lone caret on the command line?

Post by SS##66 »

I do not know if this will help, but I recently had some issues with using carets for PowerShell script inside a BAT file [on Windows 10] that got solved: https://www.dostips.com/forum/viewtopic.php?f=3&t=10850
Post Reply