<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Groovy on Andrew's Memory Blog</title><link>https://andrewmemory.acornwall.net/tags/groovy/</link><description>Recent content in Groovy on Andrew's Memory Blog</description><generator>Hugo -- gohugo.io</generator><image><url>https://andrewmemory.acornwall.net/img/rss_image.png</url><title>Groovy on Andrew's Memory Blog</title><link>https://andrewmemory.acornwall.net/</link></image><language>en</language><managingEditor>andrewmemoryblog@gmail.com (Andrew's Memory Blog)</managingEditor><webMaster>andrewmemoryblog@gmail.com (Andrew's Memory Blog)</webMaster><copyright>Copyright 2009--2025</copyright><lastBuildDate>Sat, 12 Dec 2009 15:24:20 -0700</lastBuildDate><atom:link href="https://andrewmemory.acornwall.net/tags/groovy/index.xml" rel="self" type="application/rss+xml"/><item><title>Changing Unix/Linux filenames to handle DOS conventions</title><link>https://andrewmemory.acornwall.net/blog/2009-12-12-changing-unixlinux-filenames-to-handle-dos-conventions/</link><pubDate>Sat, 12 Dec 2009 15:24:20 -0700</pubDate><author>andrewmemoryblog@gmail.com (Andrew's Memory Blog)</author><guid>https://andrewmemory.acornwall.net/blog/2009-12-12-changing-unixlinux-filenames-to-handle-dos-conventions/</guid><description>&lt;p&gt;Recently, I had a bunch of files with characters in their names that made them hard to handle under Windows. Although Unix has no problems with :, ? and others, DOS / Windows doesn&amp;rsquo;t handle them well. This made using them on my CIFS share a bit of a pain.&lt;/p&gt;
&lt;p&gt;To rename them, I hacked up a quick Groovy script. It maps all &amp;ldquo;weird&amp;rdquo; characters to _, which is pretty much universal.&lt;/p&gt;
&lt;p&gt;Note that this script doesn&amp;rsquo;t currently handle the case where two files map to the same name. If you have :foo and ?foo, they&amp;rsquo;ll both get mapped to _foo and you&amp;rsquo;ll lose one of them.&lt;/p&gt;
&lt;p&gt;I called it dosname because it was late and I wasn&amp;rsquo;t feeling creative.&lt;/p&gt;
&lt;figure class="highlight"&gt;
&lt;pre tabindex="0"&gt;&lt;code class="language-" data-lang=""&gt;#!/usr/bin/groovy -
if(args.length == 0) {
printArgs()
System.exit(0);
}
dirName = args[0];
new File(dirName).eachFile() { file -&amp;gt;
def matcher = (file.getName() =~ /[\?\:\&amp;#34;\\]/)
def newName = matcher.replaceAll(&amp;#34;_&amp;#34;)
File newFile = new File(dirName &amp;#43; &amp;#34;/&amp;#34; &amp;#43; newName.toString())
println file.getName() &amp;#43; &amp;#34; -&amp;gt; &amp;#34; &amp;#43; newFile.getName()
file.renameTo(newFile)
}
System.exit(0);
def printArgs() {
println(&amp;#34;Usage: dosname [dirname]&amp;#34;)
println(&amp;#34; Note: I don&amp;#39;t handle the case where two files map to the same filename yet!&amp;#34;)
}&lt;/code&gt;&lt;/pre&gt;
&lt;/figure&gt;
&lt;p&gt;The interesting bit is in the line&lt;/p&gt;
&lt;figure class="highlight"&gt;
&lt;pre tabindex="0"&gt;&lt;code class="language-" data-lang=""&gt;def matcher = (file.getName() =~ /[\?\:\&amp;#34;\\]/)&lt;/code&gt;&lt;/pre&gt;
&lt;/figure&gt;
&lt;p&gt;That line defines the regular expression which contains a group of characters which will be replaced by _. Right now it&amp;rsquo;s &lt;/p&gt;
\[?:"\\\]&lt;p&gt; (all quoted with \ because that&amp;rsquo;s what you have to do in regex).&lt;/p&gt;
&lt;p&gt;Run it with &amp;ldquo;dosname &lt;em&gt;directoryname&lt;/em&gt;&amp;rdquo;. There&amp;rsquo;s no way to reverse its effects, so try not to use it on useful directories like /etc/.&lt;/p&gt;
&lt;p&gt;Also, I needed to install OpenJDK6 JDK (not just the runtime) in order to run Groovy on Ubuntu 9.04. Go figure.&lt;/p&gt;
&lt;p&gt;Since I don&amp;rsquo;t know Groovy, I stole most of this code from &lt;a href="http://www.rossenstoyanchev.org/write/prog/java/groovy1.html" target="_blank" rel="noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;</description></item></channel></rss>