Rename from batch file?

T

Tom McDonald

Does anyone know of a rename utility that can be run from a batch file,
in order to put the letter "a" at the beginning of all files in a folder
when the batch file is executed? Thank you for any suggestions.

Long Explanation, in case the details are important.
I'm using Grabby to take multiple screen captures of the Drempels screen
saver.
It numbers the captures sequentially, Grabby1.bmp, Grabby2.bmp, etc.
But it will overwrite a previous set of captures, if I neglect to move or
rename them when I restart the Grabby program.
So I would like to start Grabby from a batch file which will first
introduce a difference into the filenames in the folder to which Grabby
saves its captures. I do hope this was clearly explained.
 
A

Andrew Graham

Tom said:
Does anyone know of a rename utility that can be run from a batch
file, in order to put the letter "a" at the beginning of all files in
a folder when the batch file is executed? Thank you for any
suggestions.

Long Explanation, in case the details are important.
I'm using Grabby to take multiple screen captures of the Drempels
screen saver.
It numbers the captures sequentially, Grabby1.bmp, Grabby2.bmp, etc.
But it will overwrite a previous set of captures, if I neglect to
move or rename them when I restart the Grabby program.
So I would like to start Grabby from a batch file which will first
introduce a difference into the filenames in the folder to which
Grabby saves its captures. I do hope this was clearly explained.

You didn't mention your operating system, but if you use NT/XP with
cmd.exe rather than command.com, that capability is built-in.

for %A in (Gra*.bmp) do ren %A a%A

Be sure to double the percent marks if you put that command in a batch
file.

Andrew Graham
 
T

Tom McDonald

You didn't mention your operating system, but if you use NT/XP with
cmd.exe rather than command.com, that capability is built-in.

for %A in (Gra*.bmp) do ren %A a%A

Be sure to double the percent marks if you put that command in a batch
file.

Andrew Graham

Thank you, Andrew. Yes, details are important, and I was bound to leave
out a few! I'd like to do this on both WinME and XP. I just tested your
suggestion under XP, modifying it to read:

for %A in (*.bmp) do ren %A a%A

and it does exactly what I want. Each time it's executed, it puts an
"a" in front of the pre-existing filename, giving me, after the third
iteration, aaaGrabby01.bmp. This will prevent any files from being
overwritten, no matter how often I forget to empty the folder.

Now, I need to be able to do the same under WinME.

Thank you again for your help.
 
O

omega

Bjorn Simonsen said:
Tom McDonald wrote in



Above should work fine in any (MS-)DOS, in dos box under WinME
too I'm sure.

The command.com cannot ForInDo. :(

I am able to run that batch by using an NT cmd emulator to process it. Runs
fine on w98, using win95cmd.exe. Or an alternate (have not tried) might be
cmd-reactos.

win95cmd.exe
http://groups.google.com/[email protected]&output=gplain
http://www.neuro.gatech.edu/users/cwilson/cygutils/consize/index.html

cmd-reactos
http://www.neuro.gatech.edu/users/cwilson/cygutils/cmd-reactos/index.html

Note. This is win98, where I can sub the command processor to do such batch
files. I don't know anything about possible WinMe limitations.

The other choice would be an external utility. For example, XXcopy. Or -
SED, yes? (I haven't quite reached the last chapter of the manual yet. *g*)
 
T

Tom McDonald

The command.com cannot ForInDo. :(

I am able to run that batch by using an NT cmd emulator to process it.
Runs fine on w98, using win95cmd.exe. Or an alternate (have not tried)
might be cmd-reactos.

win95cmd.exe
http://groups.google.com/[email protected]
.dfncis.de&output=gplain
http://www.neuro.gatech.edu/users/cwilson/cygutils/consize/index.html

cmd-reactos
http://www.neuro.gatech.edu/users/cwilson/cygutils/cmd-reactos/index.ht
ml

Note. This is win98, where I can sub the command processor to do such
batch files. I don't know anything about possible WinMe limitations.

The other choice would be an external utility. For example, XXcopy. Or
- SED, yes? (I haven't quite reached the last chapter of the manual
yet. *g*)

Yes, it works for me on WinME. Must be "hybrid vigor". Only limitation
is that it truncates long file names.
 
O

omega

Tom McDonald said:
Yes, it works for me on WinME. Must be "hybrid vigor". Only limitation
is that it truncates long file names.

How about some extra enclosing quotes?

win95cmd/q/c FOR %%F IN (*.bmp) DO REN "%%F" "a%%F"
 
O

omega

Yes, it works for me on WinME. Must be "hybrid vigor". Only limitation
How about some extra enclosing quotes?

win95cmd/q/c FOR %%F IN (*.bmp) DO REN "%%F" "a%%F"

Sorry!

The command above takes care of the LFN, but introduces another kind of
problem. The "a" character is getting prepended to the new filenames more
than once. I cannot say I know what's going on.

Back to the drawing board. :(
 
O

omega

Hopefully I've got it right this time. The command below is giving me
acceptable results:

win95cmd/q/c FOR %%F IN (*.bmp) DO REN "%%F" a*

 
T

Tom McDonald

Hopefully I've got it right this time. The command below is giving me
acceptable results:

win95cmd/q/c FOR %%F IN (*.bmp) DO REN "%%F" a*

http://www.neuro.gatech.edu/users/cwilson/cygutils/consize/index.html

On WinME, using the native command.com,

FOR %%A IN (*.bmp) DO REN "%%A" a*

renames all files literally to a* (without extension), while

FOR %%A IN (*.bmp) DO REN "%%A" "a%%A"

eventually produces filenames like aAAAAAA~1.bmp which are NOT 8.3
(that's seven A's, a tilda, and a number), but look just as weird.

Thank you for the extra effort, but I think I can live with the strange
filenames, as long as they are unique.
 
O

omega

Tom McDonald said:
On WinME, using the native command.com,

FOR %%A IN (*.bmp) DO REN "%%A" a*

renames all files literally to a* (without extension), while

FOR %%A IN (*.bmp) DO REN "%%A" "a%%A"

eventually produces filenames like aAAAAAA~1.bmp which are NOT 8.3
(that's seven A's, a tilda, and a number), but look just as weird.

Wow. *g* Thanks for the results. It is all very strange!
Thank you for the extra effort, but I think I can live with the strange
filenames, as long as they are unique.

I wouldn't settle for it that way. I have the impression there is a wide
choice of third-party command utils that would serve for your objective,
ones where'd be able to run the same batch on either OS, and get results
with proper filenames.

One thing I have to wonder. Is the ForInDo necessary for this situation?
For instance, do you need to have the folder specified in the batch line?
Or the target filenames selected that way? Also, are you hitting single-level
only, or do you have recursed subfolders?

I'm thinking about the fact that I can use a simple, straight-up REN command.

ren *.bmp ren a*.bmp
or ren grab*.bmp agrab*.bmp

Or perhaps those results are again specific to the particular command
processor? In which case, I'd again think it'd be better to go with an
independent utility, so that you can use the same batch in both places.

I had one other question.
Does anyone know of a rename utility that can be run from a batch file,
in order to put the letter "a" at the beginning of all files in a folder
when the batch file is executed? Thank you for any suggestions.

Long Explanation, in case the details are important.
I'm using Grabby to take multiple screen captures of the Drempels screen
saver.
It numbers the captures sequentially, Grabby1.bmp, Grabby2.bmp, etc.
But it will overwrite a previous set of captures, if I neglect to move or
rename them when I restart the Grabby program.
So I would like to start Grabby from a batch file which will first
introduce a difference into the filenames in the folder to which Grabby
saves its captures. I do hope this was clearly explained.

You only want to have a total of two sets preserved? You don't need one of
those IFEXIST THEN REN batches, that makes for a larger number of renamed
groups?
 
O

omega

Tom McDonald said:
On WinME, using the native command.com,

FOR %%A IN (*.bmp) DO REN "%%A" a*

renames all files literally to a* (without extension), while

FOR %%A IN (*.bmp) DO REN "%%A" "a%%A"

eventually produces filenames like aAAAAAA~1.bmp which are NOT 8.3
(that's seven A's, a tilda, and a number), but look just as weird.

One thing I forgot to ask. If you use the win95cmd processor on WinME, with
the batch above, you get the same decent results that I do using it on W98?
 
O

omega

Tom McDonald said:
On WinME, using the native command.com,

FOR %%A IN (*.bmp) DO REN "%%A" "a%%A"

eventually produces filenames like aAAAAAA~1.bmp which are NOT 8.3
(that's seven A's, a tilda, and a number), but look just as weird.

If you want to use the winME command.com for FOR stuff that is supported,
such as that batch, then try this as the first line of your batch:

: LFNFOR ON

I just now learnt of it. Seems to solve that weird LFN problem.
 
O

omega

omega said:
The command.com cannot ForInDo. :(

Sorry about that Bjorn. Turns out you were right. I wasn't even aware until
today(!) The command.com does have some basic support of ForInDo. And I see
that I could have asked my command.com in the first place.

For /?

But I can say to much prefer the For /? output from the cmd emulator. That
one goes on and on with all kinds of talk...
 
T

Tom McDonald

If you want to use the winME command.com for FOR stuff that is supported,
such as that batch, then try this as the first line of your batch:

: LFNFOR ON

I just now learnt of it. Seems to solve that weird LFN problem.

Karen,

Thank you so much for your time and trouble.

No, the project doesn't involve subfolders.
Yes, I wish the ability to make several layers of backup, not just two.
No, I haven't yet tried the win95cmd processor on WinME.

Results so far:

for %%A in (*.bmp) do ren %%A a%%A

works exactly as I wish under XP, producing unique filenames such as
aaaGrabby01.bmp if the batch file is run 3 times and I have neglected to
deal with leftover Grabby*.bmp files from three previous uses.

lfnfor on
for %%A in (*.bmp) do ren %%A a%%A
lfnfor off

works exactly as I wish under WinME. The LFNFOR ON line makes all the
difference.

I'm happy to be able to accomplish my task using native capabilities of
the operating systems.
However, in my searching, I've just now found
*FREEWARE ALERT*
http://www.boolean.ca/renamer/

BK Renamer 1.0

BK Renamer is a command-line utility that allows you to perform
mass renaming of files based on a UNIX-style regular expression.


Now if I could just master enough of regular expressions to select only
the bitmaps and put an "a" in front of the filename.
So far, my efforts are futile.
 
B

Bjorn Simonsen

omega wrote in <[email protected]>:

OP:
Sorry about that Bjorn. Turns out you were right.

You are forgiven.;) For-in-do has been around in all MS command
processors since...I can't remember, at least MS-DOS 3.3 (oldest DOS
manual I have here :). Btw notice while single "%" prefixes is used
when typing FOR-IN-DO variables directly at the command line, use in a
batch script requires double "%%" prefixes. Here is why, from the old
MS-DOS 3.3 manual,adding two lines of context first - to help you sort
out the syntax (they use "%c" in their first example, which they then
comment:)

"To avoid confusion with the %0-%9 batch parameters, the variable
c can be any character except 1,2,3,...,9.!
[...]
"You must use two percent signs (%%) so that one will remain
after the batch parameter (%0-%9) processing is complete.
If you had only %f, instead of %%f, then the batch parameter
processor would see the %, look at f, decide that %f was an error
(a bad parameter reference), and throw out the %f so the *for*
command would never see it.
Note that if you are using the *for* command outside of a batch
file, you should only use one percent sign"

Read more under FOR /?, also have a look at SHIFT /? :)
Btw I noticed you found the LFNFOR ON/OFF command. This is for
Win9x/ME only, not needed on NT plattform (since NT can not run plain
DOS mode, only one mode is needed). In NT all one need to do is
enclose the LFNs (here the place holders) in quotes, as per usual on
the commandline (cmd.exe and command.com both uses spaces to separate
commands, and since LFNs can contains spaces - we must enclose them)
Like:
for %%A in (*.bmp) do ren "%%A" "a%%A"
Belive you must enclose in Win9x too, maybe even also the initial
variable as well. Can't remember (no Win9x running here now).

Questions? May I suggest the experts in <(MS-DOS/Win9x) and <(NT/2k/XP) :)

All the best,
Bjorn Simonsen
 
O

omega

Tom McDonald said:
[...]
for %%A in (*.bmp) do ren %%A a%%A

works exactly as I wish under XP, producing unique filenames such as
aaaGrabby01.bmp if the batch file is run 3 times and I have neglected to
deal with leftover Grabby*.bmp files from three previous uses.

Hi, Tom. What I am thinking about right now would be the matter of
your most preferred filenames? It strikes me that having different
numbers of prepending "a" characters, to separate screen snapshot
sessions, it would not be something I'd find ideal. Because then when
I want to regroup things, I'd have to use fancy work to do that.

On the other hand, if the sets of filenames from each recording session
were prepended with a number, say 01 through 20, or 001 through 100,
that seems like it'd be easier for when one later wants to search,
or regroup, the files.

Or subdirectories, for groups, and not renaming the files? Do you
have any interest in that layout? Each recording session's snapshots
getting automatically moved to subdirectories? For subdirectory
names, perhaps numbers? Or, interest in having those directories
autocreated with the date? XXCopy has a nice slim way to do that.

For renaming a group of files, there is a very basic approach. Not
at all impressive-looking, but on the other hand, it is easy and
direct to read and alter.

cd drive:\directory\grabby\snapshots\

if exist 12grab*.bmp ren 12grab*.bmp 13grab*.bmp
if exist 11grab*.bmp ren 11grab*.bmp 12grab*.bmp
[....]
if exist 01grab*.bmp ren 01grab*.bmp 02grab*.bmp
if exist grab*.bmp ren grab*.bmp 01grab*.bmp

Could also use an xcopy/y in place if preferred. Refinement would be in
the way of setting in an errorlevel thing that it stops and notifies
you if the max number of entries covered in the batch is reached. In
the example, that is 13 sessions. So one would consider a line that says
stop do not continue if 13grab* already exists.

.. . .

OR? Or am I totally off, and you definitely preferred a consistent alpha
character, like the a, to prepend each time? I wanted to find out, either
way.
 
O

omega

Bjorn Simonsen said:
For-in-do has been around in all MS command processors since...
I can't remember, at least MS-DOS 3.3

Got it now. thanks. Yet as you're aware, it is not until NT that the
capabilities expand to become notably interesting. Example,

One of the more frequently asked questions on the net concerns
the processing of a list of text, such as a directory listing,
in a batch procedure. Windows NT users have a real advantage in
the much expanded FOR command. They need only to add the /F switch
to the line to make it process text contained in a file named in
the (Set) part of the command.
ref:http://www.pressroom.com/~tglbatch/list.html

The NT emulator, win95cmd.exe, while there are some notes about it not
providing complete support of FOR, it fortunately does brings a 9x'er
far closer to some of the capabilities.
In NT all one need to do is
enclose the LFNs (here the place holders) in quotes, as per usual on
the commandline (cmd.exe and command.com both uses spaces to separate
commands, and since LFNs can contains spaces - we must enclose them)
Like:
for %%A in (*.bmp) do ren "%%A" "a%%A"

Belive you must enclose in Win9x too, maybe even also the initial
variable as well. Can't remember (no Win9x running here now).

Yes, we need quotes on that first. A directory of filenames containing
spaces otherwise returns a "file not found." Btw, that batch does not
work in the w98 command.com, as it turned out, even with LFNFOR set to
on. The "a" character gets prepended to some of the filenames, not all,
in duplicate, and occasionally in triplicate. It's a mysterious spatter.
Yet I'm not up to attempting to dissect the problem. Batches I'd study
utilizing features of FOR would not be those written for (+ limited by)
the older command.com.
 
O

omega

Tom McDonald said:
http://www.boolean.ca/renamer/

BK Renamer 1.0

BK Renamer is a command-line utility that allows you to perform
mass renaming of files based on a UNIX-style regular expression.

Now if I could just master enough of regular expressions to select only
the bitmaps and put an "a" in front of the filename.
So far, my efforts are futile.

I don't have any regexp skills. But I was able to imitate the first
example, the simpler one, such that an "a" gets successfully prepended
to target files.

bkren "(.*)\.bmp" "a\1.bmp"

[example 1]
| bkren "(.*)\.htm" "\1.html"
| (Change all files/folders in the current directory with the .htm
| extension to have the .html extension.)

For your particular project, though, I don't see that you need something
as fancy as this. Since regular REN will take care of things like adding
characters to the front of filenames. Unless perhaps you were thinking
over other renaming schemes..

I notice how this util handles some sophisticated rename operations...

[examples 2 and 3]
| bkren -s "(.*)100(.*)" "\1303\2"
| This go through all filenames in the current directory and all
| subdirectories and change all instances of "100" in these
| filenames to "303". For example it would change bkren100.zip
| to bkren303.zip. It would also change 100monkeys.gif to
| 303monkeys.gif.
|
| bkren "(.*)\.([^.]*)" "\1_backup.\2"
| Appends _backup to all filenames, but before the file extension.
| For example, changes bkren.txt to bkren_backup.txt and test.html
| to test_backup.html.

Thanks for this find, Tom. I'm keeping it.
 
T

Tom McDonald

Tom McDonald said:
http://www.boolean.ca/renamer/

BK Renamer 1.0

BK Renamer is a command-line utility that allows you to perform
mass renaming of files based on a UNIX-style regular expression.

Now if I could just master enough of regular expressions to select only
the bitmaps and put an "a" in front of the filename.
So far, my efforts are futile.

I don't have any regexp skills. But I was able to imitate the first
example, the simpler one, such that an "a" gets successfully prepended
to target files.

bkren "(.*)\.bmp" "a\1.bmp"

[example 1]
| bkren "(.*)\.htm" "\1.html"
| (Change all files/folders in the current directory with the .htm
| extension to have the .html extension.)

For your particular project, though, I don't see that you need something
as fancy as this. Since regular REN will take care of things like adding
characters to the front of filenames. Unless perhaps you were thinking
over other renaming schemes..

I notice how this util handles some sophisticated rename operations...

[examples 2 and 3]
| bkren -s "(.*)100(.*)" "\1303\2"
| This go through all filenames in the current directory and all
| subdirectories and change all instances of "100" in these
| filenames to "303". For example it would change bkren100.zip
| to bkren303.zip. It would also change 100monkeys.gif to
| 303monkeys.gif.
|
| bkren "(.*)\.([^.]*)" "\1_backup.\2"
| Appends _backup to all filenames, but before the file extension.
| For example, changes bkren.txt to bkren_backup.txt and test.html
| to test_backup.html.

Thanks for this find, Tom. I'm keeping it.

Yes, BK Renamer seems to be very powerful. BK ReplaceEm, from the same
author, is Pricelessware, I believe.

As for me and my screen captures, my needs are very simple. I'm doing
multiple PrntScrns on the Drempels screensaver. My *only* concern is
that files not be overwritten until I have a chance to sort through them
to see which I want to keep. There is no significant difference between
set 1 from Monday, and set 2 from Tuesday; as long as set 2 doesn't
overwrite set 1, I'm happy. But this is the fatal flaw in the Grabby
screen capture program. It starts over with Grabby01.bmp every time it
is started. Thus I need a safety net in case I forget (as I did once) to
move or rename the old files.

Starting Grabby with a shortcut to a batch file which first creates a
"difference" in the file names of any bitmaps in the Grabby SaveTo
folder, is the safety net.

Now, thanks to your efforts, and others who responded to my question, I
have a choice of methods for doing this.

Your regexp

bkren "(.*)\.bmp" "a\1.bmp"

works like a charm, and since BK Renamer is only 48k, this, I think, is
my perfect solution, useful, as you say, under both Operating Systems,
without needing a different batch file for XP and ME, as previously
explained. Although I certainly enjoyed learning how to turn LFNFOR on
and off, something no one should be ignorant of.

As for xxcopy, this is where I started three days ago, to the profit only
of the Aspirin company.

As for regular DOS REN, if that will change Grabby01.bmp to
aGrabby01.bmp, then I've been going in circles, chasing my tail like a
dog. But all I could do was to change Grabby01.bmp to arabby01.bmp, and
a second and third iteration of the command
ren *.bmp a*
produces no differencing at all.

I'm reminded of Schmendrick the Magician, who could turn a glass of water
into a handful of water...

Without your help, Karen, I would have no answer, and now I have two.
Thank you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top