Wednesday, July 23, 2008

Rename All Files in a Directory with NAnt

It’s a pretty simple thing to do but I had to spend a few minutes today to figure out how to accomplish it. So here is a quick build file to rename all .sql files in a specified directory (relative to the script run directory) to .txt. Funny enough, this scripts gets called by my automation program before it uploads any SQL scripts to my blog. There is nothing to it and there are comments so here goes:

<project name="renameSqlToText" default="run" xmlns="">
 <!-- The relative path to the directory where the files are located -->
 <property name="sqlDirectory" value="..\downloads\sql" />

 <target name="run">
  <!-- For each file in the directory -->
  <foreach item="File" in="${sqlDirectory}" property="fileName">
   <!-- If the file has the .sql extension -->
   <if test="${string::to-lower(path::get-extension(fileName)) == '.sql'}">
    <!-- Rename the file to .txt extension -->
    <move file="${fileName}" tofile="${path::combine(path::get-directory-name(fileName), path::get-file-name-without-extension(fileName) + '.txt')}" />


// //]]>