Solved

batch file needed to rename files with a random number

Posted on 2003-10-29
4
6,771 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
Comment Utility
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
Comment Utility
For %%f in (c:\path\*.bmp) do ren c:\path\%random%.bmp

call %0
0
 
LVL 8

Expert Comment

by:nader alkahtani
Comment Utility
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 82

Accepted Solution

by:
oBdA earned 100 total points
Comment Utility
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

Shouldn't all users have the same email signature?

You wouldn't let your users design their own business cards, would you? So, why do you let them design their own email signatures? Think of the damage they could be doing to your brand reputation! Choose the easy way to manage set up and add email signatures for all users.

Join & Write a Comment

A few months ago I had an issue with LaserJet 1020 printer which was installed to XP and Windows 7.  It was installed to XP and working, but when I tried to connect from a Windows 7 PC, it would attempt connection and then fail.  Sometimes the Spool…
Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
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 will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.

772 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now