Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

calling an exe and its input and target files

by amateur_programmer54 (Initiate)
on Nov 09, 2012 at 03:58 UTC ( #1003055=perlquestion: print w/replies, xml ) Need Help??

amateur_programmer54 has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I am trying to write a small Perl script to run an executable file. I want the .exe file to work on the entire contents of a data directory. I’d like to be able to run the final perl program on machines that run Windows XP or Windows 7.

The “System” command doesn’t seem to work for what I am doing and I must include both input and target file names and paths after the executable file name and I am attempting to mix paths with a variable from an array.

The program converts .MID files to .CSV files when run stand alone. Once I have edited the .CSV file I will convert it back to .MID format with another similar file.

The syntax I would use after a command prompt to convert one file at a time, is as follows:

C:\directory\midicsv.exe c:\path\input_file.mid c:\path\target_file.csv

I would like to keep the names of “input_file” and “target_file” the same other than the extension. “Input_file” may be a name that includes the character _

I am trying to convert files in batches. In order to do that I am capturing all the file names in a given data directory to an array, and was hoping to use the file names saved in the array as part of the input_file name string.

Below is what I have written so far. I am getting the following error: “C:midicsvmidicsv.exe’ is not recognized as an internal or external command.” What am I doing wrong?

Can I mix an array variable with a Path? If so can you give me the proper syntax? If not, how can I insert the filenames after the name of the .exe I am calling?

Can I rename all the files to similar names ending in .CSV, and establish an output path to a different directory?

my $directory = 'c:\midicsv\newmidi'; opendir(DIR,$directory); my @files = readdir(DIR); closedir(DIR); #the following line is not truly necessary but I left it in so I could + see that the file names were being captured. foreach(@files){ print $_,"\n";} #this is the line giving me trouble, foreach(@files) {system("c:\midicsv\midicsv.exe", "c:\midicsv\newmidi\ +$_", "c:\midicsv\newcsv\$_");}

Replies are listed 'Best First'.
Re: calling an exe and its input and target files
by NetWallah (Canon) on Nov 09, 2012 at 04:50 UTC
    Inside "double quotes", the "\" character (which is the 'escape' character) needs to be escaped, using "\\".

    So, your foreach should read:

    foreach(@files) { system("c:\\midicsv\\midicsv.exe", "c:\\midicsv\\newmidi\\$_", "c:\ +\midicsv\\newcsv\\$_") }
    This shows the mixing of a variable with a path, answering your first question.

    For renaming files, you need to separate the pieces of the name, and replace the last piece.
    This is typically done using regular expressions, but you can also use "split".

    Here is a simplified example of replacing a trailing "csv" with "mid":

    my $file = "C:\\temp\\somefile.csv"; my $midfile = $file; # copy it $midfile =~ s/\.csv$/.mid/i;

                 "By three methods we may learn wisdom: First, by reflection, which is noblest; Second, by imitation, which is easiest; and third by experience, which is the bitterest."           -Confucius


      Its taken a week, and I've got it to

      -capture the file names

      -Ignore the directory makers "." and ".."

      -Run the .exe file and convert the files named in the array

      -rename all the files in the director from .mid to .csv

      Then it took about 2 minutes to write and debug the reverse process.

      I haven't written code since 1979, when I was writing in FORTRAN, using punch-cards and, running the prgrams on an IBM System 370 mainframe. Its been great fun.<\P>

      now on to the more daunting task of editing the CSV files so they will work properly when converted back to MIDI format and are used with a finicky biofeedback program that teaches people to sing of all things<\P>

      Thanks again to all the monks who gave input.<\P>

Re: calling an exe and its input and target files
by Anonymous Monk on Nov 09, 2012 at 04:56 UTC
Re: calling an exe and its input and target files
by space_monk (Chaplain) on Nov 09, 2012 at 08:16 UTC
    Use some of the CPAN libraries to perform filename manipulation.
    A Monk aims to give answers to those who have none, and to learn from those who know more.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1003055]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2020-10-24 21:09 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (247 votes). Check out past polls.