Solved

Why does my Excel VBA file system object procedure return a 'rogue' file.

Posted on 2014-09-16
23
415 Views
Last Modified: 2014-09-16
Hi All,

I have an Excel VBA procedure which checks a folder to see if there is any file residing there. If there is then it copies the file to a destination folder.

However, because the file check is in a For Next loop, and even though I only have one file in the folder, it processes an 'invisible' file before exiting the loop. I know this because when I put it in Debug and test the values it shows the second time around the loop that the 'filename' is "Thumbs.db". God knows where this file has come from because it doesn't display in Windows Explorer.

Consequently it is affecting my processing because my filename variable changes to this value.

Any clue as to what this might be ? How can I test the folder without having to loop, as I'll only ever have either no files in the folder or only one file in the folder.

My code is enclosed...

' Check if a file has been moved into the Input folder
    Set fs = CreateObject("Scripting.FileSystemObject")

    strSourceFolder = "C:\Users\Toco\Status Report\Data\Input"
    strDestinationFolder = "C:\Users\Toco\Status Report\Data\Archive"
    Set r = fs.GetFolder(strSourceFolder)

    For Each f In r.Files
       fs.CopyFile strSourceFolder & "\" & f.Name, strDestinationFolder & "\" & f.Name
       strFileName = f.Name
    Next

Open in new window

0
Comment
Question by:Tocogroup
  • 10
  • 5
23 Comments
 
LVL 24

Accepted Solution

by:
Phillip Burton earned 500 total points
Comment Utility
Thumbs.db is an internal file, created when you look at the folder in Windows Explorer, and it stores thumbnail views of the files. You can't easily delete it.

Therefore, you should test for its existence, and take appropriate actions, i.e.

For Each f In r.Files
    If UCase(f.Name) <> "THUMBS.DB" then
       fs.CopyFile strSourceFolder & "\" & f.Name, strDestinationFolder & "\" & f.Name
       strFileName = f.Name
    End If
    Next
0
 

Author Comment

by:Tocogroup
Comment Utility
Well how about that !  A hidden file.

Returning to my loop, I have a number of issues with it as it doesn't seem to allow me to assign the file to a variable which I can then use to open/copy/save as/delete the file in subsequent processing.

Everytime i try to assign it as per the code below I get a 'Run-time error 9 Subscript out of range', which from my experience has nothing to do with arrays.

     For Each f In r.Files
        If UCase(f.Name) <> "THUMBS.DB" Then
           fs.CopyFile strSourceFolder & "\" & f.Name, strDestinationFolder & "\" & f.Name
           strFileName = f.Name
        End If
     Next

     Set wbInput = Workbooks.Open(strSourceFolder & "\" & strFileName)

Open in new window


I'm sure there must be a simpler way of managing files than using FileSystemObject ??
0
 
LVL 24

Expert Comment

by:Phillip Burton
Comment Utility
Have you tried FileCopy (instead of CopyFile)?

How about an ON ERROR RESUME NEXT command as well?

If you want to check for hidden files, then you can always catalogue the folder first, using something like the programme in www.filecats.co.uk - that shows hidden files (as well as system files etc).
0
 

Author Comment

by:Tocogroup
Comment Utility
I've requested that this question be closed as follows:

Accepted answer: 0 points for Tocogroup's comment #a40325194

for the following reason:

Thanks for your assistance. You've been more than helpful.
Toco
0
 
LVL 24

Expert Comment

by:Phillip Burton
Comment Utility
I have solved the problem.
0
 

Author Comment

by:Tocogroup
Comment Utility
Ah ok
0
 

Author Comment

by:Tocogroup
Comment Utility
I've requested that this question be closed as follows:

Accepted answer: 0 points for Tocogroup's comment #a40325194

for the following reason:

Much obliged
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 24

Expert Comment

by:Phillip Burton
Comment Utility
Please award the points.
0
 

Author Comment

by:Tocogroup
Comment Utility
But I've awarded the points...twice
0
 

Author Comment

by:Tocogroup
Comment Utility
I've requested that this question be closed as follows:

Accepted answer: 0 points for Tocogroup's comment #a40325194

for the following reason:

I've now awarded three lots of 500 points. I think there's a problem with the system !
0
 
LVL 24

Expert Comment

by:Phillip Burton
Comment Utility
:-)
0
 

Author Comment

by:Tocogroup
Comment Utility
Well yes.....that's what I'm doing. I have been using EE for many years now so I do know how to award points.
I've attached a screenshot of when I accept Philip's solution. I then select the A radio button and then the Submit button.

?? Strange
Capture.PNG
0
 

Author Comment

by:Tocogroup
Comment Utility
I'm not selecting my own comments !
0
 

Author Comment

by:Tocogroup
Comment Utility
Hah...nice one !

Just a thought though....I'm running this in Internet Explorer 8 and it renders awfully. Unfortunately my employees are not so sympathetic and won't allow me to download the latest version of IE or, even better, Firefox.

Will be interesting to see if this reoccurs.

Regards
Toco
0
 

Author Comment

by:Tocogroup
Comment Utility
Thanks, but like Nectar points I'm not sure I'd know what to do with them.      :-)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now