Solved

Need help checking existance and deleting a file in VB.Net

Posted on 2014-02-24
3
323 Views
Last Modified: 2014-02-25
I have the following code in my VB.Net app which copies a file from one filepath and moves it to another filepath (see code below).

I want to be able to:
1. Check to see if the file already exists in the destination filepath
2. If the file exists, I want to delete it before copying over the new file
3. Finally, I want to copy the file from the source file path to the destination file path

How do i do this?  Thanks in advance mrotor


MY CURRENT CODE USED TO COPY FILE FROM ONE FILEPATH TO ANOTHER

Dim filename As String = "MYFile.txt" '"TestFile.txt"
Dim source As String = Trim("\\TEST_DIRECTORY\ACCOUNT\")
Dim destination As String = System.IO.Path.Combine("\\FINAL_DIRECTORY\PROD\", filename)
System.IO.File.Copy(source, destination, True)

Open in new window

0
Comment
Question by:mainrotor
3 Comments
 
LVL 11

Accepted Solution

by:
Gregory Miller earned 500 total points
ID: 39884389
If My.Computer.FileSystem.FileExists("\\TEST_DIRECTORY\ACCOUNT\MYFile.txt") Then
    File.Delete("\\TEST_DIRECTORY\ACCOUNT\MYFile.txt")
    File.Copy("\\OTHER_TEST_DIRECTORY\ACCOUNT\MYFile.txt","\\TEST_DIRECTORY\ACCOUNT\MYFile.txt")
Else
    MsgBox("File not found.")
End If

Open in new window

0
 
LVL 80

Expert Comment

by:David Johnson, CD, MVP
ID: 39884392
Dim filename As String = "MYFile.txt" 
Dim source As String = Trim("\\TEST_DIRECTORY\ACCOUNT\")
Dim destination As String = System.IO.Path.Combine("\\FINAL_DIRECTORY\PROD\", filename)
dim filesys
Set filesys = CreateObject("Scripting.FileSystemObject")
If filesys.FileExists(destination) Then filesys.DeleteFile destination
End If 
filesys.CopyFile source,destination,true
set filesys = nothing

Open in new window

0
 
LVL 5

Expert Comment

by:Bob Bender
ID: 39884401
I found a block of VB code on MSDN that copies files ( txt and jpg ) from a source folder to a destination folder, then deletes the source folder files.  

Not a big believer in writing the code for anyone.  After all, being able to modify the code to make it do what you want is the goal of all developers.  How else can you learn.


http://msdn.microsoft.com/en-us/library/system.io.file.delete(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2


The Try/Catch routine is so that it can throw an exception in case of a problem.

To check that a file exists....

     File.Exists(filename) ? "File exists." : "File does not exist."

The ? in the statement is an IF condition...,  1st option is true, second one (after the :) is false.

Can follow if you have questions

bob
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

792 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