Author Topic: Recursive copy  (Read 120 times)

Offline Sam.

  • The moose man
  • Planewalker
  • *****
  • Posts: 70
  • Gender: Male
    • Classic Adventures Homepage
Recursive copy
« on: January 16, 2018, 01:12:41 PM »
Is there a command that can recurse through subdirectories to copy files to a given destination (like ~override~), or will I have to rig something up with GET_DIRECTORY_ARRAY, ACTION_BASH_FOR, and COPY?
"Ok, I've just about had my FILL of riddle asking, quest assigning, insult throwing, pun hurling, hostage taking, iron mongering, smart-arsed fools, freaks, and felons that continually test my will, mettle, strength, intelligence, and most of all, patience! If you've got a straight answer ANYWHERE in that bent little head of yours, I want to hear it pretty damn quick or I'm going to take a large blunt object roughly the size of Elminster AND his hat, and stuff it lengthwise into a crevice of your being so seldom seen that even the denizens of the nine hells themselves wouldn't touch it with a twenty-foot rusty halberd! Have I MADE myself perfectly CLEAR?!"
-- <CHARNAME> to Portalbendarwinden

Offline Sam.

  • The moose man
  • Planewalker
  • *****
  • Posts: 70
  • Gender: Male
    • Classic Adventures Homepage
Re: Recursive copy
« Reply #1 on: January 16, 2018, 10:04:23 PM »
[shrugs] My solution:
Code: [Select]
//////////////////////////////////////////////////////////////////////////////////
///////////////////////////      ps_recursive_copy      ///////////////////////////
////////////////////////////          v0.0.1          ////////////////////////////
//////////////////////////// Copyright (c) 2018 Sam.  ////////////////////////////
////////////////////////////          WTFPL           ////////////////////////////
//////////////////////////////////////////////////////////////////////////////////


//////////////////////////////////////////////////////////////////////////////////
////////////////////////    ps_recursive_copy    /////////////////////////
//////////////////////////////////////////////////////////////////////////////////
// This is a WeiDU action function that will recursively search into a parent directory, copying files to a destination directory.
//   INT_VAR WarnOnOverwrite can be 1 to print a warning when a file in the destination directory will be overwritten, 2 to warn when
//     a file already exists in the destination game, or 0 to print no warnings.
//   STR_VAR ParentDir is a string containing the name of the parent directory that will be recursively searched.
//   STR_VAR ChildDirRegex is a string containing the RegEx of child directories to search within ParentDir
//   STR_VAR FileRegex is a string containing the RegEX of files to match for the COPY_LARGE.
//   STR_VAR DestinationDir is a sting containing the name of the destination directory any matching files will be copied into.
//   This function returns the "Count" of files copied to the destination directory.
//////////////////////////////////////////////////////////////////////////////////
DEFINE_ACTION_FUNCTION ps_recursive_copy INT_VAR WarnOnOverwrite = 0 STR_VAR ParentDir = "" ChildDirRegex = "" FileRegex = "^.+$" DestinationDir = "override" RET Count BEGIN
  SILENT
  OUTER_SET Count = 0
  ACTION_BASH_FOR ~%ParentDir%~ ~%FileRegex%~ BEGIN
    ACTION_IF (WarnOnOverwrite > 0) AND (FILE_EXISTS ~%DestinationDir%/%BASH_FOR_FILE%~) BEGIN
      WARN ~%BASH_FOR_FILE% already exists in %DestinationDir% and will be overwritten.~
    END ELSE ACTION_IF (WarnOnOverwrite > 1) AND (FILE_EXISTS_IN_GAME ~%BASH_FOR_FILE%~) BEGIN
  WARN ~%BASH_FOR_FILE% already exists in the game and will be overwritten.~
END
    SILENT
    COPY_LARGE ~%BASH_FOR_FILESPEC%~ ~%DestinationDir%~
OUTER_SET Count += 1
  END
  ACTION_CLEAR_ARRAY ChildDir
  GET_DIRECTORY_ARRAY ChildDir ~%ParentDir%~ ~%ChildDirRegex%~
  ACTION_PHP_EACH ChildDir AS dirfrom => dirC BEGIN
    LAF ps_recursive_copy INT_VAR WarnOnOverwrite STR_VAR ParentDir = EVAL "%dirC%" ChildDirRegex FileRegex DestinationDir RET CountN = %Count% END
    OUTER_SET Count += CountN
  END
  VERBOSE
END

//////////////////////////////////////////////////////////////////////////////////
///////////////////////////    ps_recursive_search    ////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
// This is a WeiDU action function that will recursively search into a parent directory, and report how many files that match the given RegEX are found.
//   STR_VAR ParentDir is a string containing the name of the parent directory that will be recursively searched.
//   STR_VAR ChildDirRegex is a string containing the RegEx of child directories to search within ParentDir.  ParentDir is always searched.
//   STR_VAR FileRegex is a string containing the RegEX of files to match.
//   This function returns the "Count" of files found matching the given RegEX.
//////////////////////////////////////////////////////////////////////////////////
DEFINE_ACTION_FUNCTION ps_recursive_search STR_VAR ParentDir = "" ChildDirRegex = "" FileRegex = "^.+$" RET Count BEGIN
  OUTER_SET Count = 0
  ACTION_BASH_FOR ~%ParentDir%~ ~%FileRegex%~ BEGIN
OUTER_SET Count += 1
  END
  ACTION_CLEAR_ARRAY ChildDir
  GET_DIRECTORY_ARRAY ChildDir ~%ParentDir%~ ~%ChildDirRegex%~
  ACTION_PHP_EACH ChildDir AS dirfrom => dirC BEGIN
    LAF ps_recursive_search STR_VAR ParentDir = EVAL "%dirC%" ChildDirRegex FileRegex RET CountN = %Count% END
    OUTER_SET Count += CountN
  END
END
I can't guarantee it is 100% bug free, and I'm open to improvement suggestions.  It could certainly be I'm way overthinking this...
"Ok, I've just about had my FILL of riddle asking, quest assigning, insult throwing, pun hurling, hostage taking, iron mongering, smart-arsed fools, freaks, and felons that continually test my will, mettle, strength, intelligence, and most of all, patience! If you've got a straight answer ANYWHERE in that bent little head of yours, I want to hear it pretty damn quick or I'm going to take a large blunt object roughly the size of Elminster AND his hat, and stuff it lengthwise into a crevice of your being so seldom seen that even the denizens of the nine hells themselves wouldn't touch it with a twenty-foot rusty halberd! Have I MADE myself perfectly CLEAR?!"
-- <CHARNAME> to Portalbendarwinden

Offline The Imp

  • Planewalker
  • *****
  • Posts: 228
  • Gender: Male
Re: Recursive copy
« Reply #2 on: January 17, 2018, 03:49:34 AM »
Is there a command that can recurse through subdirectories to copy files to a given destination (like ~override~), or will I have to rig something up with GET_DIRECTORY_ARRAY, ACTION_BASH_FOR, and COPY?
I might have missed the whole reason... but why can't you just say ? In the same .tp2 file:
BEGIN ~modcomponent 2~ (only allow install if component 1 is installed)
COPY ~modfolder/component1~ ~modfolder/component2~
//patch functions
PATCH_IF ...
COPY ~modfolder/component2~ ~override~
« Last Edit: January 17, 2018, 03:51:38 AM by The Imp »

 

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Name: Email:
Verification:
Type the letters shown in the picture
Listen to the letters / Request another image
Type the letters shown in the picture:
What color is grass?:
What is the seventh word in this sentence?:
What is five minus two (use the full word)?: