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

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
This is a very common type of task (applying some process to each item in a list of files), and it can actually be generalized to a broader set of tasks (applying some shell command-line operation to each item in a list of strings, and in many cases, needing a modified version of the input string to serve as an "output string" on each iteration). In an average work week, I might need to do something like this many times a day.

So, many years ago, I wrote shloop -- execute shell command on a list, and I've been using it every since. In your case, you'd do something like this, which assumes that your system has the unix-style "ls" utility to list the contents of a directory (there's an 'ls.exe' available for MS-Windows systems), and that your "small perl script" simply takes the name of a text file as the sole command-line argument:

ls *.txt | shloop -e small_perl_script
Quite often, the situation involves creating a modified file from a source file, either by providing input and output arguments for a process, or by redirecting process output to a new file:
# supplying input and output args: "cmd inp.arg out.arg" ls *.txt | shloop -e small_perl_script -s '.txt$' # or, using redirection: "cmd inp.arg > out.arg" ls *.txt | shloop -e small_perl_script -r -s '.txt$'
shloop has grown over the years to include lots of bells and whistles, but one of the early things I wanted to be sure of was that it would work on MS-Windows shells as well as unix/linux ones. (Getting the Windows version of the linux "bash" shell is always an improvement over a "native" MS-DOS style shell, but shloop should work with cmd.exe, or command.exe or whatever you have.)


In reply to Re: How can I run an folder full of text files through a perl script? by graff
in thread How can I run an folder full of text files through a perl script? by Dr Manhattan

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (4)
    As of 2018-05-27 20:16 GMT
    Find Nodes?
      Voting Booth?