Home Coding, Scripting, Shaders

(SOLVED) MAXScript - Copy File From Directory and Paste In Directory

Hey everyone. I've been learning MAXScript for the past month and I am making something that allows a user to select a file in a Directory, copy/cut it and paste said file in a new specifiied directory. I think I've managed to get as far as showing a dialog rollout with a Browse function for the file I want to select and to select the directory I want to paste it in. The problem now is how exactly can I copy/paste the file, essentially emulating Window's "Copy" "Paste" feature within a MAXScript file? The file I'm trying to copy is an .mse file, so copying contents is essentially null.

I've tried copyFIle() but as far as I understand all it does is copy the contents from file A and write them in file B, which is not what i want.

I've also looked at DOSCommands but its copy function does not seem to be working correctly. Is there something I should be looking for or am missing?

It doesn't give me any error or anything, it simply just quickly flashes a window and does nothing.

I'll paste the snippet of the code in question ^^

Thank you in advance!

--------------------------- CODE SNIPPET-----------------------------------
(
   
    -------------------------------- GUI --------------------------------
    
    Global mseFileDir = ""
    Global mseFileName = ""
    
    Global mcrFolderDir = ""
    Global mcrFileName = ""
    
    rollout msePaths "MSE File"
    (
        -- Rollout UI
        editText mse_path "" labelOnTop:true
        button btn_mseBrowse "Browse..." width:100 height:20 align:#left
        -- End Rollout UI
        
        -- UI Functionality
        
        on btn_mseBrowse pressed do
        (
            mseFilePath = ""
            mseFileName = ""
            mseFilePath = getOpenFileName caption:"Open File"   types:"MSE(*.mse)|*.mse|"
            
            if (mseFilePath != undefined) do
            (
                mse_path.text = mseFilePath
                mseFileName = filenameFromPath mseFilePath
            )
            
            if (mseFilePath != undefined) do
            (
                append mseFileDir mseFilePath
            )
            
            print (mseFileDir)
            print (mseFileName)
        )
        
        -- End UI Functionality
    )
    
    rollout mcrPaths "Macroscript Folder"
    (
        -- Rollout UI
        editText mcr_path "" labelOnTop:true
        button btn_mcrPathBrowse "Browse..." width:100 height:20 align:#left
        -- End Rollout UI
        
        -- UI Functionality
        
        on btn_mcrPathBrowse pressed do
        (
            mcrFolderPath = ""
            mcrFolderPath = getSavePath()
            
            if (mcrFolderPath != undefined) do
            (
                mcr_path.text = mcrFolderPath
                append mcrFolderDir (mcrFolderPath)
                mcrFileName = mseFileName + ".mcr"
            )
            
            print (mcrFolderDir)
            print (mcrFileName)
        )
        
        -- End UI Functionality
    )
    
    rollout scriptApply "Apply"
    (
        -- Rollout UI
        
        button btn_Apply "Apply" width:100 height:20 align:#left
        
        -- End Rollout UI
        
        -- UI Functionality
        
        on btn_Install pressed do
        (
            if (mseFilePath != undefined and  mseFileName != undefined and mcrFolderPath != undefined) do
            (
                messageBox("MSE File is: " + mseFileName + " and MCR Folder Path is: " + mcrFolderPath)
                DOSCommand ("copy " + mseFileName + " " + mcrFolderDir)
            )
        )
        
        -- End UI Functionality
    )
    
    scriptApply = newRolloutFloater "Copy and Paste" 420 220
    addRollout msePaths scriptApply
    addRollout mcrPaths scriptApply
    addRollout scriptInstall scriptApply
)




Replies

  • miauu
    Options
    Offline / Send Message
    miauu polycounter lvl 14
    Sorry but I don't have much time to polush the code.
    [code]
    --------------------------- CODE SNIPPET-----------------------------------
    (
      
        -------------------------------- GUI --------------------------------
       
        Global mseFileDir = ""
        Global mseFileName = ""
       
        Global mcrFolderDir = ""
        Global mcrFileName = ""
     
       
        rollout scriptApply "Apply"
        (
           
            local mseFilePath = undefined
            local mseFileName = undefined
            local mcrFolderPath = undefined
           
            editText mcr_path "" labelOnTop:true
            button btn_mcrPathBrowse "Browse..." width:100 height:20 align:#left
           
             editText mse_path "" labelOnTop:true
            button btn_mseBrowse "Browse..." width:100 height:20 align:#left
           
            -- Rollout UI
           
            button btn_Install "Apply" width:100 height:20 align:#left
           
            -- End Rollout UI
           
            -- UI Functionality
           
            on btn_mcrPathBrowse pressed do
            (
                mcrFolderPath = ""
                mcrFolderPath = getSavePath()
               
                if (mcrFolderPath != undefined) do
                (
                    mcr_path.text = mcrFolderPath
                    append mcrFolderDir (mcrFolderPath)
                    mcrFileName = mseFileName + ".mcr"
                )
               
                print (mcrFolderDir)
                print (mcrFileName)
            )

            on btn_mseBrowse pressed do
            (
                mseFilePath = ""
                mseFileName = ""
                mseFilePath = getOpenFileName caption:"Open File"   types:"MSE(*.mse)|*.mse|"
               
                if (mseFilePath != undefined) do
                (
                    mse_path.text = mseFilePath
                    mseFileName = filenameFromPath mseFilePath
                )
               
                if (mseFilePath != undefined) do
                (
                    append mseFileDir mseFilePath
                )
               
                print (mseFileDir)
                print (mseFileName)
            )       
           
            on btn_Install pressed do
            (
                format "mseFilePath: % \n" mseFilePath
                format "mcrFolderPath: % \n" mcrFolderPath
    --             if (mseFilePath != undefined and  mseFileName != undefined and mcrFolderPath != undefined) do
    --             (
    --                 messageBox("MSE File is: " + mseFileName + " and MCR Folder Path is: " + mcrFolderPath)
                   
                    copyFile mseFileName (mcrFolderPath + "\\" + mseFileName)
                   
    --             )
            )
           
            -- End UI Functionality
        )
        createDialog scriptApply
    --     scriptApply = newRolloutFloater "Copy and Paste" 420 220
    --     addRollout msePaths scriptApply
    --     addRollout mcrPaths scriptApply
    --     addRollout scriptInstall scriptApply
    )

    [/code]
    Don't use so many global variables.
  • r0fld4nc3
    Options
    Offline / Send Message

    Hey miauu,

    Thank you so so much for this! It worked like a charm :)

    I'll inspect it further as see if I can replicate something similar without looking  to interiorise what I've learned!

    Again, thank you thank you!!
  • Tekoppar
    Options
    Offline / Send Message
    Tekoppar polycounter lvl 10
    It should be noted that copyFile can fail for several reasons and returns whether or not the copy was successful. Also you get an error if you select the folder before selecting a file.

    To explain the changes I did, I removed the globals as they're not needed, I added a check to see if the chosen folder/file is writeable/exists and if the copyFile fails we check if the file already existed. The reason behind the changes is to avoid any assumptions that anything worked. If we didn't bother to check if the folders were writeable the code assumes that they are. But by checking we avoid that and if it is we can take action and give the user a better experience.

    The reason I check if the copied file exists after trying is that the chance of it existing before the copy is smaller then that it would exists, so in most cases we avoid having to check if it exists, but if it does we have it.
    --------------------------- CODE SNIPPET-----------------------------------
    (
     
        -------------------------------- GUI --------------------------------
        rollout scriptApply "Apply"
        (
          
            local mseFilePath = undefined
            local mseFileName = undefined
            local mcrFolderPath = undefined
          
            editText mcr_path "" labelOnTop:true
            button btn_mcrPathBrowse "Browse..." width:100 height:20 align:#left
          
             editText mse_path "" labelOnTop:true
            button btn_mseBrowse "Browse..." width:100 height:20 align:#left
          
            -- Rollout UI
          
            button btn_Install "Apply" width:100 height:20 align:#left
          
            -- End Rollout UI
          
            -- UI Functionality
          
            on btn_mcrPathBrowse pressed do
            (
                mcrFolderPath = getSavePath()
              
                if (mcrFolderPath.count > 0) do
                (
                    mcr_path.text = mcrFolderPath
                )
            )

            on btn_mseBrowse pressed do
            (
                mseFilePath = getOpenFileName caption:"Open File"   types:"MSE(*.mse)|*.mse|"
              
                if (mseFilePath.count > 0) do
                (
                    mse_path.text = mseFilePath
                )
            )      
          
            on btn_Install pressed do
            (
                sPath = mcr_path.text
                sName = filenameFromPath(mse_path.text)
               
                --check if the chosen directory is writeable
                writ = isDirectoryWriteable sPath
                --check if the selected file exists
                exists = doesFileExist(mse_path.text)
                if  (writ and exists) then (
                    val = copyFile mse_path.text (sPath + "\\" + sName)
                    if (val == false) do (
                        --if copyfile fails, check if file already exists since we alredy know
                        --that the folder is writeable
                        val = doesFileExist (sPath + "\\" + sName)
                        if (val) then (
                            print "File already exists, ask user if he wants to replace it"
                        ) else (
                            print "File doesn't exist"
                        )
                    )
                ) else (
                    if (writ == false) do (
                        print "Directory isn't writeable"
                    )
                    if (exists == false) do (
                        print "mse file doesn't exist"
                    )
                )
            )
          
            -- End UI Functionality
        )
        createDialog scriptApply
    --     scriptApply = newRolloutFloater "Copy and Paste" 420 220
    --     addRollout msePaths scriptApply
    --     addRollout mcrPaths scriptApply
    --     addRollout scriptInstall scriptApply
    )


  • r0fld4nc3
    Options
    Offline / Send Message
      @Tekoppar I see. I'm glad that you mentioned it still. Since miauu replied I did make sure to add a few other things and it no longer fails if the folder is selected before the file. It is all working as intended but I thank you for bringing this to my attention too! The fact that copyFile could fail, etc, is very useful to know and I'll be modifying what I have to further accommodate your necessary changes. Thank you very much :)
Sign In or Register to comment.