#Files: Filter by FileName

#Files: Filter by FileName

#Files: Filter by FileName

translate #複数ファイル: ファイル名フィルタ

Extracts only files that match the conditions from the files stored in File-type Data Items. The conditions can be specified as “equal to”, “contains”, “starts with”, or “ends with”. A case-insensitive filter can be specified. You can also store the remaining files that were not extracted.

Auto Step icon
Configs for this Auto Step
SelectConfA1
A1: Select FILE DATA for Original Files *
OptionConfB1
B1: Select OPERATOR for Matching Condition *
StrConfB2
B2: Set KEYWORDs for Matching Condition (Multi Rows: OR-Search) *#{EL}
StrConfB3
B3: To Limit, Set Max Number to Extract (eg: “1”)#{EL}
SelectConfC1
C1: Select FILE DATA that stores Extracted Files (update)
SelectConfC2
C2: Select FILE DATA that stores Extracted Files (append)
SelectConfC3
C3: Select FILE DATA that stores Not Extracted (update)
Script (click to open)

/**
 * Questetra Add-on: FILE名によるフィルタ
 * 
 * ファイル型データに格納された複数のファイルから、
 * 指定された文字列条件(equals / contains / startsWith / endsWith など)に基づいて
 * マッチするファイルのみを抽出し、別のファイル型データに格納する。
 * 
 * 対応演算子: 大文字小文字区別あり・なしを指定可能。
 * 
 * @engine GraalJS standard mode (engine-type: 3)
 * @license MIT License
 * @lastModified 2025-06-10
 */


//////// START "main()" /////////////////////////////////////////////////////////////////
main();
function main(){ 

//// == Config Retrieving / 工程コンフィグの参照 ==
const filesPocketOriginal = configs.getObject( "SelectConfA1" );  /// REQUIRED ///////////////
  let filesOriginal       = engine.findData( filesPocketOriginal );  // java.util.ArrayList
      // java.util.List of com.questetra.bpms.core.event.scripttask.QfileView
  if( filesOriginal     === null ) {
    throw new Error( "\n AutomatedTask UnexpectedFileError:" +
                     " No File {A1} is attached \n" );
  }else{
    engine.log( " AutomatedTask FilesArray {A1}: " +
                filesOriginal.size() + " files" );
  }
const strOperator         = configs.get      ( "OptionConfB1" ); /// REQUIRED (SELECT_ITEM)
  // "equals",  "conains",  "stratswith",  "endswith",
  // "equals2", "conains2", "startswith2", "endswith2" (2:ignore case)
const strKeywords         = configs.get      ( "StrConfB2" );    /// REQUIRED
  if( strKeywords       === "" ){
    throw new Error( "\n AutomatedTask ConfigError:" +
                     " Config {B2: String} is empty \n" );
  }

/** @type {number} 最大抽出数(指定なしの場合は無制限) */
const strMax              = configs.get      ( "StrConfB3" );     // not required
const numMax              = isNaN ( parseInt(strMax,10) ) ?
                            Infinity : // defalut
                            parseInt(strMax,10);
  engine.log( " #of Max: " + numMax );

/** @type {java.util.ArrayList} 抽出ファイル格納用 */
const filesPocketUpdate   = configs.getObject( "SelectConfC1" );  // NotRequired /////////////
  let filesUpdate         = new java.util.ArrayList();

/** @type {java.util.ArrayList} 追加格納用(既存に追加) */
const filesPocketAppend   = configs.getObject( "SelectConfC2" );  // NotRequired /////////////
  let filesAppend         = new java.util.ArrayList();
  if ( filesPocketAppend !== null ) {
    if ( engine.findData( filesPocketAppend ) !== null ) {
      filesAppend         = engine.findData( filesPocketAppend ); // java.util.ArrayList
      engine.log( " AutomatedTask FilesArray {C2}: " +
                  filesAppend.size() + " files" );
    }
  }

/** @type {java.util.ArrayList} 抽出されなかったファイル格納用 */
const filesPocketLeft     = configs.getObject( "SelectConfC3" );  // NotRequired /////////////
  let filesLeft           = new java.util.ArrayList();



//// == Data Retrieving / ワークフローデータの参照 ==
// (Nothing. Retrieved via Expression Language in Config Retrieving)



//// == Calculating / 演算 ==
const arrKeywords  = strKeywords.split( '\n' ).filter(k => k !== "");

if ( strOperator === "equals" ) {
  loopInput:
  for ( let i = 0; i < filesOriginal.size(); i++ ) {
    const fileTmp = new com.questetra.bpms.core.event.scripttask.NewQfile(
      filesOriginal.get(i).getName(), 
      filesOriginal.get(i).getContentType(), 
      filesOriginal.get(i)
    );
    if ( filesUpdate.size() < numMax ){
      for ( let j = 0; j < arrKeywords.length; j++ ) {
        if ( filesOriginal.get(i).getName() == arrKeywords[j] ){
          filesUpdate.add ( fileTmp );
          filesAppend.add ( fileTmp );
          continue loopInput;
        }
      }
    }
    filesLeft.add ( fileTmp );
  }
} else if ( strOperator === "equals2" ) {
  loopInput:
  for ( let i = 0; i < filesOriginal.size(); i++ ) {
    const fileTmp = new com.questetra.bpms.core.event.scripttask.NewQfile(
      filesOriginal.get(i).getName(), 
      filesOriginal.get(i).getContentType(), 
      filesOriginal.get(i)
    );
    if ( filesUpdate.size() < numMax ){
      for ( let j = 0; j < arrKeywords.length; j++ ) {
        if ( filesOriginal.get(i).getName().toUpperCase() == arrKeywords[j].toUpperCase() ){
          filesUpdate.add ( fileTmp );
          filesAppend.add ( fileTmp );
          continue loopInput;
        }
      }
    }
    filesLeft.add ( fileTmp );
  }

} else if ( strOperator === "contains" ) {
  loopInput:
  for ( let i = 0; i < filesOriginal.size(); i++ ) {
    const fileTmp = new com.questetra.bpms.core.event.scripttask.NewQfile(
      filesOriginal.get(i).getName(), 
      filesOriginal.get(i).getContentType(), 
      filesOriginal.get(i)
    );
    if ( filesUpdate.size() < numMax ){
      for ( let j = 0; j < arrKeywords.length; j++ ) {
        if ( filesOriginal.get(i).getName().includes( arrKeywords[j] ) ){
          filesUpdate.add ( fileTmp );
          filesAppend.add ( fileTmp );
          continue loopInput;
        }
      }
    }
    filesLeft.add ( fileTmp );
  }

} else if ( strOperator === "contains2" ) {
  loopInput:
  for ( let i = 0; i < filesOriginal.size(); i++ ) {
    const fileTmp = new com.questetra.bpms.core.event.scripttask.NewQfile(
      filesOriginal.get(i).getName(), 
      filesOriginal.get(i).getContentType(), 
      filesOriginal.get(i)
    );
    if ( filesUpdate.size() < numMax ){
      for ( let j = 0; j < arrKeywords.length; j++ ) {
        if ( filesOriginal.get(i).getName().toUpperCase().includes( arrKeywords[j].toUpperCase() ) ){
          filesUpdate.add ( fileTmp );
          filesAppend.add ( fileTmp );
          continue loopInput;
        }
      }
    }
    filesLeft.add ( fileTmp );
  }

} else if ( strOperator === "startswith" ) {
  loopInput:
  for ( let i = 0; i < filesOriginal.size(); i++ ) {
    const fileTmp = new com.questetra.bpms.core.event.scripttask.NewQfile(
      filesOriginal.get(i).getName(), 
      filesOriginal.get(i).getContentType(), 
      filesOriginal.get(i)
    );
    if ( filesUpdate.size() < numMax ){
      for ( let j = 0; j < arrKeywords.length; j++ ) {
        if ( filesOriginal.get(i).getName().startsWith( arrKeywords[j] ) ){
          filesUpdate.add ( fileTmp );
          filesAppend.add ( fileTmp );
          continue loopInput;
        }
      }
    }
    filesLeft.add ( fileTmp );
  }

} else if ( strOperator === "startswith2" ) {
  loopInput:
  for ( let i = 0; i < filesOriginal.size(); i++ ) {
    const fileTmp = new com.questetra.bpms.core.event.scripttask.NewQfile(
      filesOriginal.get(i).getName(), 
      filesOriginal.get(i).getContentType(), 
      filesOriginal.get(i)
    );
    if ( filesUpdate.size() < numMax ){
      for ( let j = 0; j < arrKeywords.length; j++ ) {
        if ( filesOriginal.get(i).getName().toUpperCase().startsWith( arrKeywords[j].toUpperCase() ) ){
          filesUpdate.add ( fileTmp );
          filesAppend.add ( fileTmp );
          continue loopInput;
        }
      }
    }
    filesLeft.add ( fileTmp );
  }

} else if ( strOperator === "endswith" ) {
  loopInput:
  for ( let i = 0; i < filesOriginal.size(); i++ ) {
    const fileTmp = new com.questetra.bpms.core.event.scripttask.NewQfile(
      filesOriginal.get(i).getName(), 
      filesOriginal.get(i).getContentType(), 
      filesOriginal.get(i)
    );
    if ( filesUpdate.size() < numMax ){
      for ( let j = 0; j < arrKeywords.length; j++ ) {
        if ( arrKeywords[j] === "" ){
          engine.log( " AutomatedTask RuntimeWarning: Empty in Keywords config" );
          continue;
        }
        if ( filesOriginal.get(i).getName().endsWith( arrKeywords[j] ) ){
          filesUpdate.add ( fileTmp );
          filesAppend.add ( fileTmp );
          continue loopInput;
        }
      }
    }
    filesLeft.add ( fileTmp );
  }

} else if ( strOperator === "endswith2" ) {
  loopInput:
  for ( let i = 0; i < filesOriginal.size(); i++ ) {
    const fileTmp = new com.questetra.bpms.core.event.scripttask.NewQfile(
      filesOriginal.get(i).getName(), 
      filesOriginal.get(i).getContentType(), 
      filesOriginal.get(i)
    );
    if ( filesUpdate.size() < numMax ){
      for ( let j = 0; j < arrKeywords.length; j++ ) {
        if ( arrKeywords[j] === "" ){
          engine.log( " AutomatedTask RuntimeWarning: Empty in Keywords config" );
          continue;
        }
        if ( filesOriginal.get(i).getName().toUpperCase().endsWith( arrKeywords[j].toUpperCase() ) ){
          filesUpdate.add ( fileTmp );
          filesAppend.add ( fileTmp );
          continue loopInput;
        }
      }
    }
    filesLeft.add ( fileTmp );
  }

} else {
  throw new Error( "\n AutomatedTask RuntimeError:" +
                   " Unexpected OPERATOR \n" );
}


//// == Data Updating / ワークフローデータへの代入 ==
if ( filesPocketUpdate !== null ) {
  engine.setData( filesPocketUpdate, filesUpdate );
}
if ( filesPocketAppend !== null ) {
  engine.setData( filesPocketAppend, filesAppend );
}
if ( filesPocketLeft !== null ) {
  engine.setData( filesPocketLeft, filesLeft );
}

} //////// END "main()" /////////////////////////////////////////////////////////////////


/*
Notes:
- Use this when you want to extract a part of the files stored in the "File type data item".
    - When a Process token reaches the automatde step, files that match the conditions will be automatically extracted.
    - If to narrow down to image files only:
        - File name "ends with" `.png` or `.jpg`
    - If to pass "one PNG file" to the generation AI:
        - File name "ends with" `.png`
        - and Maximum number of extractions is `1`
- Also possible to store files that were not extracted.

Notes-ja:
- "ファイル型データ項目" に格納されているファイル群から、一部を抽出したい場合に利用します。
    - 案件が自動工程に到達した際、条件にマッチするファイルが自動的に抽出されます。
    - 画像ファイルだけに絞り込みたい場合の設定例:
        - ファイル名が `.png` もしくは `.jpg` で "終わる"
    - 生成AIに「1枚のPNGファイル」を渡したい場合の設定例:
        - ファイル名が `.png` で "終わる"
        - 最大抽出数は `1`
- 抽出されなかったファイル群を格納することも可能です。
*/

Download

warning Freely modifiable JavaScript (ECMAScript) code. No warranty of any kind.
(Installing Addon Auto-Steps are available only on the Professional edition.)

Notes

  • Use this when you want to extract a part of the files stored in the File-type Data Item.
    • When a Process token reaches this automated step, files that match the conditions will be automatically extracted.
    • Example settings if you wish to narrow down to image files only:
      • File name “ends with” .png or .jpg
    • Example settings if you wish to pass one PNG file to the generative AI:
      • File name “ends with” .png
      • and maximum number of extractions is 1
  • It is also possible to store files that were not extracted.

Capture

Appendix

  • B1: Select OPERATOR for Matching Condition
    • equals
    • equals (ignore case)
    • contains
    • contains (ignore case)
    • starts with
    • starts with (ignore case)
    • ends with
    • ends with (ignore case)

See Also

Scroll to Top

Discover more from Questetra Support

Subscribe now to keep reading and get access to the full archive.

Continue reading