Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default Check if file exists

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


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,582
Default Check if file exists

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
_______


"WembleyBear" wrote in message
...
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




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default Check if file exists

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


"WembleyBear" wrote:

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


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default Check if file exists

Thanks very much Jon - that did the trick!

Martyn



"Jon Peltier" wrote:

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
_______


"WembleyBear" wrote in message
...
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





Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Check if a File Exists Ray Clark[_2_] Excel Programming 4 June 11th 08 05:13 PM
Check if file exists Jon Excel Discussion (Misc queries) 14 October 4th 07 04:57 PM
Check if file exists... abdrums Excel Programming 3 July 13th 07 05:30 AM
Check if a sheet exists in a file, without opening that file Bogdan Excel Programming 5 March 9th 07 01:46 PM
check if file exists Curt Excel Programming 0 December 7th 05 05:02 PM


All times are GMT +1. The time now is 02:26 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"