Sequential number suffix?
- Posts: 4
- Joined: Thu Apr 19, 2012 5:44 pm
- Location: Los Angeles
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
- Support Specialist
- Posts: 66
- Joined: Thu Mar 05, 2009 3:49 pm
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.
<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>
- Posts: 4
- Joined: Thu Apr 19, 2012 5:44 pm
- Location: Los Angeles
-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