1.

Solve : Using batch script to parse directories from a string?

Answer»

I'm writing a script that needs need to call a parameter from an EXECUTABLE's initialization file. I know where the executable is in this instance, but I'd like to write a reusable subroutine as I see this as a potential helpful TOOL. What I am after specifically would be the following:

Program 1.exe has a parameter file located at:

c:\a\b\c d e\f\g.ini

Program 2.exe similarly has one located at:

c:\a\b\c d e\256\here_there\initialize.xml

I'd like to have the subroutine work as follows:

Code: [Select]:: extract.bat
@echo off

:GetTarget from user input

set FILEPATH="c:\a\b\c d e\f.txt"
if not (%1)==() set FILEPATH=%1

REM where FILEPATH is a path to a file, whether fully qualified
REM or relative doesn't matter, we'll parse the string and
REM make it fully qualified and even set environmental
REM VARIABLES as needed.

:CheckExist make sure the expanded %FILEPATH% exists
echo. Checking to see if %FILEPATH% exists...
echo.
if not exist %FILEPATH% (echo.%FILEPATH% does not exist or is not a valid file name.
goto :EOF
)
echo. %FILEPATH% exists.
echo.

:EXPAND FILEPATH to parse
if "FILEPATH"=="" goto :JUMPOUT
for /f "tokens=1* delims=\" %%a in ("%FILEPATH%") do (
echo. %%a
set FILEPATH=%%b
goto :EXPAND
)

:JUMPOUT

:EOFHere's where I get a little confused. I guess I need to echo the output to a temp file in %TMP% so I can use the find command, but there has to be an easier way to parse the result.

Finding the drive should be easy since we can search on a ':' and the letter prior is the drive.

Finding the file name should be easy as it will be the last output. However, I don't want to use a FIND command here because the file may or may not have an extension.

The rest of the path should be easy as we can just construct the directories as the variables in between.

set path=%dir1%%dir2%%dir3%...%dir*%

However, getting each of these above output lines into a variable is where I am a little fuzzy.

Also, I want to be able to check the path of the extract.bat file itself and test to see if the string provided in the input is a fully qualified path or a relative path to the current directory, and then regardless of the input construct a fully qualified path as a possible output to pass to another subroutine.

Any advice or hints or input on this script would be greatly appreciated.
Quote

:EXPAND FILEPATH to parse
if "FILEPATH"=="" goto :JUMPOUT
for /f "tokens=1* delims=\" %%a in ("%FILEPATH%") do (
echo. %%a
set FILEPATH=%%b
goto :EXPAND <-------------------- this won't work!
)

:JUMPOUT

Sounds like you should study the FOR DOCUMENTATION... type FOR /? at the prompt... Look for the variable modifiers.

to extract the path from a fully qualified path and filename is quite simple

Code: [Select]for /f "delims=" %%A in ("C:\folder1\folder2\folder3\filename.ext") do (
echo drive %~dA
echo path %~pA
echo name %~NA
echo extension %~xA
)

Code: [Select]drive C:
path \folder1\folder2\folder3\
name filename
extension .ext
Quote from: Salmon Trout on May 05, 2010, 12:37:21 PM
Sounds like you should study the FOR documentation... type FOR /? at the prompt... Look for the variable modifiers.

to extract the path from a fully qualified path and filename is quite simple

Code: [Select]for /f "delims=" %%A in ("C:\folder1\folder2\folder3\filename.ext") do (
echo drive %%~dA
echo path %%~pA
echo name %%~nA
echo extension %%~xA
)

Code: [Select]drive C:
path \folder1\folder2\folder3\
name filename
extension .ext


Discussion

No Comment Found