SVicente's Blog

Follow me and you'll know what I like to do

MS-DOS Batch – copying files between directories

This weekend I took advantage of a free time and I did a task for a friend of mine. He had a specific situation that I decided to solve with a MS-DOS batch. To remember my old times as MS-DOS programmer.

The situation was: a series of mp3 files with coded names that were repeated. Because they were formed by a vinyl-to-cd recorder such that – FILE001.mp3, FILE002.mp3 and so on… And each album was saved in a folder named also coded as FOLD3001, FOLD3002, FOLD3003… My task was: move all mp3 files saved in each folder to ONLY ONE folder as repository and destination.

Graphically represented:

+— FOLD3001
|                           |— FILE001.mp3
|                           |— FILE002.mp3
|                           +– FILE007.mp3
+— FOLD3002
|                           |— FILE003.mp3
|                           |— FILE004.mp3
|                           |— FILE005.mp3
|                           +– FILE007.mp3

So what was my solution? Copy each file to destination folder before replacing its name to one that is preceded by folder name where it is originally saved.

This way:

         |— FOLD3001_FILE001.mp3
         |— FOLD3001_FILE002.mp3
         |— FOLD3001_FILE007.mp3
         |— FOLD3002_FILE003.mp3
         |— FOLD3002_FILE004.mp3
         |— FOLD3002_FILE005.mp3
         +– FOLD3002_FILE007.mp3

To get rid of possible filename collisions.

Thus, follow my two batch files to solve this special problem, but that could be adapted to many other similar situations envolved files and directories to move, copy and rename.

To execute this program:

  1. just copy these 2 files (“renome1.bat, renome2.bat”) to a same folder;
  2. customize according to your preferences: working variables (origin and destiny) in “renome1.bat”
  3. open a ms-dos command window;
  4. run “renome1.bat”;
  5. wait for final result.

Enjoy code!

@echo off

rem --------------------------------------------------------
rem file.....: renome1.bat
rem author...: twitter = @svicente99
rem date.....: Aug.11th 2012
rem objective: rename a set of files in multiple directories
rem --------------------------------------------------------

:set working variables
set workDIR=D:\_PHILIPS
set v_copyDIR=D:\_COPY2

if not exist %v_copyDIR%\nul mkdir %v_copyDIR%

for /d %%X in ( %workDIR%\*. ) do call renome2.bat %%X


del filename.txt
del dorename.bat

rem Show all copy files have being copied
dir %v_copyDIR%\. /w

set v_copyDIR=
set workDIR=


@echo off

: --------------------------------------------------------

: file.....: renome2.bat
: author...: twitter = @svicente99
: date.....: Aug.11th 2012
: objective: inner a directory rename files
:            called from 'renome1.bat'

: important references:


: --------------------------------------------------------

if %1.==GoSub. goto GoSub

set v_folder=%1
echo Processing mp3 files in: %v_folder%
for %%f in ( %v_folder%\*.mp3 ) do call %0 GoSub dir /b/n %%f %%v_folder%%
goto End

set v_file_origin=%4
set v_folder=%5

goto SubRemove_MainFolder

rem Execute a dir /b/n to get only name of file
%2 %3 %4 > filename.txt

rem Set its name to a msdos variable using a special for
for /F "delims=" %%X in (filename.txt) do set v_nmFile=%%X

rem Create a batch command to copy file to "CopyDir"
echo copy /b/y %v_file_origin% %v_copyDIR%\%v_folder%_%%1 ^>nul > dorename.bat

rem Execute batch command to copy effectivelly
dorename.bat %v_nmFile%

goto End

set str=%v_folder%
set str=%str:D:\_PHILIPS\=%
set v_folder=%str%
set str=
goto ReturnFor_SubRemove

set v_nmFile=
set v_folder=
set v_file_origin=


August 13, 2012 - Posted by | Programming | , ,

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: