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

jporkkahtc

  • Senior Community Member
  • Posts: 2433
  • Hero Points: 202
  • 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: 2679
  • Hero Points: 143
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: 2433
  • Hero Points: 202
  • 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: 2679
  • Hero Points: 143
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: 2433
  • Hero Points: 202
  • 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;
}