Move files listed in Excel to a certain folder

MMShaggy

New Member
Joined
Oct 10, 2017
Messages
3
I have multiple excel files that have everything on Sheet2 and row "A" with file names such as:

2012-015GHEW-BW-1981-b1_1.0_0001.tif
2012-015GHEW-BW-1982-b1_3.0_0014.tif
2012-015GHEW-BW-1982-b1_7.1_0008.tif

I would like to use these Excel files to move all files listed from folder "C:\Test" to a folder named "C:\Test2", I thought I could do it with one of your VB script's but I get one error

"Run-time error '76'"
Path not found.

the error is on this line of code
Code:
FileCopy oldFile, newFile


This is the full script I'm using:
Code:
Sub SetUp4a()  Dim rng As Range
  Dim oldPath As String
  Dim newPath As String
  Dim oldFile As String
  Dim newFile As String
  
  '================================================
  'Set the paths to the folders you are processing
  'REMEMBER END BACKSLASH
  '================================================
  oldPath = "C:\Test\"
  newPath = "C:\Test2\"
  
  '=======================================================
  'set up the start of the range you want to loop through
  'EDIT SHEET NAME AND FIRST CELL ADDRESS IF NECESSARY
  '======================================================
  Set rng = Sheets("Sheet2").Range("A1")
  
   '==================================================
  'loop through column B until you find an empty cell
  '==================================================
  Do Until rng = ""
      
      'build up the full path to the old file
      oldFile = oldPath
      
      'build up the full path to the new file you want to create
      newFile = newPath
  
      'copy the old file to the new new folder
      FileCopy oldFile, newFile
    
    'get the next file by moving down one row
    Set rng = rng.Offset(1, 0)
  Loop
End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
You'll need to replace...

Code:
oldFile = oldPath

with

Code:
oldFile = oldPath & rng.Value

And, you'll need to replace...

Code:
newFile = newPath

with

Code:
newFile = newPath & rng.Value

Also, since you want to move those files from one folder to another, you'll need to use the Name statement. So you'll need to replace...

Code:
FileCopy oldFile, newFile

with

Code:
Name oldFile As newFile

Hope this helps!
 
Upvote 0
You'll need to replace...

Code:
oldFile = oldPath

with

Code:
oldFile = oldPath & rng.Value

And, you'll need to replace...

Code:
newFile = newPath

with

Code:
newFile = newPath & rng.Value

Also, since you want to move those files from one folder to another, you'll need to use the Name statement. So you'll need to replace...

Code:
FileCopy oldFile, newFile

with

Code:
Name oldFile As newFile

Hope this helps!


Thank you for the quick reply, I made the changes but the same line is still giving me a Run-time error '53' "File not found".
Would this happen if all files in the list are not in the source folder? If so is there a way to continue looking down the list and moving only the ones it does find in the source folder? Thank you again for your expertise I really appreciate your help.


Code:
Sub SetUp4a()  Dim rng As Range
  Dim oldPath As String
  Dim newPath As String
  Dim oldFile As String
  Dim newFile As String
  
  '================================================
  'Set the paths to the folders you are processing
  'REMEMBER END BACKSLASH
  '================================================
  oldPath = "C:\Test\"
  newPath = "C:\Test2\"
  
  '=======================================================
  'set up the start of the range you want to loop through
  'EDIT SHEET NAME AND FIRST CELL ADDRESS IF NECESSARY
  '======================================================
  Set rng = Sheets("Sheet2").Range("A1")
  
   '==================================================
  'loop through column B until you find an empty cell
  '==================================================
  Do Until rng = ""
      
      'build up the full path to the old file
      oldFile = oldPath & rng.Value
      
      'build up the full path to the new file you want to create
      newFile = newPath & rng.Value
  
      'copy the old file to the new folder
      Name oldFile As newFile
    
    'get the next file by moving down one row
    Set rng = rng.Offset(1, 0)
  Loop
End Sub
 
Upvote 0
You can use the Dir and Len functions to test whether the file exists. The Dir function returns the filename if it exists, otherwise it returns an empty string. Then you can use the Len function to test for a filename or empty string.

Code:
[FONT=Courier New]    [COLOR=darkblue]Do[/COLOR] [COLOR=darkblue]Until[/COLOR] rng = ""
      
      [COLOR=green]'build up the full path to the old file[/COLOR]
      oldFile = oldPath & rng.Value
      
      [COLOR=darkblue]If[/COLOR] Len(Dir(oldFile, vbNormal)) > 0 [COLOR=darkblue]Then[/COLOR]
        [COLOR=green]'build up the full path to the new file you want to create[/COLOR]
        newFile = newPath & rng.Value
    
        [COLOR=green]'copy the old file to the new folder[/COLOR]
        Name oldFile [COLOR=darkblue]As[/COLOR] newFile
      [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
    
    [COLOR=green]'get the next file by moving down one row[/COLOR]
    [COLOR=darkblue]Set[/COLOR] rng = rng.Offset(1, 0)
  [COLOR=darkblue]Loop[/COLOR][/FONT]

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,636
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top