Xtreme Visual Basic Talk

Xtreme Visual Basic Talk (http://www.xtremevbtalk.com/)
-   ASP.Net (http://www.xtremevbtalk.com/asp-net/)
-   -   File Compare and Move - Same File Name with Different File Extensions (http://www.xtremevbtalk.com/asp-net/328041-file-compare-move-file-name-file-extensions.html)

sgwilliams 02-05-2016 12:17 PM

File Compare and Move - Same File Name with Different File Extensions
 
2 Attachment(s)
I'm looking for a VB script or a script of some other flavor that can accomplish the same thing. This is a script that will compare two files in the same folder which have the same file name with different extensions and move the older of the two to a destination folder.

Example filenames are:

example.dwf
example.idw.dwf

Here is the code I have so far for the form I'm working with.
Code:

Imports System.IO
Imports System.ComponentModel

Public Class Form1

    Private Const FOLDERPATH As String = "\\dell2011\engineering\Carter Prints"
    Private _bsFiles As New BindingSource()
    Private _dtFiles As New DataTable()

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        If Not Directory.Exists(FOLDERPATH) Then Throw New Exception("Folder doesn't exist")
        _dtFiles.Columns.Add("FilePath")
        _dtFiles.Columns.Add("FileName")

        RefreshFiles()

        _bsFiles.DataSource = _dtFiles
        ListBox1.DisplayMember = "FileName"
        ListBox1.ValueMember = "FilePath"
        ListBox1.DataSource = _bsFiles
    End Sub

    Private Sub RefreshFiles()
        'loop around each path and loads the files.
        '_files.AddRange(IO.Directory.GetFiles(FOLDERPATH).Where(Function(x)
        '                                                            Dim y = System.IO.Path.GetExtension(x)
        '                                                            Return (New String() {".dwf", ".pdf"}).Contains(y)
        '                                                        End Function) _
        '                                                .Select(Function(x) New FileInformation(x)))

        For Each filePath As String In IO.Directory.GetFiles(FOLDERPATH).Where(Function(x)
                                                                                  Return (New String() {".dwf", ".pdf"}).
                                                                                      Contains(System.IO.Path.GetExtension(x))
                                                                              End Function)
            _dtFiles.Rows.Add(New Object() {filePath, IO.Path.GetFileName(filePath)})
        Next
    End Sub

    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
        If String.IsNullOrWhiteSpace(TextBox1.Text) Then
            _bsFiles.RemoveFilter()
        Else
            _bsFiles.Filter = String.Format("FileName LIKE '%{0}%'", TextBox1.Text)
        End If
    End Sub

    Private Sub ListBox1_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles ListBox1.MouseDoubleClick
        Dim theFile = ListBox1.SelectedValue
        If theFile IsNot Nothing Then
            Process.Start(CStr(theFile))
        End If
    End Sub
    Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
            SendKeys.Send("{TAB}")
            e.Handled = True
        End If
    End Sub

    Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click

    End Sub

    Private Sub Label2_Click(sender As Object, e As EventArgs) Handles Label2.Click

    End Sub
    Private Sub ListBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ListBox1.KeyDown
        Dim theFile = ListBox1.SelectedValue
        If e.KeyCode = Keys.Enter Then
            Process.Start(CStr(theFile))
        End If
    End Sub
    Private WithEvents Tmr As New Timer

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Tmr.Dispose()
    End Sub

    Private Sub CloseWindowToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CloseWindowToolStripMenuItem.Click
        Close()
    End Sub
End Class

If you will notice the files have different extensions some have just a .dwf & others have a double extension that is .idw.dwf. I want to remove both types from the file name so just the part number is left visible in listbox1. There are a couple issues when trying to accomplish this.

Issue one: If I remove both extensions from the current list there will be duplicate values left. The attached image "idw dwf extn.JPG" shows file "C-674-A.dwf" & "C-674-A.idw.dwf". When you remove the extensions from both file names the part number would be the same. One is an old revision of the print, The other is the most recent. This is a problem I need to fix by running a vb script to remove the file with the oldest date when it see's duplicate part numbers. Once the system is cleaned from the duplicate numbers then I can tackle issue 2.

Issue two: When the user does a search for the part number they will be confused by the file extension because it is alien to them as they do not know what it is, nor should they need too. When I changed the IO.Path.GetFileName to IO.Path.GetFileNameWithoutExtension it only removed the last four digits from the file name. This means it removed the ".dwf" from the double file name extension of ".idw.dwf" leaving just the extension ".idw".

See attached image " stripped exten1.PNG" for what the current form looks like running.

I appreciate any help you can give me on this.


All times are GMT -6. The time now is 08:22 PM.

Powered by vBulletin® Version 3.8.9
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Optimisation provided by DragonByte SEO v2.0.15 (Lite) - vBulletin Mods & Addons Copyright © 2017 DragonByte Technologies Ltd.
All site content is protected by the Digital Millenium Act of 1998. Copyright©2001-2011 MAS Media Inc. and Extreme Visual Basic Forum. All rights reserved.
You may not copy or reproduce any portion of this site without written consent.