Page 1 of 1

Issue with Directory Extraction & Spaces

Posted: 06 Feb 2025 10:48
by lmstearn
Googlus est amicus however not today, - is this a known issue with cmd, or just back over here with YT:

Code: Select all

@echo off
Setlocal EnableDelayedExpansion
SET "folderName=C:\ProgramData\Microsoft OneDrive"
FOR %%g IN ("%folderName%") DO (
set "tmpVar=%%~pg"
REM Strip possible leading backslash
IF "!tmpVar:~0,1!"=="\" SET "Foldername=!tmpVar:~1!"
)
@echo on

echo tmpVar !tmpVar!
echo folderName !folderName!
@echo off
SET /P ttt="Test Complete"
exit /B
There are other ways to extract the drive from the path, this seemed the most convenient except for the (blech) spaces, please correct where required.
Thanks.

Re: Issue with Directory Extraction & Spaces

Posted: 06 Feb 2025 12:07
by aGerman
The expanded string using the "~p" modifier will always contain a leading backslash. (Why do you think this is depending on the path containing spaces?)
"C:" is a drive
"C:\" is the root folder of drive C:
"\" is the root folder relative to the current drive
"\wherever" is a path relative to the current drive

However:
"wherever" (without the leading backslash) is a path relative to the current directory :!:
So, the absence of the leading backslash changes the meaning. I don't see any bug in the expansion performed using "~p".

If you want to remove the leading backslash then you don't need the
IF "!tmpVar:~0,1!"=="\"
because this condition is always true.

Steffen

Re: Issue with Directory Extraction & Spaces

Posted: 06 Feb 2025 22:25
by lmstearn
Hi Steffen, yes it was very late, and like DosTips, my gateway is not as good as it used to be.
Ouch, should have checked the same with:

Code: Select all

SET "folderName=C:\ProgramData\Microsoft"
Not a bug, thanks!
Missed that, hence made the common error of assuming the whole thing is a path, which it kind of is really. :P
%~p1 - expands %1 to a path only
Prefer this anyway:
%~p1 - expands %1 to its constituent path only
Think the original intention was the following, given the argument is a path:

Code: Select all

set "tmpVar=%%~png"
If you want to remove the leading backslash then you don't need the
IF "!tmpVar:~0,1!"=="\"
because this condition is always true.
Thanks. :)

Re: Issue with Directory Extraction & Spaces

Posted: 07 Feb 2025 10:32
by aGerman
lmstearn wrote:
06 Feb 2025 22:25
Think the original intention was the following, given the argument is a path:

Code: Select all

set "tmpVar=%%~png"
Hmm. I'd rather expect the intention was %%~pnxg :lol: (Only guessing though.)
Why? Because even a folder name containing one or more dots is perfectly valid. Without the x, both the last dot and the final substring are excluded since they are treated like a file extension.

Steffen

Re: Issue with Directory Extraction & Spaces

Posted: 13 Feb 2025 04:58
by castwhom
Your script extracts the drive letter inefficiently; use %%~dg for a cleaner, more reliable solution.

Re: Issue with Directory Extraction & Spaces

Posted: 13 Feb 2025 10:03
by aGerman
But this topic is about the oposite (keep everything except the drive).