Page 1 of 1
Sequential number suffix?
Posted: Thu May 10, 2012 6:54 pm
by ngeorgieff
Hi,
Is it possible to add a sequential number as suffix to file name.
For example: Read the last file name number and add one:
File.00001.txt +1
File.00002.txt +1
File.00003.txt +1
Thanks
Nikolay
Re: Sequential number suffix?
Posted: Tue May 15, 2012 6:35 pm
by Support_Duane
Yes, it is possible.
Here's an example. You'll need to change the name of the folder in which the files reside and both substring functions are based on a filename layout of xxxx.nnnnn.txt. If you're using something different, you'll need to change the parameters in the substring functions accordingly.
Code: Select all<project name="get last filename in filelist" mainModule="Main" version="2.0">
<module name="Main" logLevel="debug">
<createFileList fileListVariable="fl" numFilesFoundVariable="fc" version="1.0">
<fileset dir="/djohnson/addseq" />
</createFileList>
<forEachLoop itemsVariable="${fl}" currentItemVariable="thisFile" currentIndexVariable="i" beginIndex="${fc}">
<print version="1.0">
<![CDATA[i = ${i}]]>
</print>
<setVariable label="set count" name="count" value="${i + 1}" version="2.0" />
<setVariable label="set prefix = '.0000'" name="prefix" value=".0000" version="2.0" />
<if condition="${count >= 10}">
<setVariable label="set prefix = '.000'" name="prefix" value=".000" version="2.0" />
</if>
<if condition="${count >= 100}">
<setVariable label="set prefix = '.00'" name="prefix" value=".00" version="2.0" />
</if>
<if condition="${count >= 1000}">
<setVariable label="set prefix = '.0'" name="prefix" value=".0" version="2.0" />
</if>
<if condition="${count >= 10000}">
<setVariable label="set prefix = '.'" name="prefix" value="." version="2.0" />
</if>
<setVariable label="set newName" name="newName" value="${Substring(thisFile:name, 1, 4)}${prefix}${count}.txt" version="2.0" />
<print version="1.0">
<![CDATA[
new file name = ${thisfile:parentfile}/${newname}
]]>
</print>
<copy sourceFile="${thisfile}" destFile="${thisfile:parentfile}/${newname}" version="1.0" disabled="false" />
</forEachLoop>
</module>
</project>
Re: Sequential number suffix?
Posted: Thu May 24, 2012 7:26 pm
by ngeorgieff
Thank you. In this case we are using fileset count +1. If we delete some files, this will broke the sequential number.
-rw-r--r-- 1 root root 4835 May 24 16:54 File.00002.txt
-rw-r--r-- 1 root root 4835 May 24 16:54 File.00003.txt
-rw-r--r-- 1 root root 4835 May 24 16:55 File.00005.txt
-rw-r--r-- 1 root root 4835 May 24 17:17 File.00006.txt
------
-rw-r--r-- 1 root root 4835 May 24 16:54 File.00002.txt
-rw-r--r-- 1 root root 4835 May 24 16:54 File.00003.txt
-rw-r--r-- 1 root root 4835 May 24 17:21 File.00005 2.txt <------
-rw-r--r-- 1 root root 4835 May 24 16:55 File.00005.txt
-rw-r--r-- 1 root root 4835 May 24 17:17 File.00006.txt
Re: Sequential number suffix?
Posted: Thu Jun 21, 2012 10:27 am
by monahanks
You can connect to a SQL database that stores the sequential number, retrieve the number and then update the sequence with a +1. Our method involves stored procedures and a table of different sequence numbers keyed by a file type (AA is an order file, BB is sales, etc). It gets a little involved (to me) since I'm new to SQl and stored procedures, but it works well. The project that handles the SQL database access and retrieval is in a common folder accessible (or included in) any project that needs a sequential number.