substring or regex??
Posted: Fri Oct 09, 2015 1:10 pm
I am retrieving a file that has a naming format of "[A-Z]{4}[0-9]{2}[A-Z]{1}.[0-9]{6}.[0-9]{4}.[0-9]{2}[a-z]{2}[0-9]{1}[a-z]{1}[0-9]{2}", for example "BUEP01S.151002.1905.09ds5v10" (pretty ugly eh?). I need to rename the file by taking portions of the original file name to create the renamed file name. When renaming the file, I need to select all the characters before the first period in the original file name, so for our example I'll need to select "BUEP01S". Then I need to get the first four characters after the last period in the original file name, so for our example I'll need "09ds". Then I need to include an extension of ".txt". So the final result of the rename for our example file ("BUEP01S.151002.1905.09ds5v10") would be "BUEP01S09ds.txt".
I tried using various regex's in the rename task, but could never get it to work. As a temporary work around I tried using substring. The substring method works, but it's cumbersome and I'm sure there is a better way to do this, one with less tasks. I've attached my substring code below, anyone have any suggetsions on how to improve this or any regex suggestions (other than directing me to the "Wildcards and Regular Expressions" secton in the Appendix of the help documentation in GAD)?
I am using GoAnywhere Director v4.7.0 on a Windows Server 2012
Thank you for your time!
I tried using various regex's in the rename task, but could never get it to work. As a temporary work around I tried using substring. The substring method works, but it's cumbersome and I'm sure there is a better way to do this, one with less tasks. I've attached my substring code below, anyone have any suggetsions on how to improve this or any regex suggestions (other than directing me to the "Wildcards and Regular Expressions" secton in the Appendix of the help documentation in GAD)?
I am using GoAnywhere Director v4.7.0 on a Windows Server 2012
Thank you for your time!
Code: Select all
<project name="test_rename" mainModule="Main" version="2.0" logLevel="debug">
<description>Test rename of a file using substring</description>
<module name="Main" logLevel="debug">
<createWorkspace version="1.0" />
<setVariable label="setVariable - error_SftpToEnq" name="error_SftpToEnq" value="0" version="2.0" />
<sftp label="sftp - to enqapp1" resourceId="enqapp1-sftp-ga" outputSessionId="EnqSessionID" version="1.0" onError="setVariable:error_SftpToEnq=1">
<get label="get - file from enqapp1" destinationDir="${system.job.workspace}" destinationFilesVariable="LocFiles" numFilesDownloadedVariable="FileDwnldCount" processedSourceFilesVariable="RemoteFiles">
<fileset dir="/data03/fio/prd/xfr/cst/tcm/">
<regexFilter>
<include pattern="^BUEP01S" caseSensitive="true" />
</regexFilter>
</fileset>
</get>
</sftp>
<ftpCloseSession label="ftpCloseSession" sessionId="${EnqSessionID}" version="1.0" />
<forEachLoop label="forEachLoop - Rename Files" itemsVariable="${LocFiles}" currentItemVariable="CurrentFile">
<setVariable label="setVariable - FileName" name="FileName" value="${CurrentFile:name}" version="2.0" />
<print label="print - to log file" version="1.0">
<![CDATA[The variable "FileName" contains: ${FileName}]]>
</print>
<setVariable label="setVariable - FilePrefix" name="FilePrefix" value="${Substring(FileName,1,7)}" version="2.0" />
<print label="print - to log file" version="1.0">
<![CDATA[The variable "FilePrefix" contains: ${FilePrefix}]]>
</print>
<setVariable label="setVariable - FileSuffix" name="FileSuffix" value="${Substring(FileName,21,4)}" version="2.0" />
<print label="print - to log file" version="1.0">
<![CDATA[The variable "FileSuffix" contains: ${FileSuffix}]]>
</print>
<setVariable label="setVariable - NewFileName" name="NewFileName" value="${FilePrefix}${FileSuffix}.txt" version="2.0" />
<print label="print - to log file" version="1.0">
<![CDATA[The variable "NewFileName" contains: ${NewFileName}]]>
</print>
<rename label="rename" inputFile="${FileName}" newName="${NewFileName}" outputFilesVariable="RenamedFiles" version="1.0" />
</forEachLoop>
</module>
</project>