[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now


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

Posted on 2014-02-24
Medium Priority
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


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

Question by:mainrotor
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 12

Accepted Solution

Gregory Miller earned 2000 total points
ID: 39884389
If My.Computer.FileSystem.FileExists("\\TEST_DIRECTORY\ACCOUNT\MYFile.txt") Then
    MsgBox("File not found.")
End If

Open in new window

LVL 83

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


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.


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


Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

656 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