DOS Batch If Files Exists Rename and Move

September 22, 2011 at 17:50:08
Specs: Windows XP

I want to create DOS Batch file that is going to check if there are files with same name in other folder, and if exist to rename it by adding some numbers in filename. Something like this, this is directory structure:


Now i want that batch file to do above what something like this:

if in mypic\ exist same filenames like in mypic\newfolder
then rename mypic\newfolder\*.bmp files that are same
adding some numbers to files in mypic\*.bmp
move mypic\*.bmp to mypic\newfolder\


See More: DOS Batch If Files Exists Rename and Move

Report •

September 22, 2011 at 18:01:10
@echo off

For /f a% in (listofdirsandfiles.txt) do (
If exist file.txt ren a% a%1234numbers

You would need to dir and output the new file names to a file, and the use another for loop to move them.

Something like that. If nobody else answers, ill write something that is more complete when i get home. Im on a tablet at the pub right now. :-)

Report •

September 23, 2011 at 08:18:11
@echo off

REM First we need to create a list of files in one of the locations
REM so that we can compare items in that list to the second location
REM and look for a match.

dir c:\mypic /b *.bmp > location1list.tmp

REM now we'll use that to compare to the other location
REM and list any files that match
for /f %%a in (location1list.tmp) do dir c:\mypic\newfolder\%%a > location2list.tmp

REM ok so now we have a list of all the files in location 2
REM that have the same name as a file in location 1
REM now all we have to do is rename those

for /f %%a in (location2list.tmp) do ren c:\mypic\newfolder\%%a 1234numbers%%a

REM ok so now all of the files in location 2
REM that matched a file name in location 1
REM have been renamed with prefix like 1234numbers*.bmp
REM now all we have to do is move the files from location 1
REM to location 2

for %%a in (location1.tmp) do move /Y c:\mypic\%%a c:\mypic\newfolder

Report •

Related Solutions

Ask Question