Renaming all files with random names

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Shawn@Rogers
Posts: 7
Joined: 13 Feb 2012 15:53

Renaming all files with random names

#1 Post by Shawn@Rogers » 09 Mar 2012 12:01

Good day,

Here is a summary:

Context
Multiple photos with simple names like 1.jpg must be moved to a folder already containing similar file names

Goal
Renaming all files (*.jpg) from a folder with a random file name.jpg to move them to a folder that may contain simple file names.jpg

Attempts
rename x.jpg %RANDOM%.jpg works with one file name but
rename *.jpg %RANDOM%.jpg tries to rename all files with the same instant value of %random% variable.

Additional features
This may also be good to rename it with something like [date]0001.jpg [date]0002.jpg ...

Thank you four your help, my DOS knowledge is getting greater with all of you!

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Renaming all files with random names

#2 Post by foxidrive » 09 Mar 2012 20:45

Is this a one-time task?

The shareware Total Commander has a move/copy feature were it can rename the files being copied if they clash with a another filename, or it can rename the target filename. The feature can do an entire folder without prompting.

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Renaming all files with random names

#3 Post by Aacini » 09 Mar 2012 21:14

To rename&move to random file names:

Code: Select all

@echo off
setlocal EnableDelayedExpansion
cd sourceDir
for %%f in (*.jpg) do (
    call :getNewName
    move "%%f" "targetDir\!newName!.jpg"
)
goto :EOF

:getNewName
set newName=%random%
if exist "targetDir\!newName!.jpg" goto getNewName
exit /B

To rename&move to [date]####.jpg file names:

Code: Select all

@echo off
setlocal EnableDelayedExpansion
cd sourceDir
set i=0
for %%f in (*.jpg) do (
    set /A i+=1
    set newName=000!i!
    set newName=[%date%]!newName:~-4!
    move "%%f" "targetDir\!newName:/=-!.jpg"
)

EDIT: I added setlocal commands that were missed, as foxidrive noted...
Last edited by Aacini on 10 Mar 2012 19:39, edited 1 time in total.

Ocalabob
Posts: 79
Joined: 24 Dec 2010 12:16
Location: Micanopy Florida

Re: Renaming all files with random names

#4 Post by Ocalabob » 09 Mar 2012 22:16

Greetings Shawn@Rogers,

Here's a random approach that you can 'try before you buy'. :)

Code: Select all

::Limited Testing
@echo off
setlocal
for /f "delims=" %%a in ('dir *.jpg /b') do call :ren_it "%%a"
goto :eof
:ren_it
set max=999
set min=1
set /a rng=%random% %% (max - min + 1)+ min
echo ren "%~1" "%rng%_%var%.jpg">>test_me.bat


Have a look at test_me.bat to ensure you're getting your moneys worth
and then move the files.

I prefer Aacini's script which has error checking for the 'move' .

Best wishes!
Last edited by Ocalabob on 10 Mar 2012 21:08, edited 1 time in total.

Ocalabob
Posts: 79
Joined: 24 Dec 2010 12:16
Location: Micanopy Florida

Re: Renaming all files with random names

#5 Post by Ocalabob » 09 Mar 2012 23:16

Re Greetings Shawn@Rogers,

Here's another rename script using the current date. Caveat Emptor!

Code: Select all

::Limited Testing
@echo off
setlocal

set mydate=%date%
set mynewdate1=%mydate:/=%
set mynewdate2=%mynewdate1:~4,8%

for /f "delims=" %%a in ('dir *.jpg /b') do call :date_it "%%a"
goto :eof

:date_it
echo ren "%~1" "%mynewdate2%_%~1">>test_me.bat

Again, check the contents of test_me.bat for desired results then
add your 'move' command.

Best wishes!
Last edited by Ocalabob on 10 Mar 2012 21:09, edited 1 time in total.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Renaming all files with random names

#6 Post by foxidrive » 09 Mar 2012 23:21

Aacini wrote:To rename&move to random file names:

To rename&move to [date]####.jpg file names:



issues here Aacini.

delayed expansion isn't declared so they won't work in a standard system, right?

Shawn@Rogers
Posts: 7
Joined: 13 Feb 2012 15:53

Re: Renaming all files with random names

#7 Post by Shawn@Rogers » 12 Mar 2012 09:15

Thank you for your replies!

Unfortunately, my computer has some major problems, so I will not be able to test your solution now, but I will.

Sorry for the delay.

Post Reply