Ugh....Driving Me nuts....

I am in the middle of writing a prog that takes a file within a directory on a HD renames it to the directories name and then places the newly named file on the root of that HD. Here is the code. and I am not getting it.....I would really appreciate your help.

Thanks

Imports System

Imports System.io

Imports System.IO.DirectoryInfo

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim dlg As New FolderBrowserDialog

Dim FileNamesSelected() As String

Dim SelectedFolder As String

'Dim FileFound As String

SelectedFolder = Me.FolderBrowserDialog1.SelectedPath

FileNamesSelected = System.IO.Directory.GetFiles(SelectedFolder)

For Each FileNameSelected As String In FileNamesSelected

Next

'FileFound = fiFilesFound

If dlg.ShowDialog = Windows.Forms.DialogResult.OK Then

MoveFilesToRoot(dlg.SelectedPath, FileNamesSelected)

End If

End Sub

Private Sub MoveFilesToRoot(ByVal strFolder As String, ByVal strFilesToMove As String)

'Dim diDirsFound() As IO.DirectoryInfo

Dim diDir As IO.DirectoryInfo

Dim diSubDirs As IO.DirectoryInfo

'Dim fiFilesFound() As IO.FileInfo

Dim fiFile As IO.FileInfo

diDir = New IO.DirectoryInfo(strFolder)

' Check we are not in the root folder

'If strFolder.ToUpper <> strRoot.ToUpper Then

' Find all files which match the pattern

For Each fiFile In diDir.GetFiles()

' Check if the file matches the files to move

If fiFile.Name.ToUpper = strFilesToMove.ToUpper Then

' Copy the files to the root folder, renaming them to have the folder's name

fiFile.CopyTo("\" + diDir.Name + fiFile.Extension)

End If

' Delete the file - directories have to be empty to be removed

fiFile.Delete()

Next

'End If

' Loop through each subdirectory directory

For Each diSubDirs In diDir.GetDirectories

' Move the files in any subfolders, and their subfolders

MoveFilesToRoot(diSubDirs.FullName, strFilesToMove)

' Delete the Directory

diSubDirs.Delete()

Next

End Sub

End Class



Answer this question

Ugh....Driving Me nuts....

  • donkaiser

    Here's some code to play with. Try it out a little bit

    before trying to 're-shape' it.

    ' We will add one class to the project 'FileSearch.vb'

    ' See below dotted line

    Imports System.io

    Public Class Form1

    ' Change the next line as follows:

    ' Change 'FindFileMove' to your Appliction project

    ' name. Usually the top name shown in the solution

    ' explorer.

    Dim searcher As FindFileMove.FileSearch

    Private filesFound As Long = 0

    Private Delegate Sub FormChanger(ByVal fileName As String)

    Public Sub FileFound(ByVal FileName As String)

    filesFound += 1

    Dim manipulate As New FormChanger(AddressOf ManipulateForm)

    Dim args(0) As Object

    args(0) = FileName

    ' Invoke sends it to the other thread

    Me.Invoke(manipulate, args)

    End Sub

    Public Sub ManipulateForm(ByVal fileName As String)

    Dim rootdrive As String = Path.GetPathRoot(fileName)

    Dim OrigDir As String = Path.GetDirectoryName(fileName)

    Dim SrcString As String = Replace(Mid(OrigDir, 3, OrigDir.Length - 3), "\", "_")

    Dim JustName As String = Path.GetFileName(fileName)

    ' Deletions not needed, we moved the file.

    File.Move(fileName, rootdrive & SrcString & "_" & JustName)

    End Sub

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

    searcher = New FindFileMove.FileSearch()

    Dim dlg As New FolderBrowserDialog

    If dlg.ShowDialog = Windows.Forms.DialogResult.OK Then

    With searcher

    .Callback = New FindFileMove.FileSearch.FileFound(AddressOf Me.FileFound)

    .Filter = "*.vob"

    .Recursive = True ' Means to check subdirectories

    .StartDirectory = dlg.SelectedPath

    'If true then chkAsync.Checked Then

    Dim myThread As New Threading.Thread(AddressOf .StartSearch)

    myThread.Name = "Background Thread"

    myThread.Start()

    'Else

    '.StartSearch()

    'End If

    End With

    End If

    End Sub

    End Class

    ---------------------------------------------------------------------------------------------------------

    ' Add class 'FileSearch.vb' to the project

    Imports System

    Imports System.IO

    Public Class FileSearch

    Private m_callback As FileFound

    Private m_filter As String

    Private m_startingDirectory As String

    Private m_recursive As Boolean

    Public Property Filter() As String

    Get

    Return m_filter

    End Get

    Set(ByVal Value As String)

    m_filter = Value

    End Set

    End Property

    Public Property Recursive() As Boolean

    Get

    Return m_recursive

    End Get

    Set(ByVal Value As Boolean)

    m_recursive = Value

    End Set

    End Property

    Public Property StartDirectory() As String

    Get

    Return m_startingDirectory

    End Get

    Set(ByVal Value As String)

    m_startingDirectory = Value

    End Set

    End Property

    Public Property Callback() As FileFound

    Get

    Return m_callback

    End Get

    Set(ByVal Value As FileFound)

    m_callback = Value

    End Set

    End Property

    Public Delegate Sub FileFound(ByVal fileName As String)

    Public Sub StartSearch()

    Search(m_filter, m_startingDirectory, m_recursive)

    End Sub

    Private Sub Search(ByVal fileFilter As String, _

    ByVal startDirectory As String, _

    ByVal recursiveSearch As Boolean)

    Dim filesFound() As String

    Dim directoriesFound() As String

    Dim currentDirectory As String

    Dim currentFile As String

    Dim state As New Object()

    Try

    filesFound = Directory.GetFiles(startDirectory, fileFilter)

    For Each currentFile In filesFound

    m_callback.Invoke(currentFile)

    Next

    Catch e As Exception

    'ignore error reading files, likely a security issue

    End Try

    Try

    If recursiveSearch Then

    directoriesFound = Directory.GetDirectories(startDirectory)

    For Each currentDirectory In directoriesFound

    Search(fileFilter, currentDirectory, recursiveSearch)

    Next

    End If

    Catch e As Exception

    'ignore error reading directories, as above

    End Try

    End Sub

    End Class



  • HCTwinJava

    I have tryed your code and it seems to work fine.

    It does add and extra '\" to the 'h:\' destination which does

    not seem to hurt anything.

    You seem to have checked all the obvious stuff, such as

    the H:\ root being empty to start with, so, if we are to

    believe the error message we would have to have duplicate

    directory names for this to happen. Or ...

    Somehow the code is functioning at the root level, and unless

    some variable is not updating in a timely manner in 'real time'.

    I don't see how that could happen either.

    I can only suggest to add a listbox and add all the variables involved,

    before and after the 'move file' and publish that and compare what

    is in the root vs what the listbox shows (and where the listbox stops).

    You may have to add a temporary 'try/catch' to make the published

    listbox readable after the error (exiting the routine on error.)

    Otherwise, Spotty, ReneeC, DMan, Others



  • Jamie Thomson

    The code is below.

    The Program is Hardcoded to use .VOB

    i have been creating false .VOBs by changing an empty Text file and changing the extension to .VOB.

    Renee, do you have 1 directory to many or do you have another drive other than where your OS is at

    I ask because i do not want you to erase anything by accident.

    There is one piece of info that may make a difference, but I am not really sure. The normal VOBs range in size from 1.5 gigs to 8gigs.

    My question then is this: Would erasing a file with 5gigs take longer than 3gigs and shorter than 8gigs If so, could the program not be deleting things fast enough before it continues If so, should I place the program into a thread sleep for a few moments (I read about that in another forum somewhere) If so, I would need some help implementing this.

    Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim dlg As New FolderBrowserDialog

    If dlg.ShowDialog = Windows.Forms.DialogResult.OK Then

    MoveFilesToRoot(New IO.DirectoryInfo(dlg.SelectedPath), dlg.SelectedPath, ".VOB")

    End If

    End Sub

    Private Sub MoveFilesToRoot(ByVal diDir As IO.DirectoryInfo, ByVal strRoot As String, ByVal strExtensionToMove As String)

    Dim diSubDir As IO.DirectoryInfo

    Dim fiFile As IO.FileInfo

    ' Check we are not in the root folder

    ' Added use of Geu full Path so function is tollerent of drives specified as "H:"

    If diDir.FullName.ToUpper <> IO.Path.GetFullPath(strRoot).ToUpper AndAlso diDir.FullName.ToLower <> IO.Path.GetFullPath(strRoot).ToLower Then

    ' Find all files which match the pattern

    For Each fiFile In diDir.GetFiles()

    ' Check if the file matches the files to move

    If fiFile.Extension.ToUpper = strExtensionToMove.ToUpper AndAlso fiFile.Extension.ToLower = strExtensionToMove.ToLower Then

    ' Copy the files to the root folder, renaming them to have the folder's name

    fiFile.MoveTo(strRoot + "\" + diDir.Name + strExtensionToMove)

    End If

    ' Delete the file - driectories have to be empty to be removed

    'fiFile.Delete()

    Next

    End If

    'Loop through each subdirectory directory

    For Each diSubDir In diDir.GetDirectories

    ' Move the files in any subfolders, and their subfolders

    MoveFilesToRoot(diSubDir, strRoot, strExtensionToMove)

    ' Delete the Directory

    diSubDir.Delete()

    Next

    End Sub

    End Class


  • Harald K&amp;#246;stinger

    Renee,

    The how and where is it failing is really hard to pinpoint. What is it doing to cause it to fail is that its not taking the file in the selected folder (directory) and moving it at all. Its deleting everything in the folder (directory) and not deleting the folder (directory). That is the how it is failing. The where I believe is scattered through out the code, I am sorry that I cant be more clear on where than that (I have Watched it and Stepped through it and its doing I suppose what its supposed to do, but the end results are not what it should have been or be)

    Yes, you are understanding me correctly.

    As for multiple subdirectories....this is being used in a very specific location for a very specific purpose by me so I am sure that it will be used in the appropriate ways.


  • Stephen Inglish

    I didn't question your logic of why you wanted to rename the

    file with the directory name it came from, so, my example code

    renames the files sort of like:

    _Myfolder_Subfoldername_OriginalFileName.vob

    (Replacing the "\" in the path with a "_")

    What you may be running into in your code is trying to

    move a file to an existing file name. (I have not used you code.)

    But, if you had

    H:\aaa\bbb\test.vob and

    H:\aaa\bbb\ccc\test.vob

    and did not correctly rename them, then you would be trying to

    overwrite an existing file with a move. (Which is probably where

    your error is coming from.)

    Also, the filename you are moving to the root may just exist anyway.

    So, you need to do some testing for that senario and figure out

    how you would handle the error with a 'try/catch'.



  • GAtkins

    Okay here is the scenario:

    We have a program that we use to extract files from a DVD (we have contracts with the Studios involved so everything I am speaking is completely legal). This software extracts the main movie into a folder that we assign:

    h:\00001@Studio@Film

    It then extracts a VOB into that folder which it names VTS_XX_1.VOB (XX is a number between 01 and 100)

    h:\0001@Studio@Film\VTS_01_1.VOB

    I have written the program to go into the folders move the file to the root (h:) and rename it to the Folder it existed in, then to erase the folder.

    There will never, never be a scenario where there will be

    h:\00001@Studio@Film\Directory\Directory\VTS_01_1.VOB

    I am not of the mindset to believe that the program looks at the filename and says this is the same file name as the last filename in the other folder and then throughs an exception. I know this because in the test folders i was working on during debug every file in each of the different folder was named: Test1.VOB

    So I am still wondering why its coming across this error.


  • mistry_bhavin

    Okay ...

    So I went back through it again, looking up references as I needed....and came up with this. While incrediblly close to what I need, its not. It does go in to H:\Test Directory\ and gets all the files in the directories and dumps them from the sub-directories of Test Directory into Test Directory I need it to look only at the root directory (H:\ in this example) and get all the .vob from the subdirectories in the root directory and dump them on the root driectory after renaming them. Also I am having difficulty getting a Msg Box to pop when the prog is done....

    Any Ideas

    Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim dlg As New FolderBrowserDialog

    If dlg.ShowDialog = Windows.Forms.DialogResult.OK Then

    MoveFilesToRoot(New IO.DirectoryInfo(dlg.SelectedPath), dlg.SelectedPath, ".VOB")

    End If

    End Sub

    Private Sub MoveFilesToRoot(ByVal diDir As IO.DirectoryInfo, ByVal strRoot As String, ByVal strExtensionToMove As String)

    Dim diSubDir As IO.DirectoryInfo

    Dim fiFile As IO.FileInfo

    ' Check we are not in the root folder

    ' Added use of Geu full Path so function is tollerent of drives specified as "H:"

    If diDir.FullName.ToUpper <> IO.Path.GetFullPath(strRoot).ToUpper Then

    ' Find all files which match the pattern

    For Each fiFile In diDir.GetFiles()

    ' Check if the file matches the files to move

    If fiFile.Extension.ToUpper = strExtensionToMove.ToUpper Then

    ' Copy the files to the root folder, renaming them to have the folder's name

    fiFile.CopyTo(strRoot + "\" + diDir.Name + strExtensionToMove)

    End If

    ' Delete the file - driectories have to be empty to be removed

    fiFile.Delete()

    Next

    End If

    ' Loop through each subdirectory directory

    For Each diSubDir In diDir.GetDirectories

    ' Move the files in any subfolders, and their subfolders

    MoveFilesToRoot(diSubDir, strRoot, strExtensionToMove)

    ' Delete the Directory

    diSubDir.Delete()

    Next

    End Sub


  • Tijnars

    GOD I am so stupid.....

    Because I am still new in this....I had not reliazed that when I was putting it together. That it doesnt matter where you are, whether you are in H:\ or in H:\SomeDirectory\....it will take all the subdirectories within the directory that you choose....

    I HATE BEING NEW!!!!!!


  • Shawnk

    Quote: "TallDude - You seem to have checked all the obvious stuff, such as the H:\ root being empty to start with, so, if we are to believe the error message we would have to have duplicate directory names for this to happen."

    I want to make it as clear as possible: the H:\ root is not empty. It has subdirectories in it. A directory-tree may look lke the following -

    H:\
    a00001@Studio_Name@Film_Name
    VTS_08_1.VOB
    a00002@Studio_Name@Film_Name
    VTS_19_1.VOB
    a00003@Studio_Name@Film_Name
    VTS_01_1.VOB

    And this may continue for 46 other folders (directories).

    Like I have said, when I test this in debug mode and have a folder in my c:\ to act as representation of H:\ with subsequent folders in it with files this works without a problem.

    Example of my testing directory:

    c:\Test_Directory
    c:\Test_Directory\ a00001@Studio_Name@Film_Name\VTS_08_1.VOB
    c:\Test_Directory\ a00002@Studio_Name@Film_Name\VTS_19_1.VOB
    c:\Test_Directory\ a00003@Studio_Name@Film_Name\VTS_01_1.VOB

    Does that make sense



  • yosonu

    Have not looked closely at all the code, but ...

    One main loop does not have the action statements

    inside the loop.

    For Each FileNameSelected As String In FileNamesSelected

    Next



  • Docpro777

     

    Do you have the latest copy of your code

     

    Also... A question

     

    I'm using .sln instead of.vob. If I have a directory structure like:

    H:\a\test

    Where there is a .sln file in test.... it is desired to show up in H:\  ( ) with what exact file name

     

     



  • Hammad_Awan

    Two things:

    1.) How and where, exactly is it failing.

    2.) Let me see if you conveyed accurately what you communicated:

    If you have a in the path and name of:

    C:\temp\foo.dat

    After you've processed it, you want its path and name to be:

    C:/temp.dat

    So your program is constantly renaming and dumping file to the hard drive root This mean you'll be able to do this operation to one file per directory

    What do you propose to name a file in c:/Hooray/Its/Tuesday/text.dat



  • NoEgo

    Well....Before I look at yours...I wen through and spotted the issues I was having (yeah)....Specifically, I can use it to look the root, access the sub-directories and then have it dump them onto the root folder with the proper rename of the folder that they exist in.

    This works while I debug it.....

    However, I then published it, like I have with others, placed it on a test computer with 6 folders on the H: with six .VOB files. I run the program and it gets through the first three folders, renames them to the folder name, moves them (without deleting them) and then deletes the folders. Then it pops up with an exception.

    Help would be appreciated.

    =========================================Exception============================================

    ************** Exception Text **************
    System.IO.IOException: Cannot create a file when that file already exists.

    at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
    at System.IO.__Error.WinIOError()
    at System.IO.FileInfo.MoveTo(String destFileName)
    at FolderRenamer2.Form1.MoveFilesToRoot(DirectoryInfo diDir, String strRoot, String strExtensionToMove)
    at FolderRenamer2.Form1.MoveFilesToRoot(DirectoryInfo diDir, String strRoot, String strExtensionToMove)
    at FolderRenamer2.Form1.MoveFilesToRoot(DirectoryInfo diDir, String strRoot, String strExtensionToMove)
    at FolderRenamer2.Form1.Button1_Click(Object sender, EventArgs e)
    at System.Windows.Forms.Control.OnClick(EventArgs e)
    at System.Windows.Forms.Button.OnClick(EventArgs e)
    at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
    at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
    at System.Windows.Forms.Control.WndProc(Message& m)
    at System.Windows.Forms.ButtonBase.WndProc(Message& m)
    at System.Windows.Forms.Button.WndProc(Message& m)
    at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
    at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
    at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


    ************** Loaded Assemblies **************
    mscorlib
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.42 (RTM.050727-4200)
    CodeBase: file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
    ----------------------------------------
    FolderRenamer2
    Assembly Version: 1.0.0.0
    Win32 Version: 1.0.0.0
    CodeBase: file:///C:/Documents%20and%20Settings/admin/Local%20Settings/Apps/2.0/PPY83XGN.933/3OTOKM63.EOH/fold..tion_869885fb5fcc0e56_0001.0000_41914f53b69ae178/FolderRenamer2.exe
    ----------------------------------------
    Microsoft.VisualBasic
    Assembly Version: 8.0.0.0
    Win32 Version: 8.0.50727.42 (RTM.050727-4200)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/Microsoft.VisualBasic/8.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualBasic.dll
    ----------------------------------------
    System
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.42 (RTM.050727-4200)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
    ----------------------------------------
    System.Windows.Forms
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.42 (RTM.050727-4200)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
    ----------------------------------------
    System.Drawing
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.42 (RTM.050727-4200)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
    ----------------------------------------
    System.Runtime.Remoting
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.42 (RTM.050727-4200)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Runtime.Remoting/2.0.0.0__b77a5c561934e089/System.Runtime.Remoting.dll
    ----------------------------------------

    ===========================================Code==============================================


    Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim dlg As New FolderBrowserDialog

    If dlg.ShowDialog = Windows.Forms.DialogResult.OK Then

    MoveFilesToRoot(New IO.DirectoryInfo(dlg.SelectedPath), dlg.SelectedPath, ".VOB")

    End If

    End Sub

    Private Sub MoveFilesToRoot(ByVal diDir As IO.DirectoryInfo, ByVal strRoot As String, ByVal strExtensionToMove As String)

    Dim diSubDir As IO.DirectoryInfo

    Dim fiFile As IO.FileInfo

    ' Check we are not in the root folder

    ' Added use of Geu full Path so function is tollerent of drives specified as "H:"

    If diDir.FullName.ToUpper <> IO.Path.GetFullPath(strRoot).ToUpper AndAlso diDir.FullName.ToLower <> IO.Path.GetFullPath(strRoot).ToLower Then

    ' Find all files which match the pattern

    For Each fiFile In diDir.GetFiles()

    ' Check if the file matches the files to move

    If fiFile.Extension.ToUpper = strExtensionToMove.ToUpper AndAlso fiFile.Extension.ToLower = strExtensionToMove.ToLower Then

    ' Copy the files to the root folder, renaming them to have the folder's name

    fiFile.MoveTo(strRoot + "\" + diDir.Name + strExtensionToMove)

    End If

    ' Delete the file - driectories have to be empty to be removed

    'fiFile.Delete()

    Next

    End If

    'Loop through each subdirectory directory

    For Each diSubDir In diDir.GetDirectories

    ' Move the files in any subfolders, and their subfolders

    MoveFilesToRoot(diSubDir, strRoot, strExtensionToMove)

    ' Delete the Directory

    diSubDir.Delete()

    Next

    End Sub

    End Class


  • Glint

    I'll look at if as long as this old motherboard can hold together, Tall dude.

    What compliment from an engineer like you.



  • Ugh....Driving Me nuts....