OT: (possibly) Randomly naming files.

  • Thread starter Thread starter Sid Knee
  • Start date Start date
S

Sid Knee

I have 1000 - 2000 files in a directory and would like to rename them
with random filenames - the objective being to destroy the current file
(name) sequence when these are picked up by an application.

The actual names are unimportant. A series of (say) 8-digit random
numbers would seem to be an obvious choice but it doesn't really matter.

I was thinking of using some WD-40 on my programming skills and trying
to write something in BASIC but if anyone knows an easier way or if
there's something out there that will do that ....
 
Sid Knee said:
I have 1000 - 2000 files in a directory and would like to rename them
with random filenames - the objective being to destroy the current file
(name) sequence when these are picked up by an application.

The actual names are unimportant. A series of (say) 8-digit random
numbers would seem to be an obvious choice but it doesn't really matter.

I was thinking of using some WD-40 on my programming skills and trying
to write something in BASIC but if anyone knows an easier way or if
there's something out there that will do that ....

You can do it with an ordinary batch file:
@echo off
dir /b /a-d > c:\dir.txt
for /F "tokens=*" %%* in (c:\dir.txt) do call :Sub %%*
del c:\dir.txt
goto :eof

:Sub
set name=%*
for /F "tokens=*" %%* in ('echo %name%') do set ext=%%~x*
set R=%random%%random%
if exist %R%%ext% goto :Sub
echo ren "%name%" "Q%r%%ext%"

All existing files will be renamed to a random number preceded
by the letter Q. The file extension will be left as it is. Remove the
word "echo" from the last line to activate the script.
 
Sid said:
Superb Pegasus, you came through again. Thanks a bunch!

I'll put the WD-40 back on the shelf :-)

Pegasus, I tried this with a slightly odd result:

There were 1411 files (including the .BAT) in the directory. It renamed
only 1396 of these, the remainder being untouched.

I don't see anything unique about the number 1396 in either decimal or
hex (574H). The .BAT did change it's own name along the way though,
would that be a problem? Didn't see any error message but it could have
been too fast for me.
 
Sid Knee said:
Pegasus, I tried this with a slightly odd result:

There were 1411 files (including the .BAT) in the directory. It renamed
only 1396 of these, the remainder being untouched.

I don't see anything unique about the number 1396 in either decimal or
hex (574H). The .BAT did change it's own name along the way though,
would that be a problem? Didn't see any error message but it could have
been too fast for me.

You just gave the answer yourself. Locating the batch
file inside the folder where you rename your files is
equivalent to sawing off the brach you sit on. When the
batch file renamed itself, it stopped operating!
 
Pegasus said:
You just gave the answer yourself. Locating the batch
file inside the folder where you rename your files is
equivalent to sawing off the brach you sit on. When the
batch file renamed itself, it stopped operating!

Yes, of course. I put it in the directory and simply ran it from Windows.

I'll try putting it on the command path and opening a cmd window in that
directory.

Thanks,
 
Sid said:
Yes, of course. I put it in the directory and simply ran it from Windows.

I'll try putting it on the command path and opening a cmd window in that
directory.

Just did this (actually, I didn't put it on the command path since it's
moderately destructive and I wouldn't want to run it accidentally
inside, say, system32 :-). I put it in its own directory and called up
the full path).

And I still essentially get the same result: in this case, of 1410
files, it renamed 1390 of them.
 
Sid said:
Just did this (actually, I didn't put it on the command path since it's
moderately destructive and I wouldn't want to run it accidentally
inside, say, system32 :-). I put it in its own directory and called up
the full path).

And I still essentially get the same result: in this case, of 1410
files, it renamed 1390 of them.

I think I have it. The files that didn't get renamed each had an
ampersand (&) in the filename; none of the ones that did get renamed had
the ampersand. When I replaced the ampersand, they were all renamed
correctly.

.... which has left me curious as to why this happens? Is " &" an illegal
filename character at the cmd-window level?
 
Sid Knee said:
I think I have it. The files that didn't get renamed each had an
ampersand (&) in the filename; none of the ones that did get renamed had
the ampersand. When I replaced the ampersand, they were all renamed
correctly.

... which has left me curious as to why this happens? Is " &" an illegal
filename character at the cmd-window level?

& is a so-called "poison" character in batch files because it
has a special meaning, same as %. These symbols are acceptable
for file names but they are very, very difficult to deal with in
advanced batch files.
 
Pegasus said:
& is a so-called "poison" character in batch files because it
has a special meaning, same as %. These symbols are acceptable
for file names but they are very, very difficult to deal with in
advanced batch files.

Interesting ..... thanks. I appreciate the help as always.
 
Back
Top