Jump to content
Macro Express Forums

Creating a file list with wildcards


Recommended Posts

I'm stuck with the following problem:

 

I am trying to create a text file that lists, in order, all files in a given directory, that correspond to a certain file mask.

 

For instance:

I have a directory named S:\score4\xxxx.

In that directory reside a number of files AT THE MOMENT that coincide with this file mask: P*.MUS. (So this could include P100.MUS, P101.MUS, P102.MUS, etc.)

In the same directory also reside AT THE MOMENT files that coincide with this file mask: S*.MUS. (So this could include S128A.MUS, S128B.MUS, S129A.MUS, etc.)

I would like to generate TWO text files that list the files according to the specified file mask.

 

For the moment, I get the "P*.MUS" files with this:

 

>>

Repeat with Folder S:\score4\xxxx

If Variable %fullList% Contains ".MUS"

And

If Variable %fullList% Contains "P"

Variable Modify String %makeFullList%: Append Text String Variable (%fullList%)

Variable Modify String %makeFullList%: Append Text String Variable (%T13%)

Variable Modify String %makeFullList%: Append Text String Variable (%T10%)

End If

End Repeat

Variable Modify String: Save %makeFullList% to "S:\Score4\xxxx\inmain.txt"

<<

 

I should explain that %T13% and %T10% append carriage return / line feed.

 

Next, I attempted to get the "S*.MUS" files with this:

 

>>

Repeat with Folder S:\score4\xxxx

If Variable %fullList% Contains ".MUS"

And

If Variable %fullList% Contains "S"

Variable Modify String %makeFullList%: Append Text String Variable (%fullList%)

Variable Modify String %makeFullList%: Append Text String Variable (%T13%)

Variable Modify String %makeFullList%: Append Text String Variable (%T10%)

End If

End Repeat

Variable Modify String: Save %makeFullList% to "S:\Score4\xxxx\inapp.txt"

<<

 

I quickly realized that this includes all the P*.MUS files as well, because "S" is also contained in the extension "MUS." So that didn't work. I modified the program for the "S*.MUS" files accordingly:

 

>>

Repeat with Folder S:\score4\xxxx

If Variable %appList% Contains ".MUS"

And

If Variable %appList% Does not Contain "P"

Variable Modify String %makeAppList%: Append Text String Variable (%appList%)

Variable Modify String %makeAppList%: Append Text String Variable (%T13%)

Variable Modify String %makeAppList%: Append Text String Variable (%T10%)

End If

End Repeat

Variable Modify String %makeAppList%: Trim

Variable Modify String: Save %makeAppList% to "S:\Score4\xxxx\inapp.txt"

<<

 

Now, that does give me the text files I need - but is decidedly NOT the smart way to proceed; the filenomenclature / file masks can always be different. Instead of "P*.MUS" files I could easily have "N*.MUS" files etc. In that case, the macro won't work; I would like to be able to indicate the file mask somehow (such as "X*A.MU?", for instance), and have the text file created accordingly.

 

In DOS (i.e. CMD.EXE), you can easily create such text files in this manner:

 

dir p*.mus/b/-p/-w>inmain.txt

dir s*.mus/b/-p/-w>inapp.txt

 

and finished. Change the wildcards to anything at all, and the file list is created.

 

How can this be accomplished within MacroExpress?

 

I look forward to hearing some ideas! And thank you in advance!

Link to comment
Share on other sites

What's wrong with using the DOS method you describe, but prompt for the specific criteria you need to apply, as in:

Variable Set String %tChar1%: Prompt
If Variable %tChar1% Equals ""
 Macro Return
End If
Program Launch: "cmd.exe" (Normal)
Parameters: /c dir %temp%\%tChar1%*.mus /b /-p /-w >%temp%\inmain.txt

<VARIABLE SET STRING Option="\x01" Destination="%tChar1%" Prompt="Enter character to filter against" Mask="FALSE" OnTop="FALSE" Left="Center" Top="Center" Monitor="0"/>
<IF VARIABLE Variable="%tChar1%" Condition="\x00" IgnoreCase="FALSE"/>
<MACRO RETURN/>
<END IF/>
<PROGRAM LAUNCH Path="C:\\Windows\\System32\\cmd.exe" Mode="\x00" Parameters="/c dir %temp%\\%tChar1%*.mus /b /-p /-w >%temp%\\inmain.txt" Default_Path="TRUE" Wait="1" Get_Console="FALSE"/>

Link to comment
Share on other sites

I would do it this way:

Variable Set to ASCII Char 10 to %LF% // Set linefeed
Variable Set to ASCII Char 13 to %CR% // Set Carrige Return
Repeat with Folder S:\score4\ // PLow thru the folder
 Variable Set From File path // Get teh file extension
 Variable Modify String: Copy a substring in %File%, starting at 1 and 1 characters long to %Char% // Get the first character
 If Variable %Ext% Equals ".MUS" // If the extension is what we want
   If Variable %Char% Equals "P" // And the first letter is right
     Variable Modify String %P Out%: Append Text (%File%%CR%%LF%) // Append the output string accumulator
   End If
   If Variable %Char% Equals "S"
     Variable Modify String %S Out%: Append Text (%File%%CR%%LF%) // Append the output string accumulator
   End If
 End If
End Repeat
Variable Modify String: Save %P Out% to "S:\Score4\P List.txt" // Dump the P output to file
Variable Modify String: Save %S Out% to "S:\Score4\S List.txt" // Dump the S output to file

 

<VARIABLE SET TO ASCII CHAR Value="10" Destination="%LF%" _COMMENT="Set linefeed"/>
<VARIABLE SET TO ASCII CHAR Value="13" Destination="%CR%" _COMMENT="Set Carrige Return"/>
<REPEAT WITH FOLDER Path="S:\\score4\\" OnlyFiles="TRUE" Destination="%File%" FullPath="FALSE" ProcSubfolders="FALSE" _COMMENT="PLow thru the folder"/>
<VARIABLE SET FROM FILE Filename="%File%" Option="\x01" Extension="%Ext%" Expand="FALSE" Flags="\x08" _COMMENT="Get teh file extension"/>
<VARIABLE MODIFY STRING Option="\x09" Destination="%Char%" Variable="%File%" Start="1" Count="1" NoEmbeddedVars="FALSE" _COMMENT="Get the first character"/>
<IF VARIABLE Variable="%Ext%" Condition="\x00" Value=".MUS" IgnoreCase="TRUE" _COMMENT="If the extension is what we want"/>
<IF VARIABLE Variable="%Char%" Condition="\x00" Value="P" IgnoreCase="TRUE" _COMMENT="And the first letter is right"/>
<VARIABLE MODIFY STRING Option="\x06" Destination="%P Out%" Value="%File%%CR%%LF%" NoEmbeddedVars="FALSE" _COMMENT="Append the output string accumulator"/>
<END IF/>
<IF VARIABLE Variable="%Char%" Condition="\x00" Value="S" IgnoreCase="TRUE"/>
<VARIABLE MODIFY STRING Option="\x06" Destination="%S Out%" Value="%File%%CR%%LF%" NoEmbeddedVars="FALSE" _COMMENT="Append the output string accumulator"/>
<END IF/>
<END IF/>
<END REPEAT/>
<VARIABLE MODIFY STRING Option="\x11" Destination="%P Out%" Filename="S:\\Score4\\P List.txt" Strip="FALSE" NoEmbeddedVars="FALSE" _COMMENT="Dump the P output to file"/>
<VARIABLE MODIFY STRING Option="\x11" Destination="%S Out%" Filename="S:\\Score4\\S List.txt" Strip="FALSE" NoEmbeddedVars="FALSE" _COMMENT="Dump the S output to file"/>

Link to comment
Share on other sites

Thank you, Paul, Cory, Samrae!

 

I've looked at your code, Cory, and I understand where you're going with it; it would still take quite a bit of effort, it seems to me, to allow wildcard entries of any kind, such as, "create a file list of all files with the following file mask: '?PA*B.MU?'" - an otherwise perfectly legal instruction in CMD: and the object, of course, is that this would have to be robust enough to cover ANY circumstance.

 

Of course, absent my ability to solve this directly in Macro Express, I have resorted to calling CMD to obtain the result. My process therefore coincides with Paul's solution, except that I prefer to do this unseen in the background - so I call CMD "Hidden," not "Normal." I do get the desired effect therefore in this manner:

 

>>

Variable Set String %fileMaskMainFiles%: Prompt

Variable Set String %fileMaskMainFiles% to "dir %fileMaskMainFiles%/b/-p/-w>inmain.txt"

Program Launch: "CMD.EXE" (Hidden)

Parameters: /C "%fileMaskMainFiles%"

<<

 

And finished. I hope you don't mind the query anyway; I'm always interested in finding ways to do things directly in ME, rather than having to rely on calls to other applications. I've kept Cory's solution as a springboard, in case I want to return to it in the future... Anyhow, thanks again - this is good enough for me.

Link to comment
Share on other sites

You can also use Paul’s and use the console output option. This will return the results to a variable. Then if were my I’d split it into an array, determine the start and end to your list then join just those elements into my output. Also there are switches in the DIR command like /b for “Bare” to eliminate all the header and summary.

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...