1#!/bin/tcsh -f
2
3set stat = 0
4set id = `3dnewid -fun`
5set lout = /tmp/DFN_$id.txt
6set ddir = DEMO_DeblankFileNames
7set repchar = '_'
8
9goto PARSE
10RET_PARSE:
11
12set nf = `wc -l $lout`
13set nf = $nf[1]
14if ( $nf < 1) then
15   echo "No files to fix"
16   goto END
17endif
18
19set n = 1
20while ($n <= $nf)
21   set fin = "`sed -n ${n}p $lout`"
22   if ($spo) then
23      set space = (`\echo "$fin" | \grep '. .'`)
24   else
25      set noglob
26      set space = (`\echo "$fin" | \grep -E '.\(|]| |\)|\[.'`)
27      unset noglob
28   endif
29   if ($#space > 0) then
30      #echo "Space >$fin<"
31      set badname = 1
32      set all_tested = ()
33      set spc = ${repchar}
34      set ibad = 0
35      while ($badname && $ibad < 3)
36         if ($spo) then
37            set fout = `\echo "$fin" | \sed "s/ /${spc}/g"`
38         else
39            set fout = `\echo "$fin"   | \sed "s/[() \[]/${spc}/g" \
40                                       | \sed "s/\]/${spc}/g"`
41         endif
42         if ($status) then
43            echo "Failed to substitute"
44            goto BEND
45         endif
46         set all_tested = ($all_tested "$fout")
47         if ( -f "./$fout" || -d "./$fout" ) then
48            @ ibad ++
49            set spc = "${spc}${repchar}"
50         else
51            set spc = $repchar
52            set badname = 0
53            set ibad = 0
54         endif
55      end
56      if ( $badname ) then
57         echo "Replacement name(s) for "$fin" exist, nothing will be done."
58         echo "   Names tested are $all_tested"
59      else
60         if ($dry == 0) then
61            echo "Renaming    >./$fin<"
62            echo "         to >./$fout<"
63            \mv "./$fin" "./$fout"
64         else
65            echo "Would rename   >./$fin<"
66            echo "            to >./$fout<"
67         endif
68      endif
69   else
70      #echo "No space >$fin<"
71   endif
72   @ n ++
73end
74
75goto END
76PARSE:
77   set Narg = $#
78   set dry = 1
79   set spo = 0
80   set cnt = 1
81   if ( -f $lout) then
82      echo "$lout should not exist, remove it and rerun"
83      goto BEND
84   endif
85
86   while ($cnt <= $Narg)
87		set donext = 1;
88      if ($donext && "$argv[$cnt]" == "-echo") then
89         set echo
90         set donext = 0; goto NEXT
91      endif
92
93      if ($donext && ("$argv[$cnt]" == "-h" || "$argv[$cnt]" == "-help")) then
94         goto HELP
95         set donext = 0;	 goto NEXT
96      endif
97
98      if ($donext && "$argv[$cnt]" =~ "-dry"*) then
99         set dry = 1
100         set donext = 0; goto NEXT
101      endif
102
103      if ($donext && "$argv[$cnt]" =~ "-move"*) then
104         set dry = 0
105         set donext = 0; goto NEXT
106      endif
107
108      if ($donext && "$argv[$cnt]" =~ "-nobrac"*) then
109         set spo = 1
110         set donext = 0; goto NEXT
111      endif
112
113      if ($donext && "$argv[$cnt]" == "-demo_set") then
114         goto DEMO_SET
115         set donext = 0; goto NEXT
116      endif
117
118      if ($donext == 1) then
119         \ls -1d "$argv[$cnt-$Narg]" > $lout
120         set donext = 0; goto NEXT
121      endif
122
123      NEXT:
124		@ cnt ++
125	end
126
127   if ( ! -f $lout ) then
128      if ($spo) then
129         \ls -1d ./*\ * > $lout
130      else
131         \ls -1d ./*\ * ./*\(* ./*[* ./*\]* ./*\[* | uniq > $lout
132      endif
133   endif
134
135goto RET_PARSE
136
137HELP:
138   echo ""
139   echo "A script to remove blanks and other annoying characters from filenames."
140   echo " in the current directory."
141   echo "The default set of characters to replace is ' []()'"
142   echo "Spaces are replaced with $repchar. "
143   echo "If resultant name exists, more ${repchar} are used until new name"
144   echo "is found."
145   echo ""
146   echo "   `basename $0` [-move] [FILES]"
147   echo ""
148   echo "OPTIONS"
149   echo "   -dry_run: Just show what would be done. Don't rename files."
150   echo "             This is the default option"
151   echo "   -move: Actually rename the files (opposite of -dry_run)"
152   echo "   -nobrac: Do not replace () and [] in filenames, just spaces"
153   echo "   -demo_set: Create a toy directory with bad names for testing."
154   echo "   -echo: Turn on script echo"
155   echo "   -help: This message"
156   echo "   FILES: Specify files to fix as opposed to letting it fix all"
157   echo "          the names in the current directory."
158   echo ""
159   echo "Examples:"
160   echo "   1- `basename $0` "
161   echo ""
162   echo "   2- `basename $0` -move "
163   echo ""
164   echo "   3- Run the command below and follow its suggestions"
165   echo "      `basename $0` -demo_set"
166   echo ""
167   goto END
168
169DEMO_SET:
170   if ( -d $ddir ) then
171      echo "Remove $ddir first"
172      goto BEND
173   endif
174   mkdir $ddir
175   cd $ddir
176   touch dbf\ .jpg
177   touch dbf2\ toy.tiff
178   touch dbf2_toy.tiff
179   touch ' dbf4_toy.tiff'
180   touch '1 dbf3_toy.tiff'
181   touch ' dbf5_toy.tiff '
182   mkdir 'dbf dir'
183   touch 'dbf dir/why do you ( use ) bad cha[rac]ters in your filenames'
184   cd -
185   echo ""
186   echo "See the file content of $ddir then try"
187   echo "   cd $ddir"
188   echo "   `basename $0`"
189   echo " or for the whole enchilada"
190   echo "   `basename $0` -move"
191   echo "Then check the new file names again and remove that directory when done"
192   echo ""
193   echo "Note that the file under old directory name 'dbf dir' is not touched."
194   echo "To fix it you'll need something like:"
195   echo "   cd dbf_dir"
196   echo "   `basename $0` -move"
197   echo " or the more cumbersome"
198   echo "   `basename $0` -move why*"
199   echo ""
200   goto END
201
202BEND:
203   echo "Failed"
204   set stat = 1
205   goto END
206
207END:
208   #cat /tmp/DFN_$id.txt
209   if ( -f /tmp/DFN_$id.txt) \rm -f /tmp/DFN_$id.txt
210   exit $stat
211
212