getting current, parent directory name with a special & letter

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
nnnmmm
Posts: 141
Joined: 26 Aug 2017 06:11

getting current, parent directory name with a special & letter

#1 Post by nnnmmm » 23 Dec 2021 02:53

AA.BAT gives you current and parent directory names like C:\abcd\1234\xyz, but i like it to handle a directory name with an ampersand and maybe later with ! also, like C:\abcd\1234\xy&z. is it possible to fix some of them in AA.BAT

Code: Select all

@ECHO OFF
ECHO ~dp0 = "%~dp0"
ECHO   CD = "%CD%"
ECHO.

FOR %%V IN ("%~dp0.") DO SET "NCD1=%%~nxV"
ECHO %NCD1%

FOR %%V IN ("%~dp0..") DO SET "NPD1=%%~nxV"
ECHO %NPD1%

SET "ZZ2=%~p0"
SET ZZ2=%ZZ2:~0,-1%
FOR %%V IN ("%ZZ2%") DO SET "NCD1=%%~nxV"
ECHO %NCD1%

FOR %%V IN ("%CD%") DO SET "NCD1=%%~nxV"
ECHO %NCD1%

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

Re: getting current, parent directory name with a special & letter

#2 Post by Squashman » 23 Dec 2021 15:24

Nothing in your code suggests any problems with exclamation points. Also the code will have no problems processing folder names with ampersands. The only problem you would have is using echo with a variable that has an ampersand. Use quotes around the variable when echoing it or escape the ampersand with a carrot.

My eyes deceived me. You missed a set of quotes here
SET ZZ2=%ZZ2:~0,-1%

nnnmmm
Posts: 141
Joined: 26 Aug 2017 06:11

Re: getting current, parent directory name with a special & letter

#3 Post by nnnmmm » 24 Dec 2021 05:25

with "ZZ2=%ZZ2:~0,-1%" or not with ZZ2=%ZZ2:~0,-1%

if you run this batch c:\abc\xx&yy\

it can display this "%~dp0" but this doesnt run it and causes an error at xx

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

Re: getting current, parent directory name with a special & letter

#4 Post by Squashman » 24 Dec 2021 11:16

nnnmmm wrote:
24 Dec 2021 05:25
with "ZZ2=%ZZ2:~0,-1%" or not with ZZ2=%ZZ2:~0,-1%

if you run this batch c:\abc\xx&yy\

it can display this "%~dp0" but this doesnt run it and causes an error at xx
You only addressed one of the three points of my previous comment. As I said in my first comment, you either have to echo the variable with quotes or escape the ampersand.

Code: Select all

@echo off
FOR %%V IN ("%~dp0.") DO SET "NCD1=%%~nxV"
ECHO "%NCD1%"
SET "NCD1=%NCD1:&=^&%"
ECHO %NCD1%

FOR %%V IN ("%~dp0..") DO SET "NPD1=%%~nxV"
ECHO %NPD1%

SET "ZZ2=%~p0"
SET "ZZ2=%ZZ2:~0,-1%"
FOR %%V IN ("%ZZ2%") DO SET "NCD1=%%~nxV"
ECHO "%NCD1%"
SET "NCD1=%NCD1:&=^&%"
ECHO %NCD1%

FOR %%V IN ("%CD%") DO SET "NCD1=%%~nxV"
ECHO "%NCD1%"
SET "NCD1=%NCD1:&=^&%"
ECHO %NCD1%
Which outputs

Code: Select all

C:\abc\xx&yy>parent.bat
"xx&yy"
xx&yy
abc
"xx&yy"
xx&yy
"xx&yy"
xx&yy

nnnmmm
Posts: 141
Joined: 26 Aug 2017 06:11

Re: getting current, parent directory name with a special & letter

#5 Post by nnnmmm » 24 Dec 2021 12:15

FOR %%V IN ("%~dp0.") DO SET "NCD1=%%~nxV"
ECHO "%NCD1%"
i should have known this "%NCD1%" at the least and not just %NCD1%, since i wrote batch things with some luck. i usually type or put something around until it works

:&=^& in "NCD1=%NCD1:&=^&%" is the escape? i will remember this escape. and i will do some experiments on it.

now i am beginning to feel like this.
if there are two %%, then i must put quotes like this "ncd1=%%~nxV"
if there is one % then i can still put quotes like this ncd1="%~nxV" although i end up with this ""

nnnmmm
Posts: 141
Joined: 26 Aug 2017 06:11

Re: getting current, parent directory name with a special & letter

#6 Post by nnnmmm » 10 Jan 2022 07:08

adding SET "PD1=%PD1:&=^&%" is failing my batches
i need to find how to get the parent directory name only.

N:\1 2 3\A B C\pp & ! & 121\cc! & ! & 12\ABC.bat
ABC.bat has
the result has pp ^& ! ^& 121, if i escape it again, why would i even start?

Code: Select all

@echo off
FOR %%V IN ("%~dp0..") DO SET "PD1=%%~nxV"
set "K1=%PD1%"
ECHO i(K1) look incorrect and cause an error right now but i am THE right result at the end
ECHO.
ECHO %K1%
ECHO.
ECHO."%K1%"  i and STILL alive and can STILL do group quotes
ECHO ........................................

FOR %%V IN ("%~dp0..") DO SET "PD1=%%~nxV"
SET "PD1=%PD1:&=^&%"
SET "PD2=%PD1%\ABC 123.TXT"
ECHO %PD1% .... i look i am right right now, but i am so wrong at the end
ECHO.
ECHO "%PD1%" i can NOT do group quotes
ECHO "%PD2%"

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: getting current, parent directory name with a special & letter

#7 Post by aGerman » 10 Jan 2022 11:59

I don't think you even need to escape. After setting your variable either quote the content for the ECHO output

Code: Select all

echo "%K1%"
or use delayed expansion

Code: Select all

setlocal EnableDelayedExpansion
echo !K1!
endlocal
Steffen

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: getting current, parent directory name with a special & letter

#8 Post by Compo » 11 Jan 2022 08:07

I'm not sure why I'm bothering to post this, as you made absolutely no acknowledgement of my last answer to you, but as this is a new year, I'm going to try to give you at least one more opportunity to show common courtesy.

Code: Select all

@ECHO OFF
ECHO %%~dp0 = "%~dp0"
ECHO(
FOR %%G IN ("%~dp0.") DO SET "NCD1=%%~nxG"

ECHO This one, using an unquoted ECHO, may fail.
ECHO %NCD1%
PAUSE
ECHO(
ECHO This one uses an unquoted ECHO with a simple FOR loop.
FOR %%G In ("%NCD1%") DO ECHO %%~G
PAUSE
ECHO(
ECHO This one uses an unquoted ECHO from a FOR /F loop.
FOR /F "TOKENS=1,* DELIMS==" %%G In ('"(SET NCD1) 2>NUL"') DO ECHO %%H
PAUSE
ECHO(
ECHO This one uses an unquoted ECHO with DELAYED variable EXPANSION
SETLOCAL ENABLEDELAYEDEXPANSION
ECHO !NCD1!
ENDLOCAL
PAUSE
ECHO(
ECHO This one uses SET /P then forces a line ending.
SET /P "=%NCD1%" 0<NUL & ECHO(
ECHO Press any key to exit . . .
PAUSE 1>NUL
The first output uses your submitted method of using an unquoted ECHO command to pre-expand and output a variable which may contain poison characters, all of the others are better alternatives.

nnnmmm
Posts: 141
Joined: 26 Aug 2017 06:11

a problem finding a current directory name

#9 Post by nnnmmm » 11 Jan 2022 10:43

XX=if %1 looks like c:\dir 1\scummvm1\dir 2\CurrentDir\!!AA.BAT is a problem, so i need to check if currentDir has a scummvm string in it
YY=if %1 looks like c:\dir 1\dir 2\CurrentDir - scummvm1\BB.BAT is a scummvm file because i named all their current dirs with a word scummvm in them.
ZZ=if %1 looks like c:\dir 1\dir 2\CurrentDir\!!CC.BAT is a dosbox file. because i named the 1st file with "!!" in it.
AA= works except for the case of XX

Code: Select all

 FOR /F "Tokens=*" %%V IN ('ECHO "%~1" ^|FIND "scumm" /I /C') DO SET C1=%%V
 FOR /F "Tokens=*" %%V IN ('DIR "%~1\!!*.BAT" /A-D /ON /B') DO SET F1=%%V
 IF /I NOT "%C1%"=="0" GOTO :SCUMMVM
 IF /I NOT "%F1%"=="" GOTO  :BOX
BB= doesnt work

Code: Select all

FOR %%V IN ("%~dp0.") DO SET "CD1=%%~nxV"
ECHO "%CD1%"

SET "Z2=%~p0"
SET "Z2=%Z2:~0,-1%"
FOR %%V IN ("%Z2%") DO SET "CD1=%%~nxV"
ECHO "%CD1%"

FOR %%V IN ("%CD%") DO SET "CD1=%%~nxV"
ECHO "%CD1%"

 FOR /F "Tokens=*" %%V IN ('ECHO "%CD1%" ^|FIND "scumm" /I /C') DO SET C1=%%V
 FOR /F "Tokens=*" %%V IN ('DIR "%~1\!!*.BAT" /A-D /ON /B') DO SET F1=%%V
 IF /I NOT "%C1%"=="0" GOTO :SCUMMVM
 IF /I NOT "%F1%"=="" GOTO  :BOX
i dont know why BB doesnt work, maybe CD1 is acting like a filename instead of a current dir name. when i tested all 3 CD1's, they all reported current dirs correctly.

you can make AA= more stable if you wish, i didnt quote them at the end, but i tried "F1=%%V" "F1=%%~V". C1=%%V was a best choce i ran out of energy to check \ and "" more.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: a problem finding a current directory name

#10 Post by Compo » 11 Jan 2022 12:22

Do you really want help? do you only want help of specific individuals? or do you feel as if you are entitled to free assistance?

Getting help from others is not a right, but something you must earn. This is a two way process, you ask for assistance and willing helpers try to assist you. If however, you show no willingness to respond, or try to implement or learn from their advice, you will quickly find that the number of willing helpers will diminish.

Currently you have a lot of active questions, the majority of which all seem to be rather similar, yet none of which indicate that your problem has been solved, or the subsequent ones, do not show that you've implemented the advice, or learned from it.

I have, for over thirty years, helped others on various newsgroups, sites and platforms with batch file scripting, and to date few of the questioners have made it onto my ignore list. Please do not be one of those.

Please fully explain what your issue is, when responding to answers with further problems, show your newly implemented code and properly describe the issue you want to report, and follow that process logically until your issue is resolved. Asking many similar questions separately does not allow responders to assist you, without them having to first locate all of your other questions, read through all of the threads and try to collate them before assisting you. You are just making things harder for everyone, and moreso if you appear to ignore some of the offered advice.

nnnmmm
Posts: 141
Joined: 26 Aug 2017 06:11

Re: getting current, parent directory name with a special & letter

#11 Post by nnnmmm » 11 Jan 2022 22:53

i was gonna get to my problem having issue with ! & names, but i had to finish this one in at least semi working condition at the moment, FOR /F "Tokens=*" %%V IN ('ECHO "%CD1%" ^|FIND "scumm" /I /C') DO SET C1=%%V, or i might all lose all interests, i will get to my problems getting long registries and long website names soon, but not now, my head is about to explode

i think i found the problems with these two.
AA=FOR /F "Tokens=*" %%V IN ('ECHO "%~1" ^|FIND "scumm" /I /C') DO SET C1=%%V
BB= FOR /F "Tokens=*" %%V IN ('ECHO "%CD1%" ^|FIND "scumm" /I /C') DO SET C1=%%V
when i directly ran the batch file with %CD1% at the location C:\ABC\123\BB.bat for testing,it reported 123 correctly
but at the end, i need to link the bacth file with %CD1% or "%~1" to the file mangers like ztree, xtree and windows explorer so that i can get the variable %1 from them. i have to run the batch over the directory window and not at the file window which means at this location C:\ABC\123. OS's %CD1% reports as \ABC, this means i have to deal with AA instead of BB, but i havent tested enough how this "%~1" works, and i got exhausted, so i was hoping dostip to point me to the direction, you people can read the operators like lightening. since i cant read well, i have to test it for hours, which means, i plug something until it works without a logic but sensibly and not too far from the one that is semi-working. i was gonna handle ! & 1st before i could link it with other file managers, but right now, i need to see other step and check if this is gonna even work at all , or the bacth that fixes ! & is going to waste. let me get to ! & a little later

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: getting current, parent directory name with a special & letter

#12 Post by Compo » 12 Jan 2022 03:06

Well, I tried…

Code: Select all

@WMIC Path Dos_Tips Where "AccountName='nnnmmm'" Get OutaHere 1>NUL 2>&1

nnnmmm
Posts: 141
Joined: 26 Aug 2017 06:11

Re: getting current, parent directory name with a special & letter

#13 Post by nnnmmm » 12 Jan 2022 06:48

Code: Select all

@ECHO OFF
ECHO %%~dp0 = "%~dp0"
ECHO(
FOR %%G IN ("%~dp0.") DO SET "NCD1=%%~nxG"
ECHO This one, using an unquoted ECHO, may fail.
ECHO %NCD1%
i understood, i should not have quoted set b1="%NCD1%" to get the new variable b1, just because echo failed, it came as a shock at least visually, i tried to fix all at least in my head before going to a next step and i over-quoted

nnnmmm
Posts: 141
Joined: 26 Aug 2017 06:11

Re: getting current, parent directory name with a special & letter

#14 Post by nnnmmm » 12 Jan 2022 23:20

Squashman
aGerman
Compo
thanks for all your help

Post Reply