PC Review


Reply
Thread Tools Rate Thread

Batch file "for" command renames file twice

 
 
Nebulon
Guest
Posts: n/a
 
      3rd Nov 2006
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?

 
Reply With Quote
 
 
 
 
Nebulon
Guest
Posts: n/a
 
      3rd Nov 2006
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.)

 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ie Command to Wait for "File Download" and "Save As" windows brianatee@hotmail.com Microsoft Excel Programming 11 24th Apr 2008 11:20 AM
Using "for" command in a batch file Chris Windows XP General 2 28th Feb 2007 04:27 AM
How to? batch contacts' "File As" from "L, F" to "F L"? orbii Microsoft Outlook Discussion 1 20th Dec 2006 11:05 AM
Files Not Sorted in "Select File" Dialogs and "dir" Command a_gera80@hotmail.com Windows XP General 4 2nd Jan 2005 07:38 AM
Using "new line" in batch file for command Peter Rossiter Windows XP General 8 9th Apr 2004 01:47 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:29 AM.