-
Notifications
You must be signed in to change notification settings - Fork 102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#467: quickfix for tilde expansion #468
#467: quickfix for tilde expansion #468
Conversation
Compared to the current state on |
So it more or less works now.
Output from
We could fix the case with
However, if the additional arguments are empty, trailing whitespaces are added to the variable value and I am unsure what other side-effects this may have. Preventing this with IF statements does not work as described in #467. |
set "search=%~1% " | ||
) | ||
set "replacement=%~1%=" | ||
set value=%~2 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isn't here a % missing at the end?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope. %2
is the second argument and %~2
expands it and unquotes.
https://ss64.com/nt/syntax-args.html
So your question might be why is there an extra percent sign after the first arg.
I can only reasonable answer for the %~1%=
case as here the %
sign is an escape for the equals sign.
In the other case (line 92) it may be obsolete to add the extra percent.
I can test that and can also not tell if I initially wrote this code as Rohit supported the fix #395
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So also the equals sign does not need to be escaped with percent sign.
To make you happy and avoid further confusion I removed the pointless percent signs.
According to my tests in Win VM it does not make any difference so fingers crossed that this will not break anything in this fragile CMD universe...
Can we resolve our conversations now and merge this PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well done to remove the trailing percent here. They should not be used after positional parameters.
Only variables within scripts must be surrounded by percents - for positional parameters this is just accepted for "convenience". (The percent is no escape char here)
A "%1%2" still resolves to first pos. param followed by 2nd,
From today's view point it is easy to say, this was a bad design decision - but in the 80'ies this was not so easy to forsee.
set value=%~2 | ||
if "!value:~0,1!" == "~" ( | ||
rem variable value starts with tilde that needs to be replaced with users home dir (USERPROFILE) | ||
set line=%replacement%%USERPROFILE%!value:~1! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for me it's fine to just support ~ replacement at the first character of the value. Anyhow just in case you havent seen it, there are solutions to a more generic algorithm, which anyhow underlines how crappy batch is :( https://stackoverrun.com/de/q/9716479
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the link. After rethinking it might even be wrong to resolve C:/data/file~name
to C:/data/fileC:\users\hohwillename
. So I would rather consider this as a feature :)
This is a "fix" for #467.
However, it currently only replaces the tilde (
~
) if the variable value starts with it and is not quoted. Further for such variables it reintroduces the bug #100 and #395. So assuming that we only have cases likeOr
But not something like
this PR could be merged.
If we want a clean solution, further investigation is required (and IMHO a real CMD guru is needed as I run out of ideas).