Author Topic: Run a command on a bunch of files?  (Read 1079 times)

jporkkahtc

  • Senior Community Member
  • Posts: 2620
  • Hero Points: 210
  • Text
Run a command on a bunch of files?
« on: October 08, 2021, 10:12:44 PM »
How to?

I tried creating a file list, and "Repeat command".
But, this seems to only make sense for commands that take a filename as an argument?

I want to, for example, run convert_tabs2spaces on a bunch of files.

Dan

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 2906
  • Hero Points: 153
Re: Run a command on a bunch of files?
« Reply #1 on: October 09, 2021, 12:34:40 AM »
Look at the help on the for-all command.  I think that's what you want.

jporkkahtc

  • Senior Community Member
  • Posts: 2620
  • Hero Points: 210
  • Text
Re: Run a command on a bunch of files?
« Reply #2 on: October 09, 2021, 01:01:18 AM »
That runs a command on all lines in a buffer, not once on each of some set of files.

Dan

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 2906
  • Hero Points: 153
Re: Run a command on a bunch of files?
« Reply #3 on: October 09, 2021, 01:08:23 AM »
It operates on a buffer from the SlickEdit File Manager (File > File Manager, or dir or list command).

I thought it referenced fileman mode, but it's the help on for_each that does that.

If it's not a simple set of files like *.cpp, you could put them in a list file, and then run "dir @<listfilename>".  And in the buffer that comes back with the directory listing for each file, you could run "for-all convert_tabs2spaces".

jporkkahtc

  • Senior Community Member
  • Posts: 2620
  • Hero Points: 210
  • Text
Re: Run a command on a bunch of files?
« Reply #4 on: October 10, 2021, 05:41:42 PM »
for_all does just run the command on each line in the fileman, not on each file.

I was able to hack together a function to be used with for_all to do this.
Then I can do this on the slick cmd line:
Code: [Select]
for-all runmeonfile %F,convert_tabs2spaces
Code: [Select]
_command runmeonfile(_str file="", _str commandArg="") name_info(','VSARG2_MACRO|VSARG2_REQUIRES_MDI_EDITORCTL) // VSARG2_REQUIRES_FILEMAN_MODE
{
    temp_view_id := 0;
    orig_view_id := 0;

    _str delim, commandArgs;
    delim=','
    parse file with  filename (delim) commandToRun (delim) commandArgs;

    commandArg = commandToRun;

    int status=_open_temp_view(filename, temp_view_id, orig_view_id);
    if (status=='') status=0;
    if ( status)
    {
        say("FAILED open file: " filename);
        return 1;
    }

    typeless arg1=prompt(commandArg);
 
    select_cmd := "";
    cmdargs := "";
    args := "";
    parse arg1 with select_cmd args;
    index := find_index(select_cmd,PROC_TYPE|COMMAND_TYPE);
    #if 1
    status=call_index(cmdargs,index);
    activate_window(orig_view_id);

    say("buf:"filename", runcmd: " select_cmd", status='"status"'");
    #endif

    return 0;
}