Jump to content

[beginner] How to use DirCopy() when source dir is unknown?


 Share

Recommended Posts

Hi there,

I am trying write a script that begins by moving a folder from a newly inserted USB onto C:\ of the host computer.  At the moment I am using DirCopy("X:\Setup","C:\Install_notes\Setup") where X:\ is the USB.  Unfortunately, every time I insert the USB into a new computer the USB directory letter will be randomly changed.  Is there a way to set the location of the FileMove script as the source directory or any other method of executing this?

Screenshot.PNG

Link to comment
Share on other sites

You can move the directories relative to the script, but you cannot move the directory that the script is currently running from...because the script is running.

you can make a little example in the folder and play with the syntax:

;if the target directory does not exist, then this serves to 'change the folder the name' rather than 'cut and paste'.

DirMove(@ScriptDir & "\StartupController" , @ScriptDir & "\testfolder" , 1) ;moves the StartupController Directory to the testfolder directory

 

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

1 hour ago, Subz said:

You could try DriveGetDrive to get an array of removable drives or all then loop through the array and use if fileexists($aDrives[$i] & "\Setup") Then DirCopy(...)

Solution worked perfectly, modified some code from the FileExists() help page to make this:

 

#include <AutoItConstants.au3>
#include <MsgBoxConstants.au3>

;Finds the drive path of the USB
Local $aArray = DriveGetDrive($DT_REMOVABLE)
If @error Then
    ;An error occurred when retrieving the drives.
    MsgBox($MB_SYSTEMMODAL, "", "It appears an error occurred.")
Else
    For $i = 1 To $aArray[0]
        ;Show all the drives found and convert the drive letter to uppercase.
        ;MsgBox($MB_SYSTEMMODAL, "", "Drive " & $i & "/" & $aArray[0] & ":" & @CRLF & StringUpper($aArray[$i]))
        If FileExists($aArray[$i]&"\setup\USB_Search_Target") Then

           ;Removes setup folder because DirCopy is stupid and needs to create a new folder
           DirRemove("C:\Users\admin\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup")
           sleep(200)

           ;Moves main scripts & installers to C:\
           DirCopy($aArray[$i]&"\Setup","C:\Install_notes\Setup",1)
           sleep(200)

           ;Moves the startup script to startup folder
           DirCopy($aArray[$i]&"\StartupController","C:\Users\admin\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup",1)
           sleep(500)

           Shutdown(6)
           ;MsgBox(4096,"","Test Works")
         EndIf
    Next
 EndIf

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...