Random file naming in cmd

  • Thread starter Thread starter hDy
  • Start date Start date
H

hDy

I was wondering if there was a way to rename files to a random name using the command line in windows xp. I would be doing this from a batch file but I don't have the slightest Idea how to do it. I've thought of two ways to do this but don't know if batch supports the.

The first Idea is with Variables. haveing a Variable that would turn out in the end to be a random charter. I don't care what the filenames are, I just need each one to be diffrent.

The second Idea is having a txt file that has a list of random words and some how making a batch script that used the rename function and picks a random line to rename the file. Is this possible?
 
@echo off & setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
set fdir=c:\work
for /f "tokens=*" %%c in ('dir /b %fdir%') do (
echo %%~nc.!RANDOM!%%~xc
:: ren "%%c" "%%~nc.!RANDOM!%%~xc"
)

After testing remove the echo line and uncomment the ren (rename) line

The renamed files will look something like this:
(filename.randomnumber.extension)
wmicounters.4951.vbs
wmicpu.22521.vbs
wmiCPUusage.16008.vbs
wmidiskdrive.25129.vbs
wmidiskquota.21320.vbs
wmievents.1560.vbs
WMIGetIP.19598.vbs
wmigetnetcard.18470.vbs
wmigroup.25951.vbs
WmiInDST.9956.vbs
 
Slight problem. The fdir env needs to be quoted like below.

@echo off & setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
set fdir=c:\work
for /f "tokens=*" %%c in ('dir /b "%fdir%"') do (
echo ren %%c %%~nc.!RANDOM!%%~xc
:: ren "%%c" "%%~nc.!RANDOM!%%~xc"
)
 
I was wondering if there was a way to rename files to a random name using
the command line in windows xp. I would be doing this from a batch file but
I don't have the slightest Idea how to do it. I've thought of two ways to do
this but don't know if batch supports the.
The first Idea is with Variables. haveing a Variable that would turn out
in the end to be a random charter. I don't care what the filenames are, I
just need each one to be diffrent.
The second Idea is having a txt file that has a list of random words and
some how making a batch script that used the rename function and picks a
random line to rename the file. Is this possible?

You could also use the output of the command ".GetTempFile",
which is stored in a variable called %#TempFile%.
This will work CONSISTENTLY across NT/2K/XP/K3.

The variable contains a complete filespec so if you just
wanted the name, you would use:

CALL ntlib.cmd /Init /Quiet
%.GetTempFile% %.Silent%
FOR %%A IN (%#TempFile%) DO SET "RandomFile=%%~nA"

*******

For more info about the GetTempFile command, see
(http://TheSystemGuard.com/MtCmds/GetValue/GetTempFile.htm)

For detailed info about the structure of variable #TempFile, see
(http://TheSystemGuard.com/NTCmdLib/Constants/TempFile.htm)

".Silent%" and other plain-English redirection commands are at
(http://TheSystemGuard.com/MtCmds/RedirRapids)

*******

To request your FREE copy of ntlib.cmd, including over 150
of these "Mounted Commands", see
(http://ntlib.com)

*******

-tsg

/-----------------+---------------+----------------------\
| COMPATIBILITY | CLARITY | SPEED |
| Write code ONCE | Make it clear | THEN...Make it fast! |
\-----------------+---------------+----------------------/
400+ command-line resources using ONLY native NT commands!
(http://TheSystemGuard.com/default.asp#MasterCommandList)
 
Back
Top