Solved

batch file needed to rename files with a random number

Posted on 2003-10-29
4
6,818 Views
Last Modified: 2013-12-28
I'm looking for a batch file that will simply rename all .bmp files in a folder to <randomnumber>.bmp.

Would like to do it with for-do and the cmd %random% variable, but i can't work it out.

Any help is appreciated

0
Comment
Question by:yabbedy
  • 2
4 Comments
 
LVL 2

Expert Comment

by:EschbacherG
ID: 9642382
Hey yabbedy,

The problem is, the %random% variable is set once for each type you run the batch program.  

Here's what i did:

You have two batch files:     rename_main.bat   and   rename_file.bat

rename_main.bat  looks like:



for /f %%a in ('dir /b ^| find "bmp"') do (

call  rename_file.bat  %%a

)




rename_file.bat looks like

move %1  %random%.jpg



I think this should help you out.


0
 
LVL 8

Expert Comment

by:nader alkahtani
ID: 9644165
For %%f in (c:\path\*.bmp) do ren c:\path\%random%.bmp

call %0
0
 
LVL 8

Expert Comment

by:nader alkahtani
ID: 9644191
details :

c:\path\*.bmp :

PATH means your folder that contains *.bmp files

call %0  :

when in duplicate names error occurs it will rerun the batch file again then give the bmp files new value .
0
 
LVL 83

Accepted Solution

by:
oBdA earned 100 total points
ID: 9645438
This should do the trick for you; just call it in the directory you want to rename the files in, or change the file mask to reflect your target directory.
Currently, it's in test mode, it will only display the rename command it would otherwise issue. Remove the "ECHO" where indicated to run it for real.
As usual: No warranties included, use it at your own risk, test it before you apply it in earnest.
Note: This will only run on W2k/XP.

====8<----[RenRnd.cmd]----
@echo off
setlocal
set FileMask=*.bmp

for /f "tokens=*" %%a in ('dir /b /a:-d "%FileMask%"') do call :process "%%a"
goto :leave

:process
:loop
set rnd=%Random%
if exist %rnd%%~x1 goto loop
:: *** Test mode: Remove the "ECHO" at the beginning of the next line to "arm" the script:
ECHO ren %1 %rnd%%~x1
goto :eof

:leave
====8<----[RenRnd.cmd]----

EschbacherG,
there are several flaws in your script:
* it can't handle file names with spaces,
* it will rename files or directories containing the string "bmp" anywhere (bmpimages.txt),
* it will not handle files named picture.BMP.
* it will rename the .bmp files to .jpg

NADIR,
several flaws in your script as well:
* it will obviously run into an endless loop
* it will not rename anything to start with (you forgot the "%f" in the ren command.)
* it will try rename all files to the same random number for the reason the EschbacherG already stated: Since the "for" loop is a single command, the pseudo environmental variable "random" only gets expanded once.
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Selecting Operating System at boot screen 4 65
How to start a hyper-v in safe mode 5 72
Problem to Alipay 10 24
Do Not Reply on Exchange 2010 1 14
Storage devices are generally used to save the data or sometime transfer the data from one computer system to another system. However, sometimes user accidentally erased their important data from the Storage devices. Users have to know how data reco…
If you get continual lockouts after changing your Active Directory password, there are several possible reasons.  Two of the most common are using other devices to access your email and stored passwords in the credential manager of windows.
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question