Andromo - Start Making Apps - Free Signup

Announcement

Collapse

New Forum Software

If you're here, you've found the new home for our forums. There will be some bugs to iron out, so thanks for your patience...
See more
See less

Function to Copy a Folder, Sub-folders and files

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Function to Copy a Folder, Sub-folders and files

    Here is the code...

    Sample Call: Copies the content of D:\ to C:\Program Files\My App
    Code:
    if CopyFolder("D:\\", "C:\\Program Files\\My App") == 0 then
    	result = Dialog.Message("Folder Copy", "The folder has been successfully copied!", MB_OK, MB_ICONINFORMATION, MB_DEFBUTTON1);
    else
    	result = Dialog.Message("Folder Copy", "There was an error copying the folder.", MB_OK, MB_ICONEXCLAMATION, MB_DEFBUTTON1);
    end
    Global Function
    Code:
    function CopyFolder(sSource, sDestination)
    	local m_sFolder;
    	local m_FoundFolder;
    	local error;
    	
    	--Find all the folders and sub-Folders
    	--results are stored in the table m_tblFolders
    	m_tblFolders = Folder.Find(sSource, "*", true, nil);
    
    	--if our destination folder does not have the 
    	--backslash as the last character, add it
    	if String.Right(sDestination, 1) ~= "\\" then
    		sDestination = sDestination .. "\\";
    	end
    	
    	--m_tblFolders will be nil if there are no folders 
    	if m_tblFolders ~= nil then
    		--enumerate through the found folders
    		for n, m_FoundFolder in m_tblFolders do
    			--replace the source's path with the destination's path
    			m_sFolder = String.Replace(m_FoundFolder, sSource, sDestination, false);
    			--create the folder
    			Folder.Create(m_sFolder);
    			error = Application.GetLastError();
    			if (error ~= 0) then
        			--set n to value to exit FOR loop
        			n = Table.Count(m_tblFolders)
    			end
    		end
    	end
    
    	--if no errors occurred, copy the files
    	if (error == 0) then
    		--show the status dialog
    		StatusDlg.Show(MB_ICONNONE, false);
    		--copy all files from the source folder, with recurse.
    		File.Copy(sSource .. "\\*.*", sDestination, true, true, false, true, nil);
    		--hide the status dialog
    		StatusDlg.Hide();
    		error = Application.GetLastError();
    	end
    
    	--clean up
    	m_tblFolders = nil;
    	return error;
    end
    Last edited by Worm; 03-04-2004, 04:04 PM.

  • #2
    Copy folder

    I believe this is what I am looking for but I can't get it to work.
    I have a page in AutoPlay with an option to click on file to copy to computer. When the person selects a file, I would like the screen "where would you like to save folder?" to popup. and when the person selects where they would like to save it, the linked folder saves there into that spot. Is there such a code? Will

    Comment


    • #3
      Can you expand for a newbie

      I work with Willie, who asked this and we are still having a problem. It is really about understanding how to use your code. We are newbie programmers (more designers). We copied and pasted your code into AMS 5, but it gives us an error that says copyfolder is nil.

      What are we missing? How do you add it to AMS 5 and what part of the code do we change to make it work? Sorry for seeming so dumb, but we want to learn and have few resources (programmers) to help us understand. If you could walk us through how to add your code in AMS 5 and how to alter it to do what we want, that would help us learn a lot in a short period and deliver our product. Thanks so much.

      Alan Eisenberg
      Alan Eisenberg
      Multimedia Director
      Mind & Media, Inc.

      Comment


      • #4
        From your post it looks like you want to copy a single file. If that's the case, this function is pure overkill. I think you're looking for something more like this.

        Code:
        --ask for the file to copy
        tblFile = Dialog.FileBrowse(true, "Select the file to copy", _DesktopFolder, "All Files (*.*)|*.*|", "", "", false, true)
        
        --if the user didn't cancel
        if tblFile[1] ~= "CANCEL" then
        	--Get the folder to copy the file to
        	sFolder = Dialog.FolderBrowse("Please Select the folder to copy the file to.", _DesktopFolder)
        	--if the user didn't cancel
        	if sFolder ~= "CANCEL" then
        		--copy the file to the folder
        		File.Copy(tblFile[1], sFolder, false, true, true, false, nil)
        	end
        end

        Originally posted by willfreer
        I believe this is what I am looking for but I can't get it to work.
        I have a page in AutoPlay with an option to click on file to copy to computer. When the person selects a file, I would like the screen "where would you like to save folder?" to popup. and when the person selects where they would like to save it, the linked folder saves there into that spot. Is there such a code? Will

        Comment


        • #5
          Actually we want to copy a folder

          No. Actually we want to copy a whole folder that is on the CD and put it on the user's computer. So, we do want the function where we program which folder to copy and have the user specify where they want to put it.

          I believe that is the code you have. We just don't know how to apply and alter the code in AMS 5. Can you walk me through that (i.e. - First copy the top code, then...etc.)

          Thank you again for your help.
          Alan Eisenberg
          Multimedia Director
          Mind & Media, Inc.

          Comment


          • #6
            Here's an example to look at.
            Attached Files

            Comment


            • #7
              Thank you - one last question

              Thank you so much for this. It is almost exactly what we need. But, we know what the source folder is, we just need to specify the destination folder.

              What do we alter to make that work? Again, thanks for helping teach us how. This was a big help.

              Alan
              Alan Eisenberg
              Multimedia Director
              Mind & Media, Inc.

              Comment


              • #8
                Change the code in the label to this:

                Code:
                --get the destination folder
                sDestFolder = Dialog.FolderBrowse("Select the Destination Folder", _DesktopFolder)
                --see if the user cancelled
                if sDestFolder ~= "CANCEL" then
                	--initiate the copy
                	--CopyFolder is function in the Global Functions area of the project (Alt-D-F)
                	if CopyFolder(_SourceFolder .. "\\Your\\Path\\Here", sDestFolder) == 0 then
                		result = Dialog.Message("Folder Copy", "The folder has been successfully copied!", MB_OK, MB_ICONINFORMATION, MB_DEFBUTTON1);
                	else
                		result = Dialog.Message("Folder Copy", "There was an error copying the folder.", MB_OK, MB_ICONEXCLAMATION, MB_DEFBUTTON1);
                	end
                end
                Originally posted by aeisenbe
                Thank you so much for this. It is almost exactly what we need. But, we know what the source folder is, we just need to specify the destination folder.

                What do we alter to make that work? Again, thanks for helping teach us how. This was a big help.

                Alan

                Comment


                • #9
                  Last Question

                  OK, last question. Will this work in Preview mode in AMS 5 or only once it's built. It's not working in Preview mode, but I get the impression it will have to build first before it works. It gives me an "error copying the folder" message. Is that right?

                  Alan Eisenberg
                  Alan Eisenberg
                  Multimedia Director
                  Mind & Media, Inc.

                  Comment


                  • #10
                    It should work in either situation.

                    Could you post the snippet of code that is giving you problems? It'd be easier for me to help, if I weren't guessing at what you are attempting to accomplish.

                    Comment


                    • #11
                      Got it to Work

                      Actually I got it to work. The problem was seems to be that the source and destination folders I was specifying were both C: drive folders. For example, if I said:
                      ---------------------------------------------
                      if CopyFolder("C:\\test", "C:\\Documents and Settings\fred\Desktop\test") == 0 then
                      result = Dialog.Message("Folder Copy", "The folder has been successfully copied!", MB_OK, MB_ICONINFORMATION, MB_DEFBUTTON1);
                      else
                      result = Dialog.Message("Folder Copy", "There was an error copying the folder.", MB_OK, MB_ICONEXCLAMATION, MB_DEFBUTTON1);
                      end
                      ---------------------------------------------------

                      Then I would get an error message. If I changed it to a source file at a different location:

                      ---------------------------------------------
                      if CopyFolder("D:\\test", "C:\\Documents and Settings\fred\Desktop\test") == 0 then
                      result = Dialog.Message("Folder Copy", "The folder has been successfully copied!", MB_OK, MB_ICONINFORMATION, MB_DEFBUTTON1);
                      else
                      result = Dialog.Message("Folder Copy", "There was an error copying the folder.", MB_OK, MB_ICONEXCLAMATION, MB_DEFBUTTON1);
                      end
                      ---------------------------------------------------

                      Then it will work. It doesn't seem to like to move things around on the same level. Of course, my real case would be a folder on the CD, not on the C: drive, I was only doing this as a test.

                      I still do have one outstanding question (of course). The code you gave me to my question before asks for the Source folder, but I can hard-code the destination. I wanted the opposite. I know what Source folder I want to copy from my CD, but I want the user to specify where on their hard drive they want to put it. I can put in the path for the source folder myself. Do you have the code for that.

                      So what I want is for the user to be prompted to place a folder that I have already chosen somewhere on their hard drive. I don't need them to be asked what source folder they want to pick. If you've got that handy, that would be it. Otherwise I thank you so much and you have saved the day for us.
                      Alan Eisenberg
                      Multimedia Director
                      Mind & Media, Inc.

                      Comment


                      • #12
                        I've used this function many times and never had issues when copying to the same drive. I'd guess that one of the folders doesn't exist. The function needs both the source and destination folders to already exist.

                        I posted the code to ask for the destination only above, but here it is again.
                        Code:
                        --get the destination folder
                        sDestFolder = Dialog.FolderBrowse("Select the Destination Folder", _DesktopFolder)
                        --see if the user cancelled
                        if sDestFolder ~= "CANCEL" then
                        	--initiate the copy
                        	--CopyFolder is function in the Global Functions area of the project (Alt-D-F)
                        	if CopyFolder("C:\\Your\\Path\\Here", sDestFolder) == 0 then
                        		result = Dialog.Message("Folder Copy", "The folder has been successfully copied!", MB_OK, MB_ICONINFORMATION, MB_DEFBUTTON1);
                        	else
                        		result = Dialog.Message("Folder Copy", "There was an error copying the folder.", MB_OK, MB_ICONEXCLAMATION, MB_DEFBUTTON1);
                        	end
                        end

                        Comment


                        • #13
                          Also, make sure you are escaping your slashes. Instead of:

                          "C:\\Documents and Settings\fred\Desktop\test"

                          use:

                          "C:\\Documents and Settings\\fred\\Desktop\\test"

                          Comment


                          • #14
                            Nice catch Brett, I thought I'd looked for that.

                            Comment


                            • #15
                              (Newbie) Copy Folder

                              Alan and I keep geting the message "There was an error copying the folder." and we can't find where the code breakdowns. This is what we have.

                              --get the destination folder
                              sDestFolder = Dialog.FolderBrowse("Select the Destination Folder", _DesktopFolder)
                              --see if the user cancelled
                              if sDestFolder ~= "CANCEL" then
                              --initiate the copy
                              --CopyFolder is function in the Global Functions area of the project (Alt-D-F)
                              if CopyFolder("\\Media\\Users\\Alan\\AV Video Article", sDestFolder) == 0 then
                              result = Dialog.Message("Folder Copy", "The folder has been successfully copied!", MB_OK, MB_ICONINFORMATION, MB_DEFBUTTON1);
                              else
                              result = Dialog.Message("Folder Copy", "There was an error copying the folder.", MB_OK, MB_ICONEXCLAMATION, MB_DEFBUTTON1);
                              end
                              end


                              See anything that stands out? Thanks for all your help.

                              Comment

                              Working...
                              X