locked
Can't get a file in a folder to move RRS feed

  • Question

  • Hello,

    (visual basic 2008 issue)

    I am trying to move a folders(testfolder) files that is on the desktop(windows) to C:\Program Files\realfolder. I have no problem finding the folder it is to be placed into. But i cannot seem to find the desktop directory.

    I named the folder on the desktop testfolder. I cannot seem to find the correct directory for desktop anywhere.

    I tried a fair few and keep getting this result when i press the button: Could not find directory '%userprofile%\Desktop\test\'.

    Heres the code i am using:

    [code]My.Computer.FileSystem.MoveDirectory("%userprofile%\Desktop\testfolder\", _
                "C:\PROGRA~1\realfolder")[/code]

    Can someone please explain to me what i am doing wrong?

    Thanks -Bob.

    Thursday, February 9, 2012 5:06 AM

Answers

  • Something like this:

    For Each FileStr As String In Directory.GetFiles(TestFolder)
                If Not File.Exists(RealFolder & "\" & FileStr.Substring(FileStr.LastIndexOf("\") + 1)) Then
                    File.Move(FileStr, RealFolder & "\" & FileStr.Substring(FileStr.LastIndexOf("\") + 1))
                Else
                    If MessageBox.Show("Do you want to overwrite this file?""File already exists", _
                                       MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) = DialogResult.Yes Then
                        File.Delete(RealFolder & "\" & FileStr.Substring(FileStr.LastIndexOf("\") + 1))
                        File.Move(FileStr, RealFolder & "\" & FileStr.Substring(FileStr.LastIndexOf("\") + 1))
                    End If
                End If
            Next

    And if you wanted to make things simple.

    For Each FileStr As String In Directory.GetFiles(TestFolder)
     
                Dim MoveLocation As String = RealFolder & "\" & FileStr.Substring(FileStr.LastIndexOf("\") + 1)
     
                If Not File.Exists(MoveLocation) Then
                    File.Move(FileStr, MoveLocation)
                Else
                    If MessageBox.Show("Do you want to overwrite this file?""File already exists", _
                                       MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) = DialogResult.Yes Then
                        File.Delete(MoveLocation)
                        File.Move(FileStr, MoveLocation)
                    End If
                End If
     
            Next



    If a post helps you in any way or solves your particular issue, please remember to use the Propose As Answer option or Vote As Helpful

    ~ "The universe is an intelligence test." - Timothy Leary ~

    • Edited by Troy Garner Sunday, February 12, 2012 1:48 AM
    • Marked as answer by Kee Poppy Thursday, February 16, 2012 3:07 AM
    Sunday, February 12, 2012 1:44 AM

All replies

  • Hi

    Here are some examples to illustrate things for you:

            ' set up DeskTop path
            Dim test As String = My.Computer.FileSystem.SpecialDirectories.Desktop
    
            'example only - not used here
            Dim test2 As String = My.Computer.FileSystem.SpecialDirectories.ProgramFiles
    
            ' find list of directories in test path (DeskTop)
            Dim dirs As ObjectModel.ReadOnlyCollection(Of String) = My.Computer.FileSystem.GetDirectories(test)
    
            ' find files in first directory in list - (need to check for zero folders)
            Dim files As ObjectModel.ReadOnlyCollection(Of String) = My.Computer.FileSystem.GetFiles(dirs(0))
    
            ' find file info for first file in list - (need to check for zero files)
            Dim onefile As IO.FileInfo = My.Computer.FileSystem.GetFileInfo(files(0))
    Note: depending on your version of Windows, you may not find it possible to write to the Program Files directory (due to Widows protection/permissions)- always best to use a destination in User file areas (such as My Documents etc) - these can also be found in SpecialDirectories.

    Regards Les, Livingston, Scotland


    • Edited by leshay Thursday, February 9, 2012 6:15 AM
    Thursday, February 9, 2012 6:11 AM
  • Try this:

    Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\testfolder"
    

    That's the proper directory for "testfolder" on your desktop. Although what you're doing wrong here is trying to parse %UserProfile% out as a string, so it's not looking for C:\Users\UserAccountName, it's actually looking for "%UserProfile%" as a string value. VB.NET does not parse that out like you would use it in a language like Batch.

    System.IO.Directory.Move(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\testfolder", _
                                     Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86) & "\realfolder")

    This moves directory located for instance at C:\Users\UserName\Desktop\testfolder to the Program Files folder and inside a directory called "realfolder" if everything needed for this move exists.


    If a post helps you in any way or solves your particular issue, please remember to use the Propose As Answer option or Vote As Helpful
    ~ "The universe is an intelligence test." - Timothy Leary ~

    • Proposed as answer by horngsh Saturday, February 11, 2012 3:04 PM
    Thursday, February 9, 2012 8:37 AM
  • In addition to the others here the official place where the use of the special folders is described. 

    However, you can like Leshay wrote also use the My class. 

    http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx


    Success
    Cor

    • Proposed as answer by Sarathi R Thursday, February 9, 2012 8:57 AM
    Thursday, February 9, 2012 8:55 AM
  • If you really wanted to use %UserProfile% though it can be done like this:

    Environment.ExpandEnvironmentVariables("%UserProfile%\Desktop\testfolder")
    

    I don't really see a need for it though as there's a SpecialFolder in Environment that will get the directory directly instead of doing %UserProfile%\Desktop\Folder


    If a post helps you in any way or solves your particular issue, please remember to use the Propose As Answer option or Vote As Helpful
    ~ "The universe is an intelligence test." - Timothy Leary ~


    • Edited by Troy Garner Thursday, February 9, 2012 9:03 AM
    Thursday, February 9, 2012 9:02 AM
  • Try this:

    Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\testfolder"
    

    That's the proper directory for "testfolder" on your desktop. Although what you're doing wrong here is trying to parse %UserProfile% out as a string, so it's not looking for C:\Users\UserAccountName, it's actually looking for "%UserProfile%" as a string value. VB.NET does not parse that out like you would use it in a language like Batch.

    System.IO.Directory.Move(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\testfolder", _
                                     Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86) & "\realfolder")

    This moves directory located for instance at C:\Users\UserName\Desktop\testfolder to the Program Files folder and inside a directory called "realfolder" if everything needed for this move exists.


    If a post helps you in any way or solves your particular issue, please remember to use the Propose As Answer option or Vote As Helpful
    ~ "The universe is an intelligence test." - Timothy Leary ~

    Thankyou Everyone for youre reply's. Ace, This is exactly what i was looking for thankyou. The only issue i get now is when i test this process it says this as an error: "Cannot create a file which already exists"

    The folder "realfolder" exists but the contents inside "testfolder" do not exist in "realfolder" which has confused me:/

    And where you hade ProgramFilesX86; My VB only lets me use ProgramFiles. Is this going to be an issue? If i put ProgramFiles folder on this computer and then used this on my 64-bit OS laptop would this still work?

    Again thankyou everyone. You are all soo good!.

    Thursday, February 9, 2012 11:00 AM
  • No, program files will do just fine :)

    You could try this:

    If System.IO.Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86) & "\realfolder"Then _
                Directory.Delete(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86) & "\realfolder")

    Added before the actual Move()

    Here's from the MSDN documentation for Directory.Move()

    This method throws an IOException if, for example, you try to move c:\mydir to c:\public, and c:\public already exists. You must specify "c:\\public\\mydir" as the destDirName parameter, provided that "mydir" does not exist under "c:\\public", or specify a new directory name such as "c:\\newdir".

    Or you could do something else when IOException is raised as an exception. Use the Try Catch with the IOException to capture that event. Maybe you want to prompt the user for permission to replace the contents of that directory and use my code above? Remember though, you're moving a directory to a new location, and in that event, you're also given opportunity to change it's name. If realfolder already exists, it would be the same as dragging a folder to a new location manually where it's folder name already existed in that directory.

    It's not going to work right away.

    Read more about Directory.Move method from MSDN official documentation here: http://msdn.microsoft.com/en-us/library/system.io.directory.move.aspx

    TIP: Perhaps you don't want to keep typing out the full Environment.GetFolderPath() string, so instead of what i've done above you could publicize the string value for that filepath in a variable usable throughout the whole class by doing:

    Private TestFolder As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\testfolder"
        Private RealFolder As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\realfolder"

    Now you can use them like so:

    System.IO.Directory.Move(TestFolder, RealFolder)
    

    Or you can just Dim them in the scope of that subroutine or function even if you'll only use it once. Another thing, I usually Import my system namespaces at the top, but I provide it in full when I help people out usually.

    Cheers! :)
    ~Ace


    If a post helps you in any way or solves your particular issue, please remember to use the Propose As Answer option or Vote As Helpful
    ~ "The universe is an intelligence test." - Timothy Leary ~


    • Edited by Troy Garner Thursday, February 9, 2012 2:04 PM
    Thursday, February 9, 2012 1:53 PM
  • Is their a way i can move the contents inside testfolder to the folder realfolder which already exists?

    Basically i would like to move about 5 files in testfolder to realfolder and realfolder will have these contents already and id like it to basically move and replace the files. The files that are in testfolder are the updated version files. I don't mind if it asks the USER if they wish for it to overwrite the old files i have nothing to hide:).

    I would like to be able to do this. And if for instance parental controls are on it will still try move the files and USER becomes prompted to allow etc.

    Using this formula you posted:

           

    Private TestFolder AsString = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\testfolder"

    Private RealFolder AsString = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) & "\realfolder"

    System.IO.Directory.Move(TestFolder, RealFolder)

    This works. But when i test this in VB it says this: "Cannot create a file when that file already exists."

    The files inside test DO NOT even exist in folder realfolder :/

    Thanks again and i'm very sorry that i keep being an annoying poster but i am reasonably new to Vb and using the sources on this site still doesnt seem to help with the issue i am having.


    Friday, February 10, 2012 4:49 AM
  • Read my latest post in full, I mentioned as to why that was happening, but it's because you're trying to move a folder to a directory location where that foldername already exists, so if you just wanted to move the files inside the folder over there, then you wouldn't want to move the full directory. I would do this:

    Private TestFolder As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\testfolder"
        Private RealFolder As String = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) & "\realfolder"


            For Each FileStr As String In Directory.GetFiles(TestFolder)
                If Not File.Exists(RealFolder & "\" & FileStr.Substring(FileStr.LastIndexOf("\") + 1)) Then _
                File.Move(FileStr, RealFolder & "\" & FileStr.Substring(FileStr.LastIndexOf("\") + 1))
            Next

    If the file in the new location doesn't already exist, it will move the file from testfolder into the realfolder location, otherwise it will not move it over, if you want to overwrite, then you could instead delete the folder, and then move the file over into the new location, or come out with a prompt to ask the user whether they want to move and overwrite that existing file or not.

    Cheers! :)
    ~Ace


    If a post helps you in any way or solves your particular issue, please remember to use the Propose As Answer option or Vote As Helpful
    ~ "The universe is an intelligence test." - Timothy Leary ~


    • Edited by Troy Garner Friday, February 10, 2012 11:30 AM
    Friday, February 10, 2012 11:23 AM
  • Wooops. Will read more carefully next time...

    So if i wanted to prompt the user to move and replace, will the formula above still work? What do you do make a msgbox or something?

    Saturday, February 11, 2012 7:09 AM
  • Something like this:

    For Each FileStr As String In Directory.GetFiles(TestFolder)
                If Not File.Exists(RealFolder & "\" & FileStr.Substring(FileStr.LastIndexOf("\") + 1)) Then
                    File.Move(FileStr, RealFolder & "\" & FileStr.Substring(FileStr.LastIndexOf("\") + 1))
                Else
                    If MessageBox.Show("Do you want to overwrite this file?""File already exists", _
                                       MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) = DialogResult.Yes Then
                        File.Delete(RealFolder & "\" & FileStr.Substring(FileStr.LastIndexOf("\") + 1))
                        File.Move(FileStr, RealFolder & "\" & FileStr.Substring(FileStr.LastIndexOf("\") + 1))
                    End If
                End If
            Next

    And if you wanted to make things simple.

    For Each FileStr As String In Directory.GetFiles(TestFolder)
     
                Dim MoveLocation As String = RealFolder & "\" & FileStr.Substring(FileStr.LastIndexOf("\") + 1)
     
                If Not File.Exists(MoveLocation) Then
                    File.Move(FileStr, MoveLocation)
                Else
                    If MessageBox.Show("Do you want to overwrite this file?""File already exists", _
                                       MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) = DialogResult.Yes Then
                        File.Delete(MoveLocation)
                        File.Move(FileStr, MoveLocation)
                    End If
                End If
     
            Next



    If a post helps you in any way or solves your particular issue, please remember to use the Propose As Answer option or Vote As Helpful

    ~ "The universe is an intelligence test." - Timothy Leary ~

    • Edited by Troy Garner Sunday, February 12, 2012 1:48 AM
    • Marked as answer by Kee Poppy Thursday, February 16, 2012 3:07 AM
    Sunday, February 12, 2012 1:44 AM
  • Beautiful!

    Thanks Ace. I have no issue if i was doing this as a batch lol. But batch is not allowed to be transferred or shared on the net. So i have decided to use Visual Basic 2008. I really do appreciate your help Thankyou.

    -Bob.

    Sunday, February 12, 2012 9:08 AM
  • Remember to mark answers in all the threads you post here so that new people visiting this entry know that it's already been solved. Otherwise people may think that you're still requiring help on the issue :)

    Glad you got things all together though!

    Cheers
    ~Ace


    If a post helps you in any way or solves your particular issue, please remember to use the Propose As Answer option or Vote As Helpful
    ~ "The universe is an intelligence test." - Timothy Leary ~

    Sunday, February 12, 2012 2:05 PM