Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / May 2008

Tip: Looking for answers? Try searching our database.

Check if file exists

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
WembleyBear - 19 May 2008 12:40 GMT
Hi

I'm fairly new to this & I've read a couple of similar posts but they don't
seem to answer my question fully. I am importing a csv file (c:\nlexp.csv)
from our Dealer Management system on a daily basis, however, if the export
from DMS is run more than once in the day, the exported file is renamed by
the DMS (c:\18nlexp.csv).

Before the file is imported, I need to:

1) Check if the file "C:\18nlexp.csv" exists. If it doesn't, then do nothing.
2) If it does exist, and the date on the file <> today, do nothing.
2) If it does exist, and the date on the file = today, then rename the file
to "C:\nlexp.csv" and continue with the import

I fine with renaming the file & the import, it's the checks I'm having a
problem with. Any help would be appreciated.

Many thanks
Martyn

Excel 2000, Windows Server 2003 over Citrix PS4
Jon Peltier - 19 May 2008 13:00 GMT
Try something like this:

dim sNewFile as String
dim sOldFile as String

sNewFille = "c:\nlexp.csv"
sOldFile = "c:\18nlexp.csv"

If Len(Dir(sOldFile)) > 0 Then
   If Int(FileDateTime(sOldFile)) = Int(Now) Then
       Name sOldFile sNewFile
       '' continue with import
       Kill sOldFile '' OPTIONAL
   End If
End If

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
Peltier Technical Services, Inc. - http://PeltierTech.com
_______

> Hi
>
[quoted text clipped - 21 lines]
>
> Excel 2000, Windows Server 2003 over Citrix PS4
WembleyBear - 19 May 2008 16:39 GMT
Thanks very much Jon - that did the trick!

Martyn

> Try something like this:
>
[quoted text clipped - 44 lines]
> >
> > Excel 2000, Windows Server 2003 over Citrix PS4
Joel - 19 May 2008 13:14 GMT
the easies way of check if a file exists is to use dir(pathname\filename).  
If it return an empty sting the the file doesn't exist.  the dir will accept
a wildcard (*) as path of the filename.

exist = dir("c:\temp\book1.xls")
if exist <> "" then
  msgbox("file Exists")
end if

You can get a file date using the following

Set fso = CreateObject _
     ("Scripting.FileSystemObject")

Set fs = fso.GetFile("c:\temp\book1.xls")
mydate = fs.DateLastModified

You can combine the two as follows

Set fso = CreateObject _
     ("Scripting.FileSystemObject")
exist = dir("c:\temp\book1.xls")
if exist = "" then
  msgbox("file doesn't Exists")
else
  Set fs = fso.GetFile("c:\temp\book1.xls")
  mydate = fs.DateLastModified
  msgbox("Last Modified : " & mydate)
end if

> Hi
>
[quoted text clipped - 18 lines]
>
> Excel 2000, Windows Server 2003 over Citrix PS4
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.