set "newpath=%~1.\"
this will resolve to (for EG:)
c:\windows.\
or
c:\windows\.\
and both of those are equivalent to
c:\windows
--
Regards,
Mic
Now that's a clever method.
It is however targeted to the particular problem - not that there's anything
wrong with that...
There is however a hidden trap. If there is no argument defined, then
NEWPATH will be set to ".\" which may or may not be what's intended.
In a more general sense, I'd use
set var=%~1
if defined var if not "%var:~-1%"=="\" set var=%var%\
where VAR contains the string in question, noting the '-length' syntax of
the substring was introduced in XP, IIRC and the length could be varied if
required - for looking for appended switches, for instance.
So one could envisage a 'safe' routine along the lines of
set var=%~1
if not defined var echo MAYDAY! MAYDAY! VAR undefined&pause&exit
if not "%var:~-1%"=="\" set var=%var%\
I'm also intrigued by the way syntax seems to be exploited.
I'd suggest that in "c:\windows.\" the "." means "here be the end of the
NAME, there is no [further] extension" whereas in "c:\windows\.\" it means
"the current directory relative to the aforementioned directory."
Explain the '%var:~-1%' part to me. It looks like it just returns the
last character of the string and it works when var is set to %~1, but
when I try
set var=D:\folder
if defined var if not "%var:~-1%"=="\" set var=%var%\
I get a syntax error.
I don't think you do. Try this batch file.
@echo off
set var=D:\folder
if defined var if not "%var:~-1%"=="\" set var=%var%\
echo %var%
pause
--
Regards,
Mic
You certainly shouldn't get an error if that's EXACTLY what batch is seeing.
It's perfectly valid syntax for XP and later.
see
SET /?
from the prompt for an incomplete analysis of the substringing options. In
short,
%var:~m,n% is the substring of n characters starting at position m in the
string (positions start from ZERO)
",n" is optional. If not specified, from position m to the end of the string
m or n negative means count of characters from the END of the string
hence %var:~-1% mas m=-1 and n omitted, so FROM 1 character away from the
end of the string TO the end of the string, which is the last character
alone.
Since spaces, commas, semicolons and tabs are separators, if VAR contains
any of these, any substring may equally contain them. If the substring is
surrounded by quotes, the separators are interpreted as regular characters,
hence quoting both sides protects against this potential minefield.
So - as to the cause of the syntax error - I'll presume you've checked your
listing twice (since that's quite the tradition, given the season)
I'd suggest you may be using Notepad. Please refrain. Use EDIT (my
preference) since Notepad has the reindeeresque habit of playing games with
the text, convinced that everything is some variety of WP file.
-------------------------------------------8<---------------------------------
@Echo oFF
Call :Test-backslash "C:\"
Set Test-path
Call :Test-backslash "%Windir%\System32\Drivers"
set test-path
Call :Test-backslash "C:\Documents and Settings\\\\\\"
set test-path
Call :Test-backslash "D:"
set test-path
pause & exit /b
:Test-backslash
Setlocal EnableExtensions
For /f "Tokens=1*" %%T In ('
cipher /Q "%~1\*"
') Do If Not Defined tp Set tp=%%U
Endlocal & (
Set "test-path=%tp%" & goto :eof
)
----- OR
--------------------------------8<------------------------------
Set "Var=yourPath"
2>Nul >Nul (Set Var && (
Set Var|FindStr "\\\>"||Set Var=%Var%\
))
Set Var
--------------------------------------8<-------------------------------
It's very easy. Try:
@echo off
setlocal
set "parampath=%~f1\"
set "parampath=%parampath:\\=\%"
echo %parampath%
It's interesting to see the variations in techniques.
I've just tested a copy command in XP Pro and this works - I don't know
when this behaviour was implemented.
copy a.txt "d:\backup\abc\\def jhi\\"
So you can use double backslashes in XP at least.
--
Regards,
Mic
And you can add checking:
if "%~1"=="" goto:eof
Had double quotes around the string. Fixed. Thanks.
I'm on a handheld so I'm shooting in the dark, but how about something
with FOR/D:
FOR /D %%d in (%path%) do SET "dir=%%fd"
Frank
Did you mean something like this?
FOR /D %%d in (%folder%) do SET "dir=%%~fd"
I find that it merely provides the literal folder that is in the
environment variable, Frank, no slashes added or removed.
--
Regards,
Mic
> Did you mean something like this?
Something like.
> I find that it merely provides the literal folder that is in the
> environment variable, Frank, no slashes added or removed.
I only break out Windows once a week for a few minutes now and I
haven't used a Windows console in over a month. I expect that my
knowledge of CMD will fade to nothing in another month or two and I'll
have to abandon this group. Until then I'll continue to appear
confused and incompetent.
Frank
> I only break out Windows once a week for a few minutes now and I
> haven't used a Windows console in over a month. I expect that my
> knowledge of CMD will fade to nothing in another month or two
It happens doesn't it. Stop using some knowledge and before long it's
hard to remember what's what.
Do you write scripts in Linux to pass the time?
--
Regards,
Mic
It is normal that most of what my computer does was written by me. My
binary programs number over 100, and that does not include programs
for MSDOS and other operating systems. I'm guessing the total is well
over 200. Since Windows NT I have also done many things in CMD script,
and I think those number between 100 and 200 -- closer to 200.
A few months ago I had to replace my Palm TX and I bought a Archos 5
Internet Tablet -- a 5" diagonal handheld running Android 1.6, the
Linux OS. There is a scripting host for it (SL4A) which hosts about
six languages (JavaScript, Python, Ruby, PHP, Beanshell, PERL, ...) so
I have been very busy making it do things with JavaScript. This
JavaScript runs in the Rhino shell and has access to a large part of
the Java library, so it's very capable.
I don't recommend the Archos 5. It experiences the same problem my TX
did: the touchscreen alignment gets so bad that the system cannot
recalibrate it.
To pass the time? I don't know. I guess I have nothing else to do so
perhaps that is why.
Frank
It sounds like you enjoy it, and coding.
Shame about the dodgy touchscreen.
--
Regards,
Mic
One more method:
SET "directory=D:\humbug"
:: Prints directory=D:\humbug
SET directory
FOR %%a in (%directory%\.) do Set "directory=%%~fa\"
:: Prints directory=D:\humbug\
SET directory
FOR %%a in (%directory%\.) do Set "directory=%%~fa\"
:: Prints directory=D:\humbug\
SET directory
Also works with FOR/D.
Frank
@echo off
SET "directory=D:\humbug\bonus folder"
SET directory
FOR %%a in ("%directory%\.") do Set "directory=%%~fa\"
SET directory
FOR %%a in ("%directory%\.") do Set "directory=%%~fa\"
SET directory
pause
A small amendment to make it function with long path names, Frank.
--
Regards,
Mic