Batch file "for" command renames file twice

N

Nebulon

I seem to be having a problem with a group of batch files.

There are four -- two I've gotten working, and two with a problem.

First two are:

rand_add.bat

for %%x in (c:\a_particular_path\*) do radd1 "%%x" "%%~nx%%~xx"


radd1.bat

set /a a=%random% %% 10
set /a b=%random% %% 10
set /a c=%random% %% 10
set /a d=%random% %% 10
set /a e=%random% %% 10
ren %1 %a%%b%%c%%d%%e%%2


This pair prepends to the name of each file in the target directory a
random, five-digit number, with exactly five digits. (Just using
%random% could result in shorter numbers with no leading zeros. I know
this implementation will be a bit wobbly-distributed, moduloing 0-32767
by 10 and all; so sue me. It seems to work well enough.)

The matching pair attempts to remove the first five characters from the
name of every file in the same directory, and consists of rand_rem.bat
and rrem1.bat. Of course, this means (barring pathologically long file
names) that running rand_add immediately followed by rand_rem, should
have no net effect.

rand_rem.bat

for %%x in (c:\sharing\working4\*) do rrem1 "%%x" "%%~nx%%~xx"


rrem1.bat

set x=%2
ren %1 "%x:~6%



Unfortunately, it doesn't work correctly. It seems that some of the
files get renamed, then picked up by the "for" under the new name and
renamed again. Sometimes a file gets several multiples of five
characters stripped from the beginning of its name.

I was able to get this far with set /?, for /?, and Google, but now I
seem to be stymied. I don't see any obvious way to make rand_rem's for
ignore new file names -- or any obvious reason why it sometimes sees
the same file repeatedly under successive names, whereas that in
rand_add works as intended, for that matter. (Shouldn't they either
both work or both have the same bug?)

It's not clear from various Google searches and for /? how to make this
work, short of creating a whole extra directory for every file to be
moved to, then back from, which seems woefully inefficient (and if that
directory then gets used for anything else, will cause problems of its
own).

Any suggestions?
 
N

Nebulon

Nebulon wrote:
[snip]

Eh. A use of the hairy "for /f" syntax seems to've fixed it.

for /F "usebackq tokens=*" %%x in (`dir /b c:\sharing\working4\*`) do
rrem1 "c:\sharing\working4\%%x" "%%x"

I changed rand_add as well, to be sure. A couple tests and they seem to
work correctly now, and reliably so.

It seems that doing the awkward dir-parsing makes an unchanging copy of
the directory listing for the loop to run off that doesn't change as
the files get renamed. Or something like that. It's a bit of a hack,
but better than creating a whole extra temp directory just for it to
move files to and from!

Now if anyone else has a similar problem there's something for them to
google, and they may even be spared trying to parse the "for /F" and
usebackq documentation too.

At least this random-prefix adding and stripping turned out not to
require I cobble together a pair of C programs.

(As for why? So I can use Explorer or other tools to sort a set of
files into pseudo-random order easily. Use rand_add, then sort by name.
Use rand_rem when done. The fixed width of the random prefix is also
susceptible to being stripped from listings and the like, so a
randomly-ordered list of the files of a directory can now be made with
relative ease and without writing any C.)
 

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