Delete, copy or move xls files

DanielS

Board Regular
Joined
Sep 4, 2011
Messages
53
I have the following folder: D:\Nov 11\..... There are subfolders for each working date (like 11012011, 11022011 etc) and each subfolder contains around 800 xls files. Out of these 800 files, I want to retain only 250 files and delete all other files. I have made a master file named "Fileslist.xls" with full names of all the 250 files (with extension) that I wish to retain. This job can done in several ways, like 1) Delete all nonmatiching files 2) Copying matching files in a new folder 3) Moving matching files in a new folder. I am working with Excel 2000.

I have searched extensively for a macro of this type but couldnt find any. Could anyone amonth the forum members help me out with this task?

Thanks
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Thank you for your reply and also the caution regarding deleting files.

As far as my problem is concerned, the link that you have suggested is of little use because I want the macro to match the names of the files from my master file "Fileslist.xls" and proceed to copy or move the files. The names of the files that I want to retain are listed in Column B of Fileslist.xls. Only those files should be selected from the 800 odd files and then move or copy them to a new directory.

Could someone come up with a solution for this problem, as I have to do this task for each working day.
 
Upvote 0
Be sure to test this code before relying on it.
Be sure to modify the 4 constants listed after the comments that delimited with //

Code:
Option Explicit
Sub MoveFiles()
    'This code should be located in a standard module in the Master List workbook
    'Be sure to modify the 4 constants listed after the comments that delimited with //
 
    Dim strFolderA As String
    Dim strFolderB As String
    Dim strFile As String
    Dim strMasterListWorksheetName As String
    Dim iSaveFileNameColumn As Integer
    Dim Cnt As Long
    Dim oFound As Object
 
    '//Source folder path//
    strFolderA = "D:\Nov 11\"
 
    '//Destination folder path//
    strFolderB = "D:\Master List Files\"
 
    '//Worksheet in this work book that contains the list of files to move//
    strMasterListWorksheetName = "Sheet1"
 
    '//Column number in strMasterListWorksheetName that contains the files to move//
    iSaveFileNameColumn = 1 'A-1, B=2, C=3, etc.
 
    If Right(strFolderA, 1) <> "\" Then strFolderA = strFolderA & "\"
    If Right(strFolderB, 1) <> "\" Then strFolderB = strFolderB & "\"
 
    If Dir(strFolderA, 16) <> "." Then
        MsgBox "Directory " & strFolderA & " does not exist.  Correct reference in this code and try again."
        GoTo End_Sub
    End If
 
    If Dir(strFolderB, 16) <> "." Then
        MsgBox "Directory " & strFolderB & " does not exist.  Correct reference in this code and try again."
        GoTo End_Sub
    End If
 
    'To filter for .xlsx files, change "*.*" to "*.xlsx"
    strFile = Dir(strFolderA & "*.*")
 
    'Clear background colors in master list
    ThisWorkbook.Worksheets(strMasterListWorksheetName).Columns("A:A").Interior.ColorIndex = -4142
 
    Do While Len(strFile) > 0
 
        Set oFound = ThisWorkbook.Worksheets(strMasterListWorksheetName).Columns("A:A") _
            .Find(What:=strFile, LookIn:=xlValues, LookAt:=xlWhole, _
            SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
            MatchCase:=False, SearchFormat:=False)
 
        If Not oFound Is Nothing Then
            'File was on list
            oFound.Interior.ColorIndex = 27 'Color the cell on the master list yellow
            Cnt = Cnt + 1
            Name strFolderA & strFile As strFolderB & strFile 'move the file
        End If
        strFile = Dir
    Loop
    MsgBox Cnt & " file(s) have been transfered to " & strFolderB, vbInformation
 
End_Sub:
 
End Sub
 
Upvote 0
Thanks for the code. I will check it and revert back to you with the result.

Thanks once again.
 
Upvote 0
Hi ajaxme and pbornemeier:

Unfortunately, I am getting error at the following stage:

Set oFound = ThisWorkbook.Worksheets(strMasterListWorksheetName).Columns("A:A") _
.Find(What:=strFile, LookIn:=xlValues, LookAt:=xlWhole, _
SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)

To test the code, I have created folders exactly as given in the code. My source folder is D:\Nov 11\ which contains 892 "*.csv" files. Destination folder is D:\Master List Files\ My workbook is "MasterFileList1.xls". In Sheet1, in column B (right from B1 to B227) there are 227 file names (With extension .csv) that I wish to move out of 892 files. So I have put
iSaveFileNameColumn = 2 (I hope I am correct?).
strMasterListWorksheetName = "Sheet1" This I have kept as it as the file names are in Sheet1 itself. I am working in Excel 2000.

So could you tell me where I have gone wrong? Is Columns("A:A") correct because I have nothing in column A of the said workbook. I am sorry but I have very superficial knowledge of VBA, so forgive my ignorance.
 
Last edited:
Upvote 0
Column A:A is not correct (my mistake). Change "A:A" in this line of code to iSaveFileNameColumn:
Rich (BB code):
        Set oFound = ThisWorkbook.Worksheets(strMasterListWorksheetName).Columns(iSaveFileNameColumn) _ 
            .Find(What:=strFile, LookIn:=xlValues, LookAt:=xlWhole, _
             SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
             MatchCase:=False, SearchFormat:=False)
If you are still getting an error, please tell me which one. I do not have a copy of Excel 2000, this does run in Excel 2003.

The newer versions of Excel have a lot more capabilities. You may want to consider getting Microsoft Office 2010 (about $125) or if you can get a student discount around $80. If the company you work for has an agreement with Microsoft, you can get it for $9.95. Check <cite>http://www.microsofthup.com </cite>for details. <cite></cite>
 
Upvote 0
Hi Phil

I am still getting the same error.

Run-time error "448"
Named argument not found.

I have created new source folder and new master workbook to test the code as follows:

Folder D:\Nov 11 which contain following csv files.
3MINDIA.csv
AARTIDRUG.csv
AARTIIND.csv
AARVEEDEN.csv
ABAN.csv
ABB.csv
ABGSHIP.csv
ABIRLANUV.csv
ABSHEKIND.csv
ACE.csv
ADHUNIK.csv
ADORWELD.csv
ADSL.csv


My workbook is MasterFileList1.xls and Column B of sheet1 contains the following names:
<TABLE style="WIDTH: 131pt; BORDER-COLLAPSE: collapse" border=0 cellSpacing=0 cellPadding=0 width=174 x:str><COLGROUP><COL style="WIDTH: 131pt; mso-width-source: userset; mso-width-alt: 6363" width=174><TBODY><TR style="HEIGHT: 12.75pt" height=17><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; WIDTH: 131pt; HEIGHT: 12.75pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" height=17 width=174>3MINDIA.csv</TD></TR><TR style="HEIGHT: 12.75pt" height=17><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; HEIGHT: 12.75pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" height=17>AARTIDRUG.csv</TD></TR><TR style="HEIGHT: 12.75pt" height=17><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; HEIGHT: 12.75pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" height=17>ABAN.csv</TD></TR><TR style="HEIGHT: 12.75pt" height=17><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; HEIGHT: 12.75pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" height=17>ABL.csv</TD></TR><TR style="HEIGHT: 12.75pt" height=17><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; HEIGHT: 12.75pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" height=17>ACE.csv

Only one name ABL.csv does not have a corresponding file in the folder in which case the code should move on to the next name in column B. I am running the macro from this workbook only.

I hope this information will help you to correct the code accordingly. All codes for Excel 2003 work very well with Excel 2000 also.

Thanks


</TD></TR></TBODY></TABLE>
 
Upvote 0
Remove , SearchFormat:=False from the .Find statement. A little research shows that is is not supported in Excel 2000.

Rich (BB code):
Set oFound = ThisWorkbook.Worksheets(strMasterListWorksheetName).Columns(iSaveFileNameColumn).Find(What:=strFile, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False)




</PRE>
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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