Moving Files from one folder to another using ranges

D

Dominique Feteau

I have two lists. Column A has a file path and Column B has a new file
path. I want the file in Column A to be moved to the path in Column B. For
example:

Column A Column B
C:\Path1\file.tif C:\Path2\file.tif

Everything I've searched for just goes to one specific folder. Anyone have
any suggestions on how to do this?

Niq
 
R

Ron de Bruin

Hi Dominique

Try

Sub Move_Rename_One_File()
'You can change the path and file name
Name "C:\Users\Ron\SourceFolder\Test.xls" As "C:\Users\Ron\DestFolder\TestNew.xls"
End Sub

You can use a cell reference instead of the path/file names

You can create a loop to do all the cells in the range
If you need help with that post back
 
C

Chip Pearson

Assuming that the values in both columns A and B have fully qualified
file names, you can use code like the following:

Sub AAA()
Dim R As Range
For Each R In Range("A1:A10")
If Dir(R.Text) <> vbNullString Then
' file in A exists. delete file
' in B. ignore error if file in
' B does not exist.
On Error Resume Next
' delete the file
Kill R(1, 2).Text
' OR
' send the file to the RecycleBin
' see www.cpearson.com/Excel/Recycle.aspx
Recycle R(1, 2).Text
Name R.Text As R(1, 2).Text
Else
' file in A does not exist.
' do nothing.
End If
Next R
End Sub


If the file named in column B already exists, it will be deleted. If
you want to permanently and irrevocably delete it, use the line of
code with the Kill function and delete the line of code with the
Recycle function. If you want to send the file in column B to the
Recycle Bin, delete the line of code with the Kill function and leave
the line with Recycle in place. The code for the Recycle function is
described and can be downloaded from
www.cpearson.com/Excel/Recycle.aspx.

Change the reference to A1:A10 to the appropriate range in column A.


Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2009
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top