Batch file Rename Special Characters

"Please leave a message at the beep, we will get back to you when your support contract expires."

Moderators: phlip, Larson, Moderators General, Prelates

Batch file Rename Special Characters

Postby root » Tue Dec 28, 2010 5:21 pm UTC

I know I am making this harder than I should, but I am stuck.

I want to write a batch file to replace some special characters for a group of files; specifically:
  • #
  • %
  • &
  • {
  • }
  • ~

I want to replace # with ""
% with "_"
& with " and "
{ and } with "(" and ")" respectively
and lastly, ~ with "-"

Of course, the characters are all over, so I need to be able to find where it is.
"This & That file #2.txt" >> "This and That file 2.txt"
and
"This file {you know which one} ~ 1.Jan.10.txt" >> "This file (you know which one) - 1.Jan.10.txt" >>

Ideally, I could have the .bat file on my desktop and run it, then simply drag the folder with the offending files on to the command line, and hit enter, and have it go from there.

Ideal Scenerio:
  1. Run .bat from destop
  2. Drag and drop "c:\testfolder\ onto cmd
  3. All files within said directory are renamed according to aforementioned system.
  4. command line closes
  5. I eat pie instead of spending 3 hours naming files because people above me don't know how to name sh*t properly

Obviously, if someone could just help me with one of the examples, I can figure out how to do the rest of the symbol replacement on my own.

I've googled for a bit and can only seem to find renaming a single file based on the adding something to the front, ore removing characters from the front, nothing on special characters in the middle of the filename, and nothing about dragging and dropping a folder onto the CMD to input the path.
User avatar
root
 
Posts: 88
Joined: Sun May 18, 2008 11:55 pm UTC

Re: Batch file Rename Special Characters

Postby Dropzone » Tue Dec 28, 2010 8:37 pm UTC

Does it really have to be a batch file? It would be trivial to do it in a scripting language like Python, and not much harder in any other real programming language, but as a batch file it's going to be fairly horrific. I've almost managed it, but I can't get the ~ case to work. Here's the code anyway:
Code: Select all
@echo off
setlocal enabledelayedexpansion
echo renaming files in %1
for %%A in (%1\*) do (
    set B="%%~nA"
    set B=!B:#=!
    set B=!B:%%=_!
    set B=!B:^&= and !
    set B=!B:{=(!
    set B=!B:}=^)!
    rem the next line doesn't work, because ~ has a special meaning when it
    rem comes immediately after the : and I can't see any way to escape it
    rem set B=!B:~=-!
    echo renaming "%%~nA"
    echo       to !B!
    ren "%%A" !B!
)
pause
You would run it by dragging the folder on to the batch file itself (no need to run the batch file first). If you do end up using this (even though it's incomplete), back the files up first - batch files tend to be very unpredictable and prone to breaking for no apparent reason.
User avatar
Dropzone
 
Posts: 405
Joined: Sun Dec 30, 2007 10:12 pm UTC
Location: North Lincs., UK

Re: Batch file Rename Special Characters

Postby root » Fri Mar 09, 2012 8:57 pm UTC

Yes, bumping a two year post, I know

Question remains the same, but I can now use python or another language. I ended up never needed the previous solution because I quit that job and got another. I never thought I'd run in to the same situation. Life eh?
User avatar
root
 
Posts: 88
Joined: Sun May 18, 2008 11:55 pm UTC

Re: Batch file Rename Special Characters

Postby Yakk » Fri Mar 09, 2012 9:15 pm UTC

Download gvim. (use google to find it).
dir/s/b > files.txt the directory tree you care about.
open files.txt in gvim.

Run the following commands (exactly):
Code: Select all
:% s/.*/& zzzMARKzzz &/g
:% s/zzzMARKzzz\(.*\)#/zzzMARKzzz\1/g
:% s/zzzMARKzzz\(.*\)%/zzzMARKzzz\1_/g
:% s/zzzMARKzzz\(.*\)&/zzzMARKzzz\1 and /g
:% s/zzzMARKzzz\(.*\){/zzzMARKzzz\1(/g
:% s/zzzMARKzzz\(.*\)}/zzzMARKzzz\1)/g
:% s/zzzMARKzzz\(.*\)~/zzzMARKzzz\1-/g
:% s/^\(.*\) zzzMARKzzz \(.*\)/"\1" "\2"/g
:% s/^/move /g
:w rename_files.bat

This will create a batch file called "rename_files.bat". Run it after you examine it closely to make sure it does what it is supposed to do. Backup your files first.

In the event that things don't work, well, you are screwed.

Note that the above is actually using sed commands. An easier way to do the above would be to write a sed script that basically does the above. Or any language that supports regular expressions.

Also note that if one of your files contains the string zzzMARKzzz in the name, the above will break. :) Also, I haven't tested the above, I just wrote it off the top of my head.
One of the painful things about our time is that those who feel certainty are stupid, and those with any imagination and understanding are filled with doubt and indecision - BR

Last edited by JHVH on Fri Oct 23, 4004 BCE 6:17 pm, edited 6 times in total.
User avatar
Yakk
 
Posts: 10038
Joined: Sat Jan 27, 2007 7:27 pm UTC
Location: E pur si muove

Re: Batch file Rename Special Characters

Postby chridd » Tue Mar 13, 2012 6:14 pm UTC

Yakk wrote:
Code: Select all
:% s/zzzMARKzzz\(.*\)#/zzzMARKzzz\1/g
:% s/zzzMARKzzz\(.*\)%/zzzMARKzzz\1_/g
:% s/zzzMARKzzz\(.*\)&/zzzMARKzzz\1 and /g
:% s/zzzMARKzzz\(.*\){/zzzMARKzzz\1(/g
:% s/zzzMARKzzz\(.*\)}/zzzMARKzzz\1)/g
:% s/zzzMARKzzz\(.*\)~/zzzMARKzzz\1-/g
Wouldn't that only change the first instance of each character? (It uses /g, but there's only one zzzMARKzzz in each line, so it won't match multiple times.)
~ chri d. d.
mittfh wrote:I wish this post was very quotable...
User avatar
chridd
 
Posts: 361
Joined: Tue Aug 19, 2008 10:07 am UTC
Location: ...Earth, I guess?

Re: Batch file Rename Special Characters

Postby Yakk » Tue Mar 13, 2012 6:50 pm UTC

Good point. Then you'd have to run it a bunch of times until it says you changed 0 lines. Sad that.

And, for whatever reason, my brackets went away? Or I never typed them? Sigh. The .* is supposed to have a backslash bracket surrounding it on each line.

A way to avoid the repeated application problem annoyance (to some degree) would be to search for each of the words you want to replace, put a mark before it, clone the line with a yyyMARKyyy in the middle, then repeatedly strip out all zzzMARKzzz before the yyyMARKyyys in the file (which is easier). Then, do a replacement on each zzzMARKzzz[special character] remaining in the file.

Another approach would be to create the file list, duplicate it, process one of the files (thus doing away with the mark nonsense), then use a utility to join the two files (with spaces/etc and a starting move) horizontally. Like the unix join command:
http://www.computerhope.com/unix/ujoin.htm
or, any number of languages where you can do this via a 2 line script.

As yet another method, the perl method:
Code: Select all
#!/usr/bin/perl

## For each line of the input, process it:
while($line = <>)
{
  my $orig = $line;
  my $dest = $line;
  $dest =~ s/\#//g;
  $dest =~ s/\%/_/g;
  $dest =~ s/\&/ and /g;
  $dest =~ s/\{/(/g;
  $dest =~ s/\}/)/g;
  $dest =~ s/\~/-/g;
  println("move \"", $orig, "\" \"", $dest, "\"");
}

which solves the problem in fewer lines, and more directly than the gvim method above. (also not compiled, also not tested).

Any other half-decent scripting language should be able to do this easily, probably using commands similar to what is going on above.
One of the painful things about our time is that those who feel certainty are stupid, and those with any imagination and understanding are filled with doubt and indecision - BR

Last edited by JHVH on Fri Oct 23, 4004 BCE 6:17 pm, edited 6 times in total.
User avatar
Yakk
 
Posts: 10038
Joined: Sat Jan 27, 2007 7:27 pm UTC
Location: E pur si muove

Re: Batch file Rename Special Characters

Postby Jorpho » Thu Mar 15, 2012 2:51 am UTC

Yakk wrote:Another approach would be to create the file list, duplicate it, process one of the files (thus doing away with the mark nonsense), then use a utility to join the two files (with spaces/etc and a starting move) horizontally. Like the unix join command:
http://www.computerhope.com/unix/ujoin.htm
or, any number of languages where you can do this via a 2 line script.
On one occasion I just used Excel and an appropriate string-manipulation formula. Inelegant, yea, but gets the job done with a minimum of fuss.
User avatar
Jorpho
 
Posts: 5040
Joined: Wed Dec 12, 2007 5:31 am UTC
Location: Canada

Re: Batch file Rename Special Characters

Postby DaGeek247 » Fri Mar 01, 2013 1:58 am UTC

Bumping a really old post, but I came across this on Google and it didn't help me very much. I did however, spend three hours getting it to work, and thought i'd share the results with all of you anyways.
Code: Select all
::turn off the annoying location ex; C:/path/to/me>
echo off
::Clean up the screen
cls


::log file is temporary, and can be anything
SET log=filelist.txt
::format supports *, and everything else
SET format=pdf
::this is what will be removed
SET in=
::this is what will replace %in%
SET out=_


::Tell us the settings
::echo Log file:....%log%
echo Format:......%format%
echo Remove:......"%in%"
echo Replace:....."%out%"
echo Example:
echo This%in%will%in%change.%format%
echo This%out%will%out%change.%format%


::make sure settings are correct
Choice /M "Is this correct?"
If Errorlevel 2 Goto No
If Errorlevel 1 Goto Yes
:No
echo Quitting Program
goto end
:Yes
Echo Continuing...


::record the list of files to rename
dir *.%format% /s/b/o > %log%


::go through the list, and replace %in% with %out%
::also echo that stuff just in case
SETLOCAL EnableDelayedExpansion
echo Modified file names:
for /F "tokens=*" %%x in (%log%) do (
   echo %%x
   SET vr=%%~nx.%format%
   SET vr=!vr:%in%=%out%!
   echo.!vr!
   rename "%%x" !vr!)


::clean up after itself
del %log%
echo Operation Successful


::make sure the user can see what happened
pause
:end


Basically it uses to for loops to accomplish a task. the first loop goes and stores a list of files that it will modify. (it also limits to a certain format). Then, using another for loop it goes through that file and renames them all according to the variables set at the very beginning. I hope this helps a fellow Googler.
DaGeek247
 
Posts: 1
Joined: Fri Mar 01, 2013 1:52 am UTC

Re: Batch file Rename Special Characters

Postby troyp » Sat Mar 02, 2013 1:52 pm UTC

Isn't this what tr is for?
eg. to translate # -> ^ $ -> & and % -> * , you'd use something like:

Code: Select all
for name in *; do new=`echo $name | tr '#$%' '^&*'`; mv "$name" "$new"; done
troyp
 
Posts: 398
Joined: Thu May 22, 2008 9:20 pm UTC
Location: Lismore, NSW

Re: Batch file Rename Special Characters

Postby EvanED » Sat Mar 02, 2013 5:46 pm UTC

Isn't this what the somewhat-standard-on-Linux rename is for? E.g. to translate "&" to "and" you'd say
Code: Select all
rename "&" and *

Can't do that with tr. :-)

(There is one gotcha: it only renames one occurrence, so you have to re-run the command if you have more than one.)
EvanED
 
Posts: 3765
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI

Re: Batch file Rename Special Characters

Postby troyp » Sun Mar 03, 2013 3:30 am UTC

Interesting. I'm not familiar with that command. "rename" on my system is a perl script that lets you do perl regex substitutions directly on filenames. I didn't mention it because renaming with perl regexes tends to fuck up for me on filenames with special characters. Presumably because I don't know Perl.

The command you mentioned is actually present under the name "rename.ul". I'll have to remember it for simple batch renames.

It's not really suitable for this particular problem, though, due to the issue you mentioned and others. Another limitation is that it seems to only translate between one pair of substrings at a time. So you'd need a rename command for each substitution and then loop over each until you run out of the "from" character (or loop over all until you run out of all "from" characters). Even worse: you'd have to handle the case where the set of "from" characters intersects the set of "to" characters. tr handles that for you automatically.

I do know there are several command line batch renamers floating around, so maybe one of them would work for this, but I've never got around to investigating them.

btw, using tr isn't quite as verbose as it looks. There were 10 superfluous characters in the variables and no aliases since I wanted to make it easy for anyone to read (and I don't know what variable conventions and aliases other people use). I'd never actually write that interactively. I'd use something like:
Code: Select all
dofiles y=`echo $x| tr '#$%' '^&*'`; mv "$x" "$y"; done
which is not ideal, but probably Good Enough.
troyp
 
Posts: 398
Joined: Thu May 22, 2008 9:20 pm UTC
Location: Lismore, NSW

Re: Batch file Rename Special Characters

Postby PM 2Ring » Sun Mar 03, 2013 6:47 am UTC

troyp wrote:I "rename" on my system is a perl script that lets you do perl regex substitutions directly on filenames.


Same here (Mepis 11). I don't know perl, but I do know sed, and the perlexpr arg for rename looks like a sed s command to me. I generally use it for simple renaming, but I usually do a dry run first using the -n (no rename) option to make sure it's going to do what I want it to. For more complicated renaming, I tend to write a quick Python script that uses Python's re module to handle the regex stuff. The Python re.sub() function has a rather handy feature: as an alternative to specifying a fixed replacement string, you can specify a function that gets called for every non-overlapping occurrence of the pattern string.
User avatar
PM 2Ring
 
Posts: 2587
Joined: Mon Jan 26, 2009 3:19 pm UTC
Location: Mid north coast, NSW, Australia

Re: Batch file Rename Special Characters

Postby troyp » Sun Mar 03, 2013 7:50 am UTC

Yeah, I think Perl's regex syntax is an extension of sed's - I can't get sed to work reliably on special characters either. Perl (and probably sed) does have escapes to quote special characters (\Q and \E for quote and end-quote), but even using those I remember having trouble (at least when renaming files, which is when I've tried them).

I also use Python for more complicated renaming jobs, but there's an approach I'd like to try instead for some cases. Use a script that simply stores the filenames in a temp file, opens it in your favourite editor, let's you edit it and when you finish it renames them accordingly. For really simple stuff it's a bit cumbersome, but for nontrivial tasks I think it's elegantly simple: just let your editor do the work - chances are you know how to use that competently :-). I know there's plenty of times I've done renaming in Python and thought "I could do this easier in emacs - I should write a function to do it from there". Then recently I was looking for something in the Ranger man page and I noticed it uses a similar approach for file renaming, which is when I thought that instead of an emacs function, I should just write a script you use from the command line that opens emacs in the terminal. (Which is easier, because I don't know emacs lisp very well.)
troyp
 
Posts: 398
Joined: Thu May 22, 2008 9:20 pm UTC
Location: Lismore, NSW

Re: Batch file Rename Special Characters

Postby EvanED » Sun Mar 03, 2013 5:35 pm UTC

troyp wrote:I also use Python for more complicated renaming jobs, but there's an approach I'd like to try instead for some cases. Use a script that simply stores the filenames in a temp file, opens it in your favourite editor, let's you edit it and when you finish it renames them accordingly. For really simple stuff it's a bit cumbersome, but for nontrivial tasks I think it's elegantly simple: just let your editor do the work - chances are you know how to use that competently :-). I know there's plenty of times I've done renaming in Python and thought "I could do this easier in emacs - I should write a function to do it from there". Then recently I was looking for something in the Ranger man page and I noticed it uses a similar approach for file renaming, which is when I thought that instead of an emacs function, I should just write a script you use from the command line that opens emacs in the terminal. (Which is easier, because I don't know emacs lisp very well.)
Oh wow, I actually really like that idea. I'm not sure how much I'd actually use it, but it's really neat. :-)

It's slightly similar to a shell function that I wrote I call editpathvar (I can post it if there's interest), which takes a variable like PATH or LD_LIBRARY_PATH, splits the :-separated components onto separate lines, opens it in an editor, and after you change stuff, re-joins the lines and sets the variable to the new value.
EvanED
 
Posts: 3765
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI

Re: Batch file Rename Special Characters

Postby phlip » Mon Mar 04, 2013 4:22 am UTC

troyp wrote:I also use Python for more complicated renaming jobs, but there's an approach I'd like to try instead for some cases. Use a script that simply stores the filenames in a temp file, opens it in your favourite editor, let's you edit it and when you finish it renames them accordingly. For really simple stuff it's a bit cumbersome, but for nontrivial tasks I think it's elegantly simple: just let your editor do the work - chances are you know how to use that competently :-). I know there's plenty of times I've done renaming in Python and thought "I could do this easier in emacs - I should write a function to do it from there". Then recently I was looking for something in the Ranger man page and I noticed it uses a similar approach for file renaming, which is when I thought that instead of an emacs function, I should just write a script you use from the command line that opens emacs in the terminal. (Which is easier, because I don't know emacs lisp very well.)

There's a tool called "vidir" (in the moreutils package in Debian and Ubuntu) that does essentially this. Spits a list of filenames to a temp file, invokes $EDITOR, and then when you're done, renames (and/or deletes) files according to the changes you've made. In the same package there's also "vipe" which does the same thing with pipelines (reads from stdin, writes to tempfile, runs $EDITOR, reads tempfile, writes to stdout). Both of those are implemented in Perl, so it's pretty simple to copy them across to other systems that don't have that package (eg Cygwin).
While no one overhear you quickly tell me not cow cow.
but how about watch phone?
User avatar
phlip
Restorer of Worlds
 
Posts: 6734
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia

Re: Batch file Rename Special Characters

Postby troyp » Mon Mar 04, 2013 10:20 am UTC

EvanED wrote:It's slightly similar to a shell function that I wrote I call editpathvar (I can post it if there's interest), which takes a variable like PATH or LD_LIBRARY_PATH, splits the :-separated components onto separate lines, opens it in an editor, and after you change stuff, re-joins the lines and sets the variable to the new value.

You don't have a windows version, do you? Every time I use Windows and have to edit the path in that stupid bloody dialog box, I promise myself that next time I'll google/write a path setting program to be rid of it. ;-)

phlip wrote:There's a tool called "vidir" (in the moreutils package in Debian and Ubuntu) that does essentially this. Spits a list of filenames to a temp file, invokes $EDITOR, and then when you're done, renames (and/or deletes) files according to the changes you've made. In the same package there's also "vipe" which does the same thing with pipelines (reads from stdin, writes to tempfile, runs $EDITOR, reads tempfile, writes to stdout). Both of those are implemented in Perl, so it's pretty simple to copy them across to other systems that don't have that package (eg Cygwin).

Hey, cool. They can join vimpager and vifm and the rest of the family. ;-) They're not immediately usable on my system, though. The text is very dark. On guake, where I first tried it, it's literally black - same as the background. All I could see were the blue line numbers and some random bits of filenames turned blue*. In other terminals (roxterm, urxvt), I can barely see the text (although I can make it more visible by reducing the opacity of the window). At first I thought vidir wasn't working (in the obvious way) either, but I think it was just laggy (I noticed the input was lagging at one point of vipe).

The other issue is that these programs seem to insist on $EDITOR. Which is fine for vipe, but not vidir. My $EDITOR is what I use for quick edits at the command line (vi), but for renaming I want my most powerful and familiar editor (emacs). Vi would probably be fine if I knew it better, but I only use it for simple stuff. I think programs using $EDITOR for nontrivial tasks should offer their own $APPNAME_EDITOR env var and/or a switch to use something else.

There's not much documentation. I'll probably have to have a look at the source. I just hope it's the type of Perl that's friendly to non-perl speakers. :-)

* not sure what that's about - it's often (but not always) digits and it starts/stops at natural separators like space and "." (but not consistently)
troyp
 
Posts: 398
Joined: Thu May 22, 2008 9:20 pm UTC
Location: Lismore, NSW

Re: Batch file Rename Special Characters

Postby phlip » Mon Mar 04, 2013 10:40 am UTC

Hmm, maybe vim is picking up something and triggering some broken syntax highlighting mode? Weird.

As for $EDITOR... perhaps you can just alias "emdir" as "env EDITOR=emacs vidir"? Alternatively, it wouldn't be hard to edit the script to check another environment variable... the code isn't particularly complicated, or Perlish/obfuscated (essentially the same thing). Wouldn't be hard to add another variable to check.
While no one overhear you quickly tell me not cow cow.
but how about watch phone?
User avatar
phlip
Restorer of Worlds
 
Posts: 6734
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia

Re: Batch file Rename Special Characters

Postby PM 2Ring » Mon Mar 04, 2013 10:59 am UTC

troyp wrote:Yeah, I think Perl's regex syntax is an extension of sed's - I can't get sed to work reliably on special characters either. Perl (and probably sed) does have escapes to quote special characters (\Q and \E for quote and end-quote), but even using those I remember having trouble (at least when renaming files, which is when I've tried them).

Well, you could always resort to using hex codes:
Code: Select all
echo "ABC" | sed 's/\x41/\x42/'


troyp wrote:I also use Python for more complicated renaming jobs, but there's an approach I'd like to try instead for some cases. Use a script that simply stores the filenames in a temp file, opens it in your favourite editor, let's you edit it and when you finish it renames them accordingly. For really simple stuff it's a bit cumbersome, but for nontrivial tasks I think it's elegantly simple: just let your editor do the work - chances are you know how to use that competently :-).

Interesting, but slightly scary: I'd prefer something that clearly shows you both the old & new names. Dumb mistakes in batch renaming can be messy to repair. :)

One technique I used to use a lot was "two-stage scripting": the batch renaming script doesn't do the renaming directly, instead, it builds a script full of rename commands, which you can check & modify in your editor before you run it.
User avatar
PM 2Ring
 
Posts: 2587
Joined: Mon Jan 26, 2009 3:19 pm UTC
Location: Mid north coast, NSW, Australia

Re: Batch file Rename Special Characters

Postby EvanED » Mon Mar 04, 2013 5:32 pm UTC

troyp wrote:
EvanED wrote:It's slightly similar to a shell function that I wrote I call editpathvar (I can post it if there's interest), which takes a variable like PATH or LD_LIBRARY_PATH, splits the :-separated components onto separate lines, opens it in an editor, and after you change stuff, re-joins the lines and sets the variable to the new value.

You don't have a windows version, do you? Every time I use Windows and have to edit the path in that stupid bloody dialog box, I promise myself that next time I'll google/write a path setting program to be rid of it. ;-)
I thought about writing one, but I don't think I ever did. But there are a couple you can find online (I'll do the googling for you :-)); see this or this. I feel like I've used the latter, but a long time ago.

PM 2Ring wrote:One technique I used to use a lot was "two-stage scripting": the batch renaming script doesn't do the renaming directly, instead, it builds a script full of rename commands, which you can check & modify in your editor before you run it.
I've done stuff like that before too; for file in *; echo mv $file $file, redirect the output to some file, edit, run.
EvanED
 
Posts: 3765
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI

Re: Batch file Rename Special Characters

Postby troyp » Tue Mar 05, 2013 12:13 pm UTC

phlip wrote:Hmm, maybe vim is picking up something and triggering some broken syntax highlighting mode? Weird.

Yeah, I don't know what's triggering it...the temp file just has the standard alphanumerical soup for a name, so it's not an extension.
update: it's stopped happening. It works fine in all terminals now. The only changes I can think of is (a) I installed vim-scripts, which I think has some colour schemes (although I assume it doesn't automatically "install" one); (b) I closed a VM (but I reopened it and it didn't affect vidir).

As for $EDITOR... perhaps you can just alias "emdir" as "env EDITOR=emacs vidir"? Alternatively, it wouldn't be hard to edit the script to check another environment variable...

Yeah, I'll use an alias. I like aliases. :-) Although ironically I won't be able to use it from emacs itself since it doesn't load my aliases when I execute a shell command. (I should start loading a shell mode buffer on startup and rebinding C-! to a function that sends a line to the shell buffer and if there's output, switches to it until you press 'q')


PM 2Ring wrote:Interesting, but slightly scary: I'd prefer something that clearly shows you both the old & new names. Dumb mistakes in batch renaming can be messy to repair. :)

One technique I used to use a lot was "two-stage scripting": the batch renaming script doesn't do the renaming directly, instead, it builds a script full of rename commands, which you can check & modify in your editor before you run it.

I decided the risk was minimal since you'd be editing them interactively (even using macros etc, you could still step through them and approve each one), could use undo/redo, compare them to a directory listing etc. Possibly I'm just being complacent, though. If it was a script specifically for emacs, maybe it could open the original names in another buffer to the right, but that wouldn't work for arbitrary editors. I did consider having the original names off to the side in the same temp file, but I figured it was too likely to interfere with editing operations (and you'd have to quote filenames or otherwise handle spaces (not to mention newlines)).

A code generation strategy is a good idea; alternatively, you could keep a record of the changes to enable (possibly automatic) reversion. Or a combination, where the script renames the files itself but generates a .undo-rename script in the directory that you can use to revert the changes. (Anyway, I'll see how vidir works before I bother writing a script myself.)

I thought about writing one, but I don't think I ever did. But there are a couple you can find online (I'll do the googling for you :-)); see this or this. I feel like I've used the latter, but a long time ago.

heh, thanks :-). I'll use one of those next time. Unless vidir works off the bat on Cygwin, in which case I could use that. (It might even work with native Windows Perl, for that matter - I don't know how portable Perl's library functions are.)[ edit: muddled different problems ]
Last edited by troyp on Tue Mar 05, 2013 9:11 pm UTC, edited 1 time in total.
troyp
 
Posts: 398
Joined: Thu May 22, 2008 9:20 pm UTC
Location: Lismore, NSW

Re: Batch file Rename Special Characters

Postby phlip » Tue Mar 05, 2013 12:26 pm UTC

I use the vidir script from the Ubuntu repos under Cygwin all the time at work, it just copied straight over without any changes. It'd probably work OK on a native Windows version of perl, with a couple of tweaks (eg getting the editor from a more convenient place than %ENV)... getting "vipe" to work under a native Windows perl would take a bit more work, since it uses /dev/tty fanciness to be able to connect the editor to the terminal even though stdin and stdout are pipes (vidir does the same thing if you tell it to read a list of files from stdin, which would also need tweaking to work under native Windows).
While no one overhear you quickly tell me not cow cow.
but how about watch phone?
User avatar
phlip
Restorer of Worlds
 
Posts: 6734
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia

Re: Batch file Rename Special Characters

Postby troyp » Tue Mar 05, 2013 9:06 pm UTC

I avoid using the native shell anyway, especially for anything nontrivial. It's infuriating. I only use it in the rare case I have some kind of Windows issue with Cygwin/Msys, Powershell might be okay, but I don't know it.

What the hell was I thinking last night, anyway? I can't use vidir to edit env vars! I was getting confused between the two different things I was talking about. :oops:
troyp
 
Posts: 398
Joined: Thu May 22, 2008 9:20 pm UTC
Location: Lismore, NSW


Return to The Help Desk

Who is online

Users browsing this forum: No registered users and 3 guests