Solved

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

Posted on 2014-02-24
3
324 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 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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

749 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