Solved

batch file needed to rename files with a random number

Posted on 2003-10-29
4
6,900 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 85

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

Turn your laptop into a mobile console!

The CV211 Laptop USB Console Adapter provides a direct Laptop-to-Computer connection for fast and easy remote desktop access with no software to install.

Question has a verified solution.

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

This article helps those who get the 0xc004d307 error when trying to rearm (reset the license) Office 2013 in a Virtual Desktop Infrastructure (VDI) and/or those trying to prep the master image for Microsoft Key Management (KMS) activation. (i.e.- C…
Sometimes clients can lose connectivity with the Lotus Notes Domino Server, but there's not always an obvious answer as to why it happens.   Read this article to follow one of the first experiences I had with Lotus Notes on a client's machine, my…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

623 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