ForEach local|remote <pattern> do <command>
The ForEach command lets you execute another command multiple times for each file which matches a search pattern (e.g. "*.html") on the local or remote file system.
The command to be executed is one parameter enclosed in single or double quotes which contains a full script command. If the the script command also requires quotes, you can use single quotes within double quotes or vice versa, e.g. "RemoteRename '$file.txt', '$file.text'".
The script command can contain the placeholder $file (written in lowercase), which will be replaced by the file names of each file that is matched by the ForEach command.
As an alternative to the $file placeholder, if there is exactly one * character in the search pattern, all * characters in the command will be replaced by the part of the file name which matches the * in the pattern (see the 'RemoteRename' example below).
If the search pattern is preceded by dir: the ForEach command will for directories rather than files. For better readability you can then use $dir instead of $file in the command parameter, although both version are identical.
Example: ForEach remote "*.cgi" do "RemoteExec 'chmod 777 $file'"
(Execute the remote chmod command to all cgi files)
Example: ForEach remote "*.mdb" do "Get '$file'"
(Get all remote MDB files. Same as Get "*.mdb" or GetDir "*.mdb")
Example: ForEach remote "*.txt" do "RemoteRename '*.txt', '*.text'"
(Rename all remote *.txt files into *.text)
Example: ForEach remote "dir:data*" do "RemoteRmDir '$dir'"
(Remove all remote directories which have a name starting with 'data')
Example: ForEach remote "*.html" do "LogToFile 'files.out', '$file'"
(Write all remote files' names which match *.html to files.out)
See also: Get, GetDir, File/Foldername Patterns, RemoteExec, RemoteRename, RemoteDelete, RemoteRmDir.
← Back to Script Commands