Process Files in a Batch File or Script

PowerGREP does not have the ability to execute a command or external program on each file that it finds. But with PowerGREP you can easily collect files into a batch file or script that executes those commands.

For this example, assume you purchased some music that you downloaded in FLAC format, for best quality. Now you want to put this music onto a pocket player in MP3 format. You want to use FFmpeg to perform the conversion. This example shows how you can easily create a batch file with PowerGREP that runs FFmpeg on each of your FLAC files to convert it into an MP3 file.

You can find this action in the PowerGREP5.pgl library as "Batch file to convert audio files to MP3 using FFmpeg".

  1. Mark the folder(s) containing the FLAC files in the File Selector.
  2. Enter *.flac into the "include files" box.
  3. Select a file format configuration such as "proprietary formats" that searches through audio files. Though we don't use this ability in this example, PowerGREP recognizes the FLAC file format and can search through metadata in FLAC files. The "audio file meta tags" file format needs to be enabled. Otherwise PowerGREP would skip all FLAC files.
  4. Set "archive formats to search inside" to "None". FFmpeg can't use paths that point to files inside archives.
  5. Start with a fresh action.
  6. Set the action type to "file or folder name collect".
  7. Leave the Search box blank to convert all FLAC files. If you enter a search term, only files containing that search term in their file name will be collected into the batch file.
  8. In the Collect box, enter a command line to FFmpeg. ffmpeg -i "%FILE%" "E:\%FILENAMENOEXT%.mp3" puts all MP3 files into the root of the E:\ drive. If your FLAC files are in a folder structure you can preserve that with path placeholders. If each album has its own folder, use "E:\%FOLDER<1\FILENAMENOEXT%.mp3". If each artist has a folder and each album a folder inside that, use "E:\%PATH<2\FILENAMENOEXT%.mp3".
  9. Set "target file creation" to "save results into a single file" and set "target file location" to the full path to a .bat file for PowerGREP to create.
  10. Set "target file text encoding" to your computer's default code page, which is probably Windows 1252.
  11. Set "target line break style" to "Windows (CR LF)".
  12. Leave "order of matches from different files" set to "no particular order". The order in which the files are converted is irrelevant.
  13. Set the backup options for the .bat file as you like them.
  14. Click the Collect button to create the batch file.
  15. Double-click one of the highlighted matches on the Results panel to open the batch file in PowerGREP's built-in editor to check that everything looks correct.
  16. Open a command prompt from the Windows Start menu and execute the batch file. Or just double-click the batch file in Windows Explorer.

In many situations, you may need to collect some code before and after the block of statements that process all the files. Your script may need a shebang or import statements. Or it may need to do some extra work before and after all the files are processed. You can do this with the "collect headers and footers" checkbox. After turning this on, select "target file header" and enter the block of code that should go before the file statements. Then select "target file footer" to enter the code that should go after the file statements.