Click here to Skip to main content
15,893,508 members
Home / Discussions / Visual Basic
   

Visual Basic

 
AnswerRe: Dynamic control events Pin
JUNEYT8-Feb-06 8:33
JUNEYT8-Feb-06 8:33 
AnswerRe: Dynamic control events Pin
H@is@here8-Feb-06 9:08
H@is@here8-Feb-06 9:08 
QuestionInterface Pin
Jeeva Jose8-Feb-06 4:11
Jeeva Jose8-Feb-06 4:11 
AnswerRe: Interface Pin
Dave Kreskowiak8-Feb-06 8:20
mveDave Kreskowiak8-Feb-06 8:20 
Question2 windows open when i click F1 in VB code Pin
tamila_tamila8-Feb-06 3:25
tamila_tamila8-Feb-06 3:25 
AnswerRe: 2 windows open when i click F1 in VB code Pin
Dave Kreskowiak8-Feb-06 8:16
mveDave Kreskowiak8-Feb-06 8:16 
GeneralRe: 2 windows open when i click F1 in VB code Pin
tamila_tamila9-Feb-06 21:41
tamila_tamila9-Feb-06 21:41 
QuestionFile System Watcher & File.Copy Method Pin
vcdflow8-Feb-06 2:59
vcdflow8-Feb-06 2:59 
I have built an application that watches a specified folder for any new/changes/deleted files. The program that keeps alist of all changes/added files in a listbox so that it knows what to move when instructed to. The reason this program was built to help my company transfer all changed files from our stage enviroment to our production enviroment for our website.

Mad | :mad:
The entire program seems work, however when a file is copied, it seems to copy the "file.txt" but erases the data inside the original, as well as the new copy of it. Also the program checks to see if the "COPY" already exists, if so it makes a copy-of-the-copy, and puts that in a backup folder. It seems as thought when this process occurs, the data contained in that "COPY" is moved succesfully to the "BACKUP" folder, but the original "COPY" is then erased. showing "0kb". This is a big issues since I really dont' want this program deleteing all of our data when it try to move the files.

If someone can figure out why when I try to copy a file it erases the data, I would be very, very grateful.!!! Roll eyes | :rolleyes: I plan on posting this application working one in articles within the next few days.

Program Uses
File System Watcher
File Copy methods.




<br />
<br />
Imports System.IO<br />
Imports System.Diagnostics<br />
<br />
Public Class Form2<br />
    ' Created 2/7/2005 <br />
    ' Concept applications to watch stage for file changes, and keep record of list. Then moving all changed files to production<br />
    ' Copy all files that have changed, or have been created to new folder based on scheduled timeframe<br />
<br />
    Public watchfolder As FileSystemWatcher<br />
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_startwatch.Click<br />
        watchfolder = New System.IO.FileSystemWatcher()<br />
<br />
        'this is the path we want to monitor<br />
        watchfolder.Path = txt_watchpath.Text<br />
<br />
        'Add a list of Filter we want to specify<br />
        'make sure you use OR for each Filter as we need to<br />
        'all of those <br />
<br />
        ' Specifies that filesystemwatch will watch subdirectories<br />
        watchfolder.IncludeSubdirectories = True<br />
<br />
        watchfolder.NotifyFilter = IO.NotifyFilters.DirectoryName<br />
        watchfolder.NotifyFilter = watchfolder.NotifyFilter Or _<br />
                                   IO.NotifyFilters.FileName<br />
        watchfolder.NotifyFilter = watchfolder.NotifyFilter Or _<br />
                                   IO.NotifyFilters.Attributes<br />
<br />
<br />
        ' add the handler to each event<br />
        AddHandler watchfolder.Changed, AddressOf logchange<br />
        AddHandler watchfolder.Created, AddressOf logchange<br />
        AddHandler watchfolder.Deleted, AddressOf logchange<br />
<br />
        ' add the rename handler as the signature is different<br />
        AddHandler watchfolder.Renamed, AddressOf logrename<br />
<br />
        'Set this property to true to start watching<br />
        watchfolder.EnableRaisingEvents = True<br />
<br />
        btn_startwatch.Enabled = False<br />
        btn_stop.Enabled = True<br />
<br />
        txt_status.Text = "Watching Folder(s) & File(s)"<br />
        'End of code for btn_start_click<br />
    End Sub<br />
<br />
    Private Sub logchange(ByVal source As Object, ByVal e As _<br />
                        System.IO.FileSystemEventArgs)<br />
        If e.ChangeType = IO.WatcherChangeTypes.Changed Then<br />
            txt_folderactivity.Text &= "File " & e.FullPath & _<br />
                                    " has been modified" & vbCrLf<br />
<br />
            ' Check to see if item is already added to the list<br />
            Dim a As Integer<br />
            Dim CaseSense As Integer<br />
            'CaseSense = 0 For CaSe SeNsItIvE<br />
            'CaseSense = 1 for not case sensitive<br />
            ' works with List, Combo<br />
            For a = 0 To changed_files.Items.Count() - 1<br />
                If InStr(1, changed_files.Items(a), e.Name, CaseSense) _<br />
            And Len(changed_files.Items(a)) = Len(e.Name) Then Exit Sub<br />
            Next<br />
<br />
            ' end if exist code<br />
<br />
            changed_files.Items.Add(e.Name) ' adds the modified file to the list<br />
<br />
            txt_itemcount.Text = changed_files.Items.Count() ' updates the item count on-screen<br />
<br />
        End If<br />
        If e.ChangeType = IO.WatcherChangeTypes.Created Then<br />
            txt_folderactivity.Text &= "File " & e.FullPath & _<br />
                                     " has been created" & vbCrLf<br />
<br />
            ' Check to see if item is already added to the list<br />
            Dim a As Integer<br />
            Dim CaseSense As Integer<br />
            'CaseSense = 0 For CaSe SeNsItIvE<br />
            'CaseSense = 1 for not case sensitive<br />
            ' works with List, Combo<br />
            For a = 0 To changed_files.Items.Count() - 1<br />
                If InStr(1, changed_files.Items(a), e.Name, CaseSense) _<br />
            And Len(changed_files.Items(a)) = Len(e.Name) Then Exit Sub<br />
            Next<br />
<br />
            ' end if exist code<br />
<br />
<br />
            changed_files.Items.Add(e.Name) ' adds the modified file to the list<br />
<br />
            txt_itemcount.Text = changed_files.Items.Count() ' updates the item count on-screen<br />
<br />
        End If<br />
        If e.ChangeType = IO.WatcherChangeTypes.Deleted Then<br />
            txt_folderactivity.Text &= "File " & e.FullPath & _<br />
                                    " has been deleted" & vbCrLf<br />
        End If<br />
    End Sub<br />
<br />
    Public Sub logrename(ByVal source As Object, ByVal e As _<br />
                            System.IO.RenamedEventArgs)<br />
        txt_folderactivity.Text &= "File" & e.OldName & _<br />
                      " has been renamed to " & e.Name & vbCrLf<br />
    End Sub<br />
<br />
    Private Sub btn_stop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_stop.Click<br />
        watchfolder.EnableRaisingEvents = False<br />
        btn_startwatch.Enabled = True<br />
        btn_stop.Enabled = False<br />
        txt_status.Text = "Stopped"<br />
    End Sub<br />
<br />
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click<br />
        Dim x As Integer<br />
        Dim count As Integer<br />
        Dim listboxitem As String<br />
        count = changed_files.Items.Count() - 1<br />
<br />
        If txt_watchpath.Text = "" Then<br />
            Dim MsgResult As MsgBoxResult<br />
            MsgResult = MsgBox("You must enter a valid (Folder to watch) path.")<br />
        End If<br />
<br />
        ' Sets the current status of the applications (copying)<br />
        txt_status.Text = "Copying Files to selected location"<br />
<br />
<br />
        ' Display the ProgressBar control.<br />
        ProgressBar1.Visible = True<br />
        ' Set Minimum to 1 to represent the first file being copied.<br />
        ProgressBar1.Minimum = 0<br />
        ' Set Maximum to the total number of files to copy.<br />
        ProgressBar1.Maximum = count<br />
        ' Set the initial value of the ProgressBar.<br />
        ProgressBar1.Value = 0<br />
        ' Set the Step property to a value of 1 to represent each file being copied.<br />
        ProgressBar1.Step = 1<br />
<br />
<br />
<br />
<br />
        For x = 0 To count<br />
<br />
            changed_files.SetSelected(x, True)<br />
            listboxitem = changed_files.SelectedItem.ToString()<br />
<br />
            '    Directories that are being manipulated.<br />
            Dim path As String = txt_watchpath.Text + "\" + listboxitem ' Watch Path<br />
            Dim path2 As String = txt_copyLocation.Text + "\" + listboxitem ' "Copy to" Location<br />
            Dim path3 As String = txt_backupLocation.Text + "\" + listboxitem ' "Copy to (if file exists)-Backup" location<br />
<br />
            Try<br />
                Dim fs As FileStream = File.Create(path)<br />
                fs.Close()<br />
<br />
                '   Ensure that the Copy locations does Not already contain the files<br />
                If File.Exists(path2) Then<br />
                    If File.Exists(path3) Then<br />
                        File.Delete(path3) 'if file exists is in backup location already- delete it<br />
                    End If<br />
                    File.Copy(path2, path3) ' if copylocation file exists , move to backup folder<br />
<br />
                End If<br />
                File.Delete(path2)<br />
                '  Copy the file.<br />
                File.Copy(path, path2)<br />
                'Console.WriteLine("{0} copied to {1}", Path, path2)<br />
<br />
                '  Try to copy the same file again, which should succeed.<br />
                File.Copy(path, path2, True)<br />
                ' Console.WriteLine("The second Copy operation succeeded, which was expected.")<br />
<br />
            Catch<br />
                txt_status.ForeColor.ToKnownColor.Red.ToString()<br />
                txt_status.Text = "An error has occurred while copying changed/created file(s)"<br />
            End Try<br />
<br />
            ProgressBar1.PerformStep() ' Increment the progress bar<br />
<br />
        Next x ' goes through next item in the listbox<br />
<br />
<br />
<br />
        ' Sets the current status of the applications (copying)<br />
        txt_status.Text = "All files have been copies Succesfully!"<br />
    End Sub<br />
<br />
    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_brs_Folder.Click<br />
        Dim folderBrowserDialog1 As New FolderBrowserDialog<br />
<br />
        'Present the user with a file open dialog box.<br />
        folderBrowserDialog1.ShowNewFolderButton = True<br />
        If folderBrowserDialog1.ShowDialog() = DialogResult.OK Then<br />
<br />
        End If<br />
        txt_watchpath.Text = folderBrowserDialog1.SelectedPath<br />
    End Sub<br />
<br />
    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load<br />
<br />
    End Sub<br />
<br />
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_brs_Copy.Click<br />
        Dim folderBrowserDialog1 As New FolderBrowserDialog<br />
<br />
        'Present the user with a file open dialog box.<br />
        folderBrowserDialog1.ShowNewFolderButton = True<br />
        If folderBrowserDialog1.ShowDialog() = DialogResult.OK Then<br />
<br />
        End If<br />
        txt_copyLocation.Text = folderBrowserDialog1.SelectedPath<br />
    End Sub<br />
<br />
    Private Sub btn_brs_Backup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_brs_Backup.Click<br />
        Dim folderBrowserDialog1 As New FolderBrowserDialog<br />
<br />
        'Present the user with a file open dialog box.<br />
        folderBrowserDialog1.ShowNewFolderButton = True<br />
        If folderBrowserDialog1.ShowDialog() = DialogResult.OK Then<br />
<br />
        End If<br />
        txt_backupLocation.Text = folderBrowserDialog1.SelectedPath<br />
    End Sub<br />
<br />
    Private Sub Button1_Click_2(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click<br />
        changed_files.Items.Clear()<br />
    End Sub<br />
<br />
    <br />
<br />
End Class<br />
<br />



Please e-mail if you have any questions or want the zip file.

erik4ever@gmail.com
AnswerRe: File System Watcher & File.Copy Method Pin
ToddHileHoffer8-Feb-06 3:22
ToddHileHoffer8-Feb-06 3:22 
GeneralRe: File System Watcher & File.Copy Method Pin
vcdflow8-Feb-06 3:43
vcdflow8-Feb-06 3:43 
GeneralRe: File System Watcher & File.Copy Method Pin
vcdflow8-Feb-06 3:56
vcdflow8-Feb-06 3:56 
QuestionHow can I hide a tabpage of tabcontrol VS 2005? Pin
JUNEYT8-Feb-06 0:25
JUNEYT8-Feb-06 0:25 
AnswerRe: How can I hide a tabpage of tabcontrol VS 2005? Pin
Dave Kreskowiak8-Feb-06 8:11
mveDave Kreskowiak8-Feb-06 8:11 
QuestionRecreate / Refresh Windows Form Pin
shiroamachi7-Feb-06 23:27
shiroamachi7-Feb-06 23:27 
AnswerRe: Recreate / Refresh Windows Form Pin
Dave Kreskowiak8-Feb-06 8:11
mveDave Kreskowiak8-Feb-06 8:11 
Questionusing dll Pin
bluey127-Feb-06 21:51
bluey127-Feb-06 21:51 
AnswerRe: using dll Pin
Dave Kreskowiak8-Feb-06 8:09
mveDave Kreskowiak8-Feb-06 8:09 
QuestionNeed help !!! Pin
welbert7-Feb-06 21:31
welbert7-Feb-06 21:31 
AnswerRe: Need help !!! Pin
Dave Kreskowiak8-Feb-06 8:09
mveDave Kreskowiak8-Feb-06 8:09 
Questionopen file using specific program Pin
cuticuteo7-Feb-06 21:00
cuticuteo7-Feb-06 21:00 
AnswerRe: open file using specific program Pin
Chatura Dilan8-Feb-06 1:57
Chatura Dilan8-Feb-06 1:57 
GeneralRe: open file using specific program Pin
cuticuteo8-Feb-06 19:16
cuticuteo8-Feb-06 19:16 
Questiondatabase conectiviyt Pin
kapil dhamija7-Feb-06 20:59
kapil dhamija7-Feb-06 20:59 
AnswerRe: database conectiviyt Pin
RichardBerry7-Feb-06 22:27
RichardBerry7-Feb-06 22:27 
Questionhow can i view a specific data from the user infomation after login process? Pin
pandapatin7-Feb-06 20:54
pandapatin7-Feb-06 20:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.