Batch script to move, rename files.?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
jeff p
Posts: 42
Joined: 28 Jul 2009 10:21

Batch script to move, rename files.?

#1 Post by jeff p » 24 Mar 2010 00:45

Hello,

I'm a complete noob regarding scripting, yet i'm desperate to find a batch script for this repeated tasks I'm undergoing.


Basically, I have a source folder "D:/Animation/Temp" with an image sequence.
The sequence number and length vary, ie:
Image_0001.jpeg, Image_0002.jpeg, Image_0003.jpeg, etc..
or sometimes it's Image_0002.jpg, Image_0004.jpg, Image_0006.jpg, Image_0008.jpg, etc..


I'm looking for a script which will copy the first file from "D:/Animation/Temp", rename this to "Face_0001.jpg" and paste it into a destination folder "D:/Animation/Folder_01"
copy the second file from "D:/Animation/Temp" and also rename this "Face_0001.jpg" and paste it into "D:/Animation/Folder_02"
copy the third file from "D:/Animation/Temp" and again rename this "Face_0001.jpg" and paste it into "D:/Animation/Folder_03"

etc... until there are no more files.

To summaries, I'm copying files from a single folder, renaming each file the same name, ie: "Face-001.jpg" but moving them to specific folders.
(Overwriting any files that are already there)

Any ideas how this can be done?

Thanks for any help

Jeff

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Batch script to move, rename files.?

#2 Post by aGerman » 24 Mar 2010 12:00

Untested

Code: Select all

@echo off &setlocal

set "source=D:\Animation\Temp"
set "dest_default=D:\Animation\Folder_"
set "filename=Face_0001.jpg"

set "n=0"
pushd "%source%"
for /f "delims=" %%a in ('dir /a-d /b *.jpg *.jpeg') do set "file=%%a" &call :process
popd
pause
goto :eof

:process
set /a n+=1
set "num=0%n%"
set "num=%num:~-2%"
set "dest_path=%dest_default%%num%"
md "%dest_path%" 2>nul
copy /y "%file%" "%dest_path%\%filename%"
goto :eof


Regards
aGerman

jeff p
Posts: 42
Joined: 28 Jul 2009 10:21

Re: Batch script to move, rename files.?

#3 Post by jeff p » 24 Mar 2010 12:53

Works wonderfully!

Thank you for taking the time to make this.

…just saved me a ton of extra work

Thanks a million! :D

Jeff

jeff p
Posts: 42
Joined: 28 Jul 2009 10:21

Re: Batch script to move, rename files.?

#4 Post by jeff p » 25 Mar 2010 02:03

Hello,

If i may trouble you with one more question?

The code you provided works great!

Is there a way I could combine several of these together under a single batch file?
I've made numerous attempts to group more than one together, but only a few images were copied over., (receiving mostly errors)

I have several (about 20) source and destination folders, with the same image "Face_0001.jpg" being copied over.
Combining everything under one file will give me the ability to easily change the image name/number through a simple Search/Replace function throughout the entire process.

As an example, I've pasted below 3 versions of the same code, with the source and destination path changed.
Yet, I'm unsure what to include between each code that will allow it to run continuously.

Any help would be greatly appreciated.

Thanks

Jeff



Code: Select all

@echo off &setlocal

set "source=D:\Animation\Temp1"
set "dest_default=D:\Animation\Folder1"
set "filename=Face_0001.jpg"

set "n=0"
pushd "%source%"
for /f "delims=" %%a in ('dir /a-d /b *.jpg *.jpeg') do set "file=%%a" &call :process
popd
pause
goto :eof

:process
set /a n+=1
set "num=0%n%"
set "num=%num:~-2%"
set "dest_path=%dest_default%%num%"
md "%dest_path%" 2>nul
copy /y "%file%" "%dest_path%\%filename%"
goto :eof





@echo off &setlocal

set "source=D:\Animation\Temp2"
set "dest_default=D:\Animation\Folder2"
set "filename=Face_0001.jpg"

set "n=0"
pushd "%source%"
for /f "delims=" %%a in ('dir /a-d /b *.jpg *.jpeg') do set "file=%%a" &call :process
popd
pause
goto :eof

:process
set /a n+=1
set "num=0%n%"
set "num=%num:~-2%"
set "dest_path=%dest_default%%num%"
md "%dest_path%" 2>nul
copy /y "%file%" "%dest_path%\%filename%"
goto :eof






@echo off &setlocal

set "source=D:\Animation\Temp3"
set "dest_default=D:\Animation\Folder3"
set "filename=Face_0001.jpg"

set "n=0"
pushd "%source%"
for /f "delims=" %%a in ('dir /a-d /b *.jpg *.jpeg') do set "file=%%a" &call :process
popd
pause
goto :eof

:process
set /a n+=1
set "num=0%n%"
set "num=%num:~-2%"
set "dest_path=%dest_default%%num%"
md "%dest_path%" 2>nul
copy /y "%file%" "%dest_path%\%filename%"
goto :eof

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Batch script to move, rename files.?

#5 Post by aGerman » 25 Mar 2010 12:08

Maybe something like that:

Code: Select all

@echo off &setlocal

set "filename=Face_0001.jpg"

set "source=D:\Animation\Temp1"
set "dest_default=D:\Animation\Folder1"
call :process1

set "source=D:\Animation\Temp2"
set "dest_default=D:\Animation\Folder2"
call :process1

set "source=D:\Animation\Temp3"
set "dest_default=D:\Animation\Folder3"
call :process1

goto :eof



:process1
set "n=0"
pushd "%source%"
for /f "delims=" %%a in ('dir /a-d /b *.jpg *.jpeg') do set "file=%%a" &call :process2
popd
pause
goto :eof

:process2
set /a n+=1
set "num=0%n%"
set "num=%num:~-2%"
set "dest_path=%dest_default%%num%"
md "%dest_path%" 2>nul
copy /y "%file%" "%dest_path%\%filename%"
goto :eof


Regards
aGerman

jeff p
Posts: 42
Joined: 28 Jul 2009 10:21

Re: Batch script to move, rename files.?

#6 Post by jeff p » 26 Mar 2010 01:52

Thanks Allot!

..Works far better than expected.

I really appreciate the help!

Jeff :D

.

Post Reply