Windows rename

Aug 22, 2016 265 Replies

I have a folder with many subdirectories of the form


99D100
99D102 etc

and I want to rename them all as



Z100 Z102 etc.


I've tried variations of the command-line REN command. It renames files, with wild cards, but I can't make it rename directories.


Any suggestions?



There are around 100 to rename, so I guess I could do them one at a time.



John Larkin Highland Technology, Inc picosecond timing precision measurement jlarkin att highlandtechnology dott com http://www.highlandtechnology.com

Use a good editor to create a batchfile that does what you want.

E.g. in vi: !!ls %s/99D\(.*\)/REN & Z\1/ :w renam.bat :q

then run renam.bat

Unfortunately most editors in Windows will have more trouble doing that, but you get the idea.

Maybe something like this:

formatting link

Free!

formatting link
formatting link

Total commander can do multi rename tool. It's great, for all sort of other stuff also

Cheers

Klaus

Sorry for the poor english...

"can do" = "has a"

Recently I needed to do something similar. Seems the wildcarding in the Windows command window is pretty lame. I had to generate a batch file with separate lines for each file to be renamed. Codewright didn't have any trouble with doing the name text replacement.

Still, I'd like to get something that is still live, Codewright has been bought and sold so many times it's a lame duck. I think you can still buy it for $350 or so, but there is ZERO support.

Rick C

So at the cmd line can you type in "help ren".. or "help"?

I get a list of stuff.

George H.

Ren dir1 dir2

Maybe the wild cards mess it up.

Cheers

When I was trying to do this the other day I found many web pages explaining how REN could not do this. It can substitute letters based on wildcards, but can't wholesale remove sections of the names. Best to use an editor to edit a file list into a set of individual REN commands.

Others seem to have found tools that let them do what they want.

Rick C

Install MinGW and MSYS (both free), and you get most of the tools of an UNIX installation, including the Bash shell.

-TV

I did not check it but I think you can get "vi" for Windows in some form. Cross-compiled version of vim under cygwin or similar.

However, you will only consider that a "good editor" after you have spent considerable time training your brain and fingers to issue commands like the above without having to look in the manual.

It is of course much quicker than using the mouse. I have been using vi for 30 years, so no issues with that. (although I now see a small typo in the above... never mind)

Isn't this the kind of thing powershell (MS's scripting utility) is supposed to do? If not, why not!

NB, I haven't looked at powershell, since I have little interest nor use for windows nowadays.

My experience is that it is usually a lot more work to code a script that does something like this, than to generate a batch file using a text editor that does the same thing.

The examples shown in this thread confirm that.

Besides, when you write a script you need to be very careful when running it the first time, maybe there is a coding error that makes you lose all your files (or all but one).

When using the editor method, you can review the result much more easily before running it.

Of course a script is the way to go when it is a repeatedly occuring task. For single-shot actions I always use the editor method.

(in Linux it is even easier because you do not have to write the script to a file, you can just type 1G!Gsh in vi to execute the contents of the buffer as a script)

You can use regex's and capture groups with back-references (just like with "real" OS's! :> )

But, I suspect (unless you are very comfortable with advanced regex features) you are more likely to "do damage" as you try to fabricate the *correct* expression for this.

[I don't think there is a "dry run" capability in PS so you test your expression with live filesystem contents]

I beg to differ. The example /bin/sh script I wrote can be given a "dry run" trivially:

for name in * do if [ -d ${name} ] then newname=Z${name#99D} echo renaming $name as $newname # mv ${name} ${newname} fi done

I've been building (reasonably complete) file hierarchy instances via a script (i.e., script has to execute as root in order to be able to access the entire hierarchy so errors can be *painful*!) and stub each action as above before unleashing it on the live filesystem.

When you're dealing with hundreds/thousands of files, trying to create all the "copy" commands explicitly is way too tedious. E.g. link /dev/[dt]ty0[0-4] instead of ln -h /dev/dty00 $DEST/dev/dty00 ln -h /dev/dty01 $DEST/dev/dty01 ln -h /dev/dty02 $DEST/dev/dty02 ln -h /dev/dty03 $DEST/dev/dty03 ln -h /dev/dty04 $DEST/dev/dty04 ln -h /dev/tty00 $DEST/dev/tty00 ln -h /dev/tty01 $DEST/dev/tty01 ln -h /dev/tty02 $DEST/dev/tty02 ln -h /dev/tty03 $DEST/dev/tty03 ln -h /dev/tty04 $DEST/dev/tty04 The "link()" version can easily be stubbed to indicate all of the commands that *will* be issued without actually doing anything.

[Imagine making a link of /usr/share/man/* "by hand" ]

I think it is all MUCH too cumbersome for a single-shot action as I described earlier. You need to write a script, to run it in dry mode observing the output, then edit and run it again.

I just do: vi !!ls /dev/[dt]ty0[0-4] :%s!.*!ln -h & DEST/&! (look at what it is going to do)

1G!Gsh :q!

Job done.

powershell includes "cmdlets", filtering, globbing and many more bash/sh/csh/zsh concepts. Hence it /not/ just a scripting language.

The OP noted "I've tried variations of the command-line REN command. It renames files, with wild cards, but I can't make it rename directories." and my comment is in that context.

Whether or not you choose to use scripting/globbing/etc is a personal choice which does not affect the ability or otherwise of the cmdlets etc to do what the OP wants.

moving the variable part to the left will be tricky, last time I checked ren couldn't do that.

search or ask on superuser.com, I've done that sort of thing in the past and forgotten the details other than it started with FOR.

This email has not been checked by half-arsed antivirus software

Join the Discussion

Have something to add? Share your thoughts — no account required.

Didn't find your answer?

Ask the community — no account required