Solved

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

Posted on 2014-02-24
3
318 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:
Technodweeb 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 78

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now