Jump to content
Macro Express Forums

Parse A File Path


Cory

Recommended Posts

I want to take a path string of a file and break it into several string variables. E.g., from “\\server\share\dir1\dir2\dir3\dir4\last, first 333-22-4444.pdf” I want to get dir2, dir4, last, first and, SSN as separate string variables. (333-22-4444) is the SSN. The number of levels of folders will be consistent and things like the length of the SSN will always be the same but dir1-4 will vary and might have spaces and punctuation. What is the best practice here?

 

I can think of a couple ways to do this and plan on checking out the samples on the website but thought I would ask here first in case some old sage had a quick answer for me. I don’t need a detailed response just a general idea which way to go.

 

Thanks!

Link to comment
Share on other sites

I have two answers for this:

 

If you DO NOT have the PGM Functions Library, you can use the Variable Set from Path command to initially parse the string into drive, path, name, extension. And then hack away within a loop to parse the path.

 

If you DO own the PGM Functions Library, you can use the {String - Parse} function to separate the string into different variables by parsing it on the "\" character in a single call.

Link to comment
Share on other sites

Thanks. I'm looking at the PGM add in solution but I'm a bit confused by it so far. I opened the manual for it and found it was 308 pages long! I looked at that function but there were som undefined terms in there so I guess I need to look back at some of the preceding 300 pages to see what I missed.

Link to comment
Share on other sites

Hi, Cory,

I'm enthusiastic about the "BasicME" approach ("proof of concept"!), even though it is only for fun and not serious application; I took your case as an example and stimulus to get "InStr$" and "StringSplit$" working; see example macro below (needs v 3.3s)

Best, Randall

// Parsing a filename Example using "Basic ME" ! -Randall  <randallc@ozemail.com.au> Mainly for fun; still lots of bugs, I imagine; not for serious work? (Yo Use the mex file from Forum))

Variable Set String %T3% "\\server\share\dir1\dir2\dir3\dir4\last, first 333-22-4444.pdf"

// Parse to Env vars as named;  Quotes around any commas for Basic

Variable Set String %T1% "StringSplit$["%T3%",\,Directories]"

Macro Run: {_Basic ME Lite1}

// Find last ("characters between rightmost  "\" and rightmost  ", ")

Variable Set String %T1% "BtwCharMid$["%T3%","\",-1,0,", ",-1,0]"

Macro Run: {_Basic ME Lite1}

Variable Set String %T4% "%AnswerString%"

// Find first ("characters between rightmost  ", " and rightmost  " ")

Variable Set String %T1% "BtwCharMid$["%T3%",", ",-1,0," ",-1,0]"

Macro Run: {_Basic ME Lite1}

Variable Set String %T5% "%AnswerString%"

// Find code (assume fixed length)

Variable Set String %T1% "RelMid$["%T3%",-15,-5]"

Macro Run: {_Basic ME Lite1}

Variable Set String %T2% from Environment Variable

Text Box Display: StringDisplay2

<REM2:Parsing a filename Example using "Basic ME" ! -Randall  <randallc@ozemail.com.au> Mainly for fun; still lots of bugs, I imagine; not for serious work? (Yo Use the mex file from Forum))><TVAR2:03:01:\\server\share\dir1\dir2\dir3\dir4\last, first 333-22-4444.pdf><REM2:Parse to Env vars as named;  Quotes around any commas for Basic ><TVAR2:01:01:StringSplit$["%T3%",\,Directories]><MACRUN2:{_Basic ME Lite1}><REM2:Find last ("characters between rightmost  "\" and rightmost  ", ")><TVAR2:01:01:BtwCharMid$["%T3%","\",-1,0,", ",-1,0]><MACRUN2:{_Basic ME Lite1}><TVAR2:04:01:%AnswerString%><REM2:Find first ("characters between rightmost  ", " and rightmost  " ")><TVAR2:01:01:BtwCharMid$["%T3%",", ",-1,0," ",-1,0]><MACRUN2:{_Basic ME Lite1}><TVAR2:05:01:%AnswerString%><REM2:Find code (assume fixed length)><TVAR2:01:01:RelMid$["%T3%",-15,-5]><MACRUN2:{_Basic ME Lite1}><TVAR2:02:11:_%Parameter12%_Display><TBOX4:T:1:000239000254000532000391:000:StringDisplay2CODE=%AnswerString%
T3=%T3%
T4=%T4%
T5=%T5%
_%Parameter12%_Display=
%T2%>

CODE=333-22-4444

T3=\\server\share\dir1\dir2\dir3\dir4\last, first 333-22-4444.pdf

T4=last

T5=first

_Directories_Display=

Environment Variable Answers for StringSplit$ Directories

_Directories_0=7

_Directories_1=`

_Directories_2=server

_Directories_3=share

_Directories_4=dir1

_Directories_5=dir2

_Directories_6=dir3

_Directories_7=dir4

_Directories_8=last, first 333-22-4444.pdf

EDIT posted 3.4s; fixed for quotes around strings with commas

Edited by randallc
Link to comment
Share on other sites

Here is a sample using the {String - Parse} function:

 

Variable Set String %T1% "\\server\share\dir1\dir2\dir3\dir4\last, first 333-22-4444.pdf"
Variable Set String %T2% "\"
Write Registry String: "ParameterString1"
Write Registry String: "ParameterString2"
Macro Run: { String - Parse }
Read Registry Integer: "ReturnInteger1"

Activate or Launch: "notepad" OR "notepad.exe"
Wait For Window Title: "notepad"

Repeat with Variable using %N1%
 Read Registry String: "Token%N2%"
 Text Type: Token %N2% = %T1%<ENTER>
Repeat End


<TVAR2:01:01:\\server\share\dir1\dir2\dir3\dir4\last, first 333-22-4444.pdf><TVAR2:02:01:\><REGWSTR:1:HKEY_CURRENT_USER\Software\Professional Grade Macros\Parameters\ParameterString1><REGWSTR:2:HKEY_CURRENT_USER\Software\Professional Grade Macros\Parameters\ParameterString2><MACRUN2:{ String - Parse }><REGRINT:1:HKEY_CURRENT_USER\Software\Professional Grade Macros\Parameters\ReturnInteger1><REM2:><LAUNCHYES3:0:0112notepad<LAUNCH:notepad.exe><WAITWIN2:000010:000000:notepad><REM2:><REP3:05:000001:000001:0001:1:02:><REGRSTR:1:HKEY_CURRENT_USER\Software\Professional Grade Macros\Parsed Tokens\Token%N2%><TEXTTYPE:Token %N2% = %T1%<ENTER>><ENDREP>

All that you need to do is set a Registry value with the string-to-be-parsed and another Registry value to the character (or string) used as the parsing delimiter. Now call the {String - Parse} function, then read the number of parsed strings (tokens) that were generated.

Link to comment
Share on other sites

  • 2 weeks later...

What is "Basic ME"? I broke down and bought the PGM library thingy so I'll mess with that first. Thanks for the example Floyd. Sometimes I just need to see it in motion in order to get it.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...