Solved

batch file needed to rename files with a random number

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

Manage your data center from practically anywhere

The KN8164V features HD resolution of 1920 x 1200, FIPS 140-2 with level 1 security standards and virtual media transmissions at twice the speed. Built for reliability, the KN series provides local console and remote over IP access, ensuring 24/7 availability to all servers.

Question has a verified solution.

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

I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
An introduction to the wonderful sport of Scam Baiting.  Learn how to help fight scammers by beating them at their own game. This great pass time helps the world, while providing an endless source of entertainment. Enjoy!
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

738 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