Solved

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

Posted on 2014-02-24
3
326 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
[X]
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
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 81

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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

726 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