Page 1 of 1

substring or regex??

Posted: Fri Oct 09, 2015 1:10 pm
by GA_Forum
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!
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>


Re: substring or regex??

Posted: Fri Oct 09, 2015 6:35 pm
by Support_Rick
Try this:

Just Created a local file list as you're doing in the SFTP Get .. but, same principle. You can see the JobLog below for this test as well.

Hope this helps.
Code: Select all
<project name="test_rename" mainModule="Main" version="2.0" logLevel="silent">
	<description>Test rename of a file using substring</description>

	<module name="Main" logLevel="debug">

		<createWorkspace version="1.0" />

		<createFileList fileListVariable="LocFiles" version="1.0">
			<fileset dir="C:\temp\ForumTest">
				<regexFilter>
					<include pattern="^BUEP01S" caseSensitive="true" />
				</regexFilter>
			</fileset>
		</createFileList>

		<forEachLoop label="forEachLoop - Rename Files" itemsVariable="${LocFiles}" currentItemVariable="CurrentFile">

			<setVariable label="setVariable - FileName" name="FileName" value="${CurrentFile:name}" version="2.0" />

			<setVariable label="setVariable - FirstDot" name="FirstDot" value="${ PositionOf( FileName, '.', 1 ) }" version="2.0" />
			<setVariable label="setVariable - LastDot" name="LastDot" value="${ LastPositionOf( FileName, '.' ) }" version="2.0" />

			<setVariable label="setVariable - FilePrefix" 	name="FilePrefix" 	value="${ Substring( FileName, 1, FirstDot-1 ) }" version="2.0" />
			<setVariable label="setVariable - FileSuffix" 	name="FileSuffix" 	value="${ Substring( FileName, LastDot+1, 4 ) }" version="2.0" />

			<setVariable label="setVariable - NewFileName" 	name="NewFileName" 	value="${ Concat( FilePrefix, FileSuffix, '.txt' ) }" version="2.0" />

			<print label="print - to log file" version="1.0">
				<![CDATA[
=======================================================================================				
The variable "FileName" 	contains: ${FileName}
The variable "FilePrefix" 	contains: ${FilePrefix}
The variable "FileSuffix" 	contains: ${FileSuffix}
The variable "NewFileName" 	contains: ${NewFileName}
=======================================================================================				
				]]>
			</print>

			<rename label="rename" inputFile="${CurrentFile}" newName="${NewFileName}" outputFilesVariable="RenamedFiles" version="1.0" />

		</forEachLoop>
	</module>

</project>
Code: Select all
2015-10-09 16:33:18           INFO      Start Date and Time: 2015-10-09 16:33:18
2015-10-09 16:33:18           INFO      Job Number: 1000000001104 
2015-10-09 16:33:18           INFO      Project Name: /Forum/test_rename
2015-10-09 16:33:18           INFO      Submitted By: RElliott
2015-10-09 16:33:18           INFO      Submitted From: Administrator UI
2015-10-09 16:33:18           INFO      System: GA-MFT-01
2015-10-09 16:33:18           INFO      GoAnywhere 5.1.1 running on Windows 7 6.1 (amd64)
2015-10-09 16:33:18           INFO      
                                        =======================================================================================				
                                        The variable "FileName" 	contains: BUEP01S.151002.1905.09ds5v10
                                        The variable "FilePrefix" 	contains: BUEP01S
                                        The variable "FileSuffix" 	contains: 09ds
                                        The variable "NewFileName" 	contains: BUEP01S09ds.txt
                                        =======================================================================================				
                                        				
2015-10-09 16:33:18           INFO      End Date and Time: 2015-10-09 16:33:18

Re: substring or regex??

Posted: Tue Oct 13, 2015 8:12 am
by GA_Forum
Very nice Rick, thank you for your assistance!