Reputation: 373
I need to process a full path to a file, i.e. C:\fold1\fold2...\foldN\filename.dat in order to get the parent file folder and file name separately, that is root=C:\fold1\fold2...\foldN and file=filaname.dat.
This because I want to generalise a call to a program no matter from where you call the main .bat (this one), such to be able to perform a pushd
to %root% before calling this specific program.
I am working at this, but I get stack when posing line=%%c
:
: before remove quotes from %var%
set var=%var:"=%
if exist %temp% (
if exist %temp%\filepath.txt del %temp%\filepath.txt
echo %var% > %temp%\filepath.txt
)
for /f "tokens=1,* delims= " %%z in (%temp%\filepath.txt) do (
set line=%%z
echo Line writes %line%.
set root=empty
goto :processtoken
)
:processtoken
for /f "tokens=1,* delims=\" %%b in ("%line%") do (
echo %%b
echo %%c
set line=%%c rem this does not work, why??
echo Now line writes %line%
if "%root%"=="empty" (
echo [INFO] Root is empty, initialising..
echo %%c
set root=%%c
) else (
set root=%root%\%%c
)
echo Root is %root%
goto :end
)
The problem I am facing is that when I print to see how %line% has changed, it shows that %line% (after set line=%%c
corresponds to the FULL PATH (while my intention is to recursively get to the file name, I still need to add the condition in finding the "\"
string in %%c, when not present anymore it will mean we got to the final step, i.e. %root% will now correspond to the final root folder).
Thanks to who will try to help me resolving this issue.
EDIT: this main program is called as follow:
prog arg1 arg2 arg3 arg4 arg5
arg5 is the path to the file which will internally be the argument for this other program. To be as general as possible I want to made functional this program no matter from where you call it. Still you have two options:
you are already in the folder containing the file to be processed by this internally called program, in such case arg5 will be passed as filename.dat (without quotes, except if it contains spaces)
you are not in the root folder so you pass directly the FULL PATH (in double quotes if it contains spaces).
The problem is in case 2, since this internal program works properly when you call it from root directory and you pass to it only FILENAME.DAT.
This is why I posed this question. %var% is simply arg5, in the ways I explained hereby.
I hope I have been a little more clear than before.
PS. I'm not an experienced programmer in all ways, so I do apologise if I miss in clearance and professionalism. The write/read from/to %temp% folder was just to exploit a newer way of programming in batch, nothing else. I knew it was superfluous.
Upvotes: 0
Views: 2470
Reputation: 16236
Another way to do this in a batch-file would be to use the PowerShell Split-Path
command designed to do this.
SET "THEFILE=%TEMP%\file path.txt"
FOR /F "delims=" %%A IN ('powershell -NoLogo -NoProfile -Command
"""$([Convert]::ToChar(34))$(Split-Path -Path """%THEFILE%""")$([Convert]::ToChar(34)) $([Convert]::ToChar(34))$(Split-Path -Path """%THEFILE%""" -Leaf)$([Convert]::ToChar(34))"""') DO (
SET "FILESTRING=%%A"
)
SET "PATHPART="
FOR %%A IN (%FILESTRING%) DO (
IF NOT DEFINED PATHPART (SET "PATHPART=%%~A") ELSE (SET "FILEPART=%%~A")
)
ECHO PATHPART is "%PATHPART%"
ECHO FILEPART is "%FILEPART%"
Of course, it is easier if the script is written for PowerShell.
$TheFile = "$Env:TEMP\filepath.txt"
$PathPart = Split-Path -Path $TheFile
$FilePart = Split-Path -Path $TheFile -Leaf
UPDATE:
Actually, it appears that splitting the path is not needed to use PUSHD. Just add \..
to the end.
PUSHD C:\Users\joe\afile.txt\..
Upvotes: -1
Reputation: 56180
since you mentioned set var=%5
in a comment:
set "root=%~dp5"
set "file=%~nx5"
should be all you need. See call /?
for more details.
Upvotes: 2
Reputation: 82297
You should take a look at the for-variable modifiers (FOR /?
)
root=%%~dpz
is used to expand to: d=drive and p=path of z
file=%%~nxz
is used to expand to: n=name and x=extension of z
Then you can change your block to
for /f "tokens=1,* delims= " %%z in (%temp%\filepath.txt) do (
set "line=%%z"
set "root=%%~dpz"
set "file=%%~nxz"
REM ** Show the variables
set root
set file
)
Upvotes: 1