Script Library

Sample scripts are provided as-is with no warranty of fitness for a particular purpose. These scripts are solely intended to demonstrate techniques for accomplishing common tasks. Additional script logic and error-handling may need to be added to achieve the desired results in your specific environment.


wildcard_rename.s

Download

Wildcard patterns are useful when you don't know the exact names of the files your script must process. However, there are certain situations where the use of wildcards is not appropriate. An obvious example is the task of renaming a file from an unknown name to a specific name. You can't have two files with the same name in the same folder so what happens if two files match the wildcard pattern?

Robo-FTP offers a method that gives script developers more precise control over the behavior of wildcard operations. Using the GETFILE or GETNEXTFILE command in a loop allows you to perform multiple logical steps on each file that matches the wildcard pattern. This technique is useful both when you don't know the exact name of a file or folder and when you need to perform operations on multiple files or folders that match the same wildcard pattern.

The sample script below renames all files in the source folder that match a wildcard pattern. The file names are modified such that the current date is inserted between the original file name and file extension. Each file is then moved into a post-processing destination folder.

This sample could easily be modified to work on remote files by using commands like FTPGETREWIND, FTPGETFILE and FTPRENAME.

This technique of using GETNEXTFILE in a loop can be adapted for use with other script commands like PGPENCRYPT that do not directly accept the use of wildcard characters.


  1  WORKINGDIR "c:\source\folder"
  2  :loop
  3  ;; populate %nextfile variable with file name matching wildcard pattern
  4  GETNEXTFILE "Weekly*.dat" 
  5  IFERROR RETURN $ERROR_SUCCESS
  6  ;; split the four character ".dat" extension into a new variable
  7  SETRIGHT extension = filename 4 /split 
  8  ;; build the new file name
  9  SET filename = "C:\destination\folder\" + filename + "_" + %date + extension
 10  RENAME %nextfile filename 
 11  IFERROR RETURN
 12  GOTO loop

Browse complete list of scripts