Solved

batch file needed to rename files with a random number

Posted on 2003-10-29
4
6,858 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 84

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

Flexible connectivity for any environment

The KE6900 series can extend and deploy computers with high definition displays across multiple stations in a variety of applications that suit any environment. Expand computer use to stations across multiple rooms with dynamic access.

Question has a verified solution.

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

In this article, I will show you HOW TO: Perform a Physical to Virtual (P2V) Conversion the easy way from a computer backup (image).
How to record audio from input sources to your PC – connected devices, connected preamp to record vinyl discs, streaming media, that play through your audio card: Vista, Windows 7, Windows 8, Windows 8.1 and Windows 10 – both 32 bit & 64.
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

735 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