Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

split a variable that contains a path

26 views
Skip to first unread message

icaro

unread,
Dec 31, 2022, 7:25:56 PM12/31/22
to
Hi all,
I have a batch file with a variable containing a random path, eg
C:\some\thing\not\so\useful
and I need to have an output splitted in this way (the first line must
contail the drive and the first directory)

C:\some
C:\some\thing
C:\some\thing\not
C:\some\thing\not\so
C:\some\thing\not\so\useful

Could you please help me?
Regards
Icaro

R.Wieser

unread,
Jan 1, 2023, 2:28:48 AM1/1/23
to
"icaro" <ic...@icaro.invalid> wrote in message
news:5bk1rh95bmkml250q...@4ax.com...

> Could you please help me?

First question : What did you try yourself ? Please include it in your
question. Who knows, maybe just a small adjustment is needed to get it to
work ...

Second question : are you sure you need all those paths ? Maybe if you
describe what you need it for* there is an easier solution available

* find the path to a specific file perhaps ? (see "for /?")

Remark : I assume its *not* a homework assignment ...

A possible way to a solution to what you asked :

https://stackoverflow.com/questions/26537949/how-to-split-variables-in-batch-files

Remark #2 : Do realize that the batch environment is rather finickey in
regard to string handling. Throwing random strings at any batch solution
will likely break sooner or later.

Regards,
Rudy Wieser


JJ

unread,
Jan 1, 2023, 2:50:08 AM1/1/23
to
This should do it.

@echo off
setlocal

set "p=C:\some\thing\not\so\useful\"

for %%A in ("%p%") do set "p=%%~dpnxA"
if "%p:~-1%" == "\" set "p=%p:~0,-1%"
set n=0
call :disppath "%p%"
for /l %%A in (%n% -1 1) do call echo %%p%%A%%
pause
goto :eof

:disppath
set "a=%~dp1"
if "%a:~-1%" == "\" set "a=%a:~0,-1%"
set/a n+=1
set "p%n%=%~1"
set "s=%a:\=%"
if "%s%" == "%a%" goto :eof
call :disppath "%a%"

Herbert Kleebauer

unread,
Jan 1, 2023, 4:56:48 AM1/1/23
to
On 01.01.2023 01:25, icaro wrote:
> Hi all,
> I have a batch file with a variable containing a random path, eg
> C:\some\thing\not\so\useful
> and I need to have an output splitted in this way (the first line must
> contail the drive and the first directory)
>
> C:\some
> C:\some\thing
> C:\some\thing\not
> C:\some\thing\not\so
> C:\some\thing\not\so\useful

@echo off
set p=C:\some\thing\not\so\useful\
for /f "tokens=1,2* delims=\" %%i in ('echo %p%') do set p=%%k&set q=%%i\%%j

:loop
echo %q%
if [%p%]==[] goto :eof
for /f "tokens=1* delims=\" %%i in ('echo %p%') do set p=%%j&set q=%q%\%%i
goto :loop



Robert Roland

unread,
Jan 1, 2023, 7:59:23 AM1/1/23
to
On Sun, 1 Jan 2023 08:28:37 +0100, "R.Wieser" <add...@not.available>
wrote:

>Remark : I assume its *not* a homework assignment ...

Do people learn batch in school these days? I'd hope they'd moved on
to PowerShell by now.
--
RoRo

R.Wieser

unread,
Jan 1, 2023, 9:52:11 AM1/1/23
to
"Robert Roland" <fa...@ddress.no> wrote in message
news:jp03rht00t5dkbb5t...@4ax.com...

> Do people learn batch in school these days? I'd hope they'd moved
> on to PowerShell by now.

:-) You never know what a teachers motives behind giving such an assignment
could be. In this case it /could/ be just carefully reading some
documentation (the teacher mentioned).

Alas, as the reason for needing those paths (in that particular order)
wasn't given we will probably never know.

Regards,
Rudy Wieser


icaro

unread,
Jan 1, 2023, 2:41:08 PM1/1/23
to
On Sun, 1 Jan 2023 08:28:37 +0100, "R.Wieser" <add...@not.available>
wrote:

Hi Rudy,
you're right. I'm trying to make a backup script to sync some folders
on my Windows PC via SSH to another Windows machine. Because Windows
doesn't have a rsync client I've tried to keep aligned the directories
with WinSCP commandline.
Unfortunately WinSCP can't create nested paths using a single command
like mkdir /c:/bk/dir1/dir2/dir3 but needs to proceed dir by dir (it
does not have -p switch to make parent directories). This is why I
need the weird output.

Eventually I found a way to customize a script taken from
StackOverflow

@echo off
setlocal enabledelayedexpansion
set "out="
set s=C:\Some\thing\not\so\useful
set t=%s%
:loop
for /f "tokens=1* delims=\" %%a in ("%t%") do (
set out=!out!/%%a
if "!out:~-1!" NEQ ":" echo out = "!out!"
set t=%%b
)
if defined t goto :loop
setlocal disabledelayedexpansion

This is the output:
out = "/C:/Some"
out = "/C:/Some/thing"
out = "/C:/Some/thing/not"
out = "/C:/Some/thing/not/so"
out = "/C:/Some/thing/not/so/useful"

Regards,
icaro

icaro

unread,
Jan 1, 2023, 2:44:37 PM1/1/23
to
>This should do it.

Yes, thank you, it works great!

Regards
icaro

icaro

unread,
Jan 1, 2023, 2:44:43 PM1/1/23
to
On Sun, 1 Jan 2023 10:56:43 +0100, Herbert Kleebauer <kl...@unibwm.de>
wrote:

Hi Herbert, thank you. Your script works great, too!

Regards
icaro

Zaidy036

unread,
Jan 1, 2023, 3:32:07 PM1/1/23
to
I have never used WinSCP but RoboCopy will do what you want with one
statement copying C:/Some

icaro

unread,
Jan 1, 2023, 5:08:13 PM1/1/23
to
On Sun, 1 Jan 2023 15:32:03 -0500, Zaidy036 <Zaid...@air.isp.spam>
wrote:

>I have never used WinSCP but RoboCopy will do what you want with one
>statement copying C:/Some

Yes, it's true. I use RoboCopy for my first backup copy, but I'm
planning to do a second offsite copy via network.
To use RoboCopy also for this case I would need to mount the
filesystem over a SSH connection and this seems to me less reliable
(but I'm not 100% sure) compared to WinSCP directory alignment.

Regards
icaro

Zaidy036

unread,
Jan 1, 2023, 8:41:48 PM1/1/23
to
Look at first answer from Google search:"robocopy using ssh"
<https://superuser.com/questions/138636/mirror-backup-from-ssh-sftp-to-windows>

R.Wieser

unread,
Jan 2, 2023, 1:00:37 AM1/2/23
to

"icaro" <ic...@icaro.invalid> wrote in message
news:u3o3rh95cfo22noc1...@4ax.com...

> Unfortunately WinSCP can't create nested paths using a single
> command like mkdir /c:/bk/dir1/dir2/dir3 but needs to proceed
> dir by dir (it does not have -p switch to make parent directories).
> This is why I need the weird output.

Thanks for the explanation.

Question : have you thought about a folder possibly (likely?) having
multiple subfolders ? Your current code doesn't account for them...

Also, are you aware that your current code happily returns relative paths
(not starting with a drive letter) ? The end result might not quite be
what you expect from it (the files could be read from / be written to
unexpected places)

> Eventually I found a way to customize a script taken from
> StackOverflow

I hope you also read Herberts reply ? Its a bit compacter, but more
important, it solves the problem of your paths starting with slash before
the drive letter - which, IIRC, makes the returned paths invalid.

Ofcourse, looking at Zaidy036's reply (toward using robocopy) this batch
tinkering could be moot. :-)

Regards,
Rudy Wieser


mokomoji

unread,
Apr 24, 2023, 11:57:20 AM4/24/23
to
2023년 1월 1일 일요일 오전 9시 25분 56초 UTC+9에 icaro님이 작성한 내용:
해당 기술에는 2가지 소스가 존재 합니다.

첫번쨰 소스
---------------------------------------------------------------------------------
@echo off
setlocal
cd /d "%~dp0"
set "z_path=%cd%"

:loop
pushd "%cd%"
cd ..
if /i "%cd%" equ "c:\" goto :z_out
goto :loop
:z_out

pushd

endlocal
pause

---------------------------------------------------------------------------------
매우 심플 합니다.
당신이 pushd와 popd를 자유 자재로 쓸 수 있다면 한가지 답이 될 수 있습니다.

두번쨰 소스
---------------------------------------------------------------------------------
@echo off
setlocal
cd /d "%~dp0"
set "z_path=%cd%"

:loop
set /a z_n+=1
set z_n2=000%z_n%
set z_n2=%z_n2:~-3%
set "z_var_%z_n2%=%cd%"
cd ..
if /i "%cd%" equ "c:\" goto :z_out
goto :loop
:z_out

for /f "tokens=2 delims==" %%f in (
'set z_var_^|sort /r'
) do (
echo "%%~f"
)

endlocal
pause
---------------------------------------------------------------------------------
이 또 한 배열을 이용한 나열 방법입니다.
call 분기로 복잡하지 않으며, goto 로 딱 떨어지는 기초 문법 입니다.




mokomoji

unread,
Apr 24, 2023, 12:32:57 PM4/24/23
to
2023년 1월 1일 일요일 오전 9시 25분 56초 UTC+9에 icaro님이 작성한 내용:
popd의 응용 방법론..
----------------------------------------------------------------------
@echo off
setlocal
cd /d "%~dp0"
set "z_path=%cd%"
rem debug mode rem echo switch
set "z_switch_1=rem"
set "z_switch_2="

:loop
pushd "%cd%"
cd ..
if /i "%cd%" equ "c:\" goto :z_out
goto :loop
:z_out


:loop2
set "z_p=%cd%"
%z_switch_1% echo "%cd%"
rem c:\ only drive
popd
if /i "%z_p%" equ "%cd%" goto :z_out2
%z_switch_2% echo "%cd%"
rem c:\firstpath
goto :loop2
:z_out2

endlocal
pause


---------------------------------------------------------


switch1 type
set "z_switch_1=rem"
set "z_switch_2="

output1
"C:\Users"
"C:\Users\mokomoji"
"C:\Users\mokomoji\Desktop"
"C:\Users\mokomoji\Desktop\배치파일 2023"
"C:\Users\mokomoji\Desktop\배치파일 2023\20230425"
계속하려면 아무 키나 누르십시오 . . .

switch2 type
set "z_switch_1="
set "z_switch_2=rem"

output2
"C:\"
"C:\Users"
"C:\Users\mokomoji"
"C:\Users\mokomoji\Desktop"
"C:\Users\mokomoji\Desktop\배치파일 2023"
"C:\Users\mokomoji\Desktop\배치파일 2023\20230425"
계속하려면 아무 키나 누르십시오 . . .
0 new messages