Jump to content

How to process a list of files


Recommended Posts

Okay, I've RTFM'd and searched the forum to no avail. I am making a GUI frontend for a command line prog and I need to take a list of files (like when you go ctrl-A in a 'Select File' dialog) and process them one after the other until they are all done. Specifically, converting a list of graphics files from one format to another. I know how to do the GUI, the loop engine is what is baffling me. I have written a script to do single files, so the command operands are no problem. A rough sketch of the workflow goes like this:

; input box -> input file(s)

; input box -> output directory

; wait for "GO" button

; start loop

; get next filename

; RunWait (commandlineprog.exe <input filename> <outputdir/base filename.newextension>)

; If -> any more files?

; Then -> back to loop

; Else -> exit loop

; MsgBox "all done!"

It's the "get next filename...any more files" loop that is baffling me. Thanks for help.

Link to comment
Share on other sites

"get next filename" isnt a function

Yes, I know. I didn't write it as a command, just as a marker for the intended action. As in "somehow parse what the next file in line is so we can run it through the grinder" . As I read in the help there is no "FileGetNext" either, just "FileFindFirst" and "FileFindNext" which are search funtions, not file name processing functions, which is what I need. Perhaps what is needed is a way to push the filenames into an array and then simply step through that array in the loop. Hmmm...
Link to comment
Share on other sites

First of all, how you plan to get filenames (input box -> input file(s))? For example, it's simple to do with standard open dialog, it have option to multilselect files. Then you have to StringSplit them.

$ret = FileOpenDialog ( "Open", "init dir", "*.*", 4)
$aFN = StringSplit($ret, "|")

For $i = 2 to $aFN[0]
  $curFN = $aFN[1] & "\" & $aFN[$i]
  RunWait (...)
Next
Link to comment
Share on other sites

Most helpful lazycat, thank you thank you and thank you again. And yes, I planned on using a standard Open File Dialog, thanks. Next question, any way to extract the base name from... oop, stringsplit again, right? Something like:

$aBN = StringSplit ($curFN, ".")
$basename = $aBN[1]

I'll test this out and post a complete code if it works :(

Thanks all!

Link to comment
Share on other sites

Another idea...

I would use the MS-DOS command "DIR" with the switches "/B" and "/S" and send the results to a file. Then I would use FileReadLine() to open and read file. I would convert the "File Grinder" into a function and call it into action until the end-of-file is reached.

Link to comment
Share on other sites

Most helpful lazycat, thank you thank you and thank you again. And yes, I planned on using a standard Open File Dialog, thanks. Next question, any way to extract the base name from... oop, stringsplit again, right? Something like:

$aBN = StringSplit ($curFN, ".")
$basename = $aBN[1]

I'll test this out and post a complete code if it works :(

Thanks all!

<{POST_SNAPBACK}>

Actually, your idea is good, but it can cause problems.

I assume what you're expecting is something like this:

$a = StringSplit('c:\windows\filename.txt', '.')
$a[1] = 'c:\windows\filename'
$a[2] = 'txt'

Am I correct? This will work fine.. until you find a folder with a period in it (or a filename with more than one).

$a = StringSplit('c:\Documents and Settings\User.Account\filename.txt', '.')
$a[1] = 'c:\Documents and Settings\User'
$a[2] = 'Account\filename'
$a[3] = 'txt'

See the problem?

Your best bet, is to use a couple of functions. Here's what I've had success with.

Let's use your variable, $curFN, for the example.

First we get the location of the last period (StringInStr), then using StringLeft to pull out the pertinent information:

$curFN = 'My.Awesome.File.jpg'
$dotPos = StringInStr($curFN, '.', 0, -1) - 1
$baseFN = StringLeft($curFN, $dotPos)

msgbox(0,'',$baseFN)

See what I mean?

I also made up this one if you've got a full path, and want to extract the basename.

$curFN = 'C:\Windows\My.Awesome.File.jpg'
$dotPos = StringInStr($curFN, '.', 0, -1) - 1
$slashPos = StringInStr($curFN, '\', 0, -1)
$baseFN = StringLeft($curFN, $dotPos)
$baseFN = StringTrimLeft($baseFN, $slashPos)

msgbox(0,'',$baseFN)
Link to comment
Share on other sites

I find it easiest just to reverse it, that way you will always get the file name first, then you just reverse it again. Of course the method you use will vary each time depending on what information you want to retrieve, for me its usually the file type and the file name.

#Include <string.au3>

$a = StringSplit(_StringReverse('C:\Windows\My.Awesome.File.jpg'), '\')
$Name = StringTrimRight (_StringReverse($a[1]), 4)

Msgbox(4096,"Test",$Name)
Edited by Burrup

qq

Link to comment
Share on other sites

Saunders - Yes I was aware of the "more than one dot in the file name" problem and was putting that on my to-do list; Thanks for the help there.

Burrup - Quite the clever idea, I'll try that, too.

Boy, you'd think something like extracting the base name of a file would be a little easier huh? Maybe a function of this type would make a good feature request? It'd be nice (and very useful IMHO) to have like a more specified StringSplit-type function so one could return the basename, directory path, root directory, the filename alone, etc. with one function instead of the gymnastics demonstrated here. Very FINE gymnastics, I might add... Thanks all for your help, I'm almost ready to post code. When I RTFM'd trying to figure all this out, I remember reading through StringSplit and it's relations, but somehow missed the application.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...