Jump to content

Automatic executing script of USB

kung.lao

Hello, 

 

I'm trying to to make a script that automatically executes when you plug the USB drive in a windows computer. So from the moment it is plugged in, it should copy all files in a certain folder of the USB drive to a given location on the computer. So is it possible to put a script on a USB drive that automatically executes? What would be the easiest way to do this? I have a little bit of experience in python, but am willing to do some more research and write it in another language if that would be easier

 

Thanks

Link to comment
Share on other sites

Link to post
Share on other sites

Autorun.inf -- Google it for examples of how to use it. It will allow a program to be auto-run when the drive is mounted into the file system.

Wife's build: Amethyst - Ryzen 9 3900X, 32GB G.Skill Ripjaws V DDR4-3200, ASUS Prime X570-P, EVGA RTX 3080 FTW3 12GB, Corsair Obsidian 750D, Corsair RM1000 (yellow label)

My build: Mira - Ryzen 7 3700X, 32GB EVGA DDR4-3200, ASUS Prime X470-PRO, EVGA RTX 3070 XC3, beQuiet Dark Base 900, EVGA 1000 G6

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, kung.lao said:

Hello, 

 

I'm trying to to make a script that automatically executes when you plug the USB drive in a windows computer. So from the moment it is plugged in, it should copy all files in a certain folder of the USB drive to a given location on the computer. So is it possible to put a script on a USB drive that automatically executes? What would be the easiest way to do this? I have a little bit of experience in python, but am willing to do some more research and write it in another language if that would be easier

 

Thanks

I have to say, this is one very good idea.

 

I am good at python coding but do not know very advanced stuff. My understandings stretch as far as Codecademy. However, I know you can make a .bat (batch file) file which executes the script whenever the USB is plugged into a machine. Google it or search it on youtube. If that doesn't work, then try to search the different segments - for example, search for a video telling you how to make the batch file and another telling you how to make the python program copy the files or so. PM me if you can get this working, I'm curious!

||| Drakon (Desktop Build) |||

|| CPU: 3800X || Cooler: Kraken X63 || Motherboard: B450 Aorus M || Memory: HyperX DDR4-3200MHz 16G ||

|| Storage: 512GB 970 Pro + 500GB 850 EVO + 250GB 850 EVO + 1TB HDD + 2TB HDD || Graphics Card: RX 5700 XT Red Devil || Case: Thermaltake Core V21 || PSU: XFX XTR 750W 80+Gold || 

 

Link to comment
Share on other sites

Link to post
Share on other sites

I did something similar last week with an external drive. I ended up going to the event viewer and created a scheduled task from the event when I connected the external drive. The task basically just starts a sync program installed on the external drive as soon as I connect the external drive.

Link to comment
Share on other sites

Link to post
Share on other sites

On 8-3-2016 at 7:09 PM, brandishwar said:

Autorun.inf -- Google it for examples of how to use it. It will allow a program to be auto-run when the drive is mounted into the file system.

Newer versions of windows don't support these anymore, so I won't be able to use it, but thanks for the tip.

On 8-3-2016 at 7:09 PM, Aleksbgbg said:

I have to say, this is one very good idea.

 

I am good at python coding but do not know very advanced stuff. My understandings stretch as far as Codecademy. However, I know you can make a .bat (batch file) file which executes the script whenever the USB is plugged into a machine. Google it or search it on youtube. If that doesn't work, then try to search the different segments - for example, search for a video telling you how to make the batch file and another telling you how to make the python program copy the files or so. PM me if you can get this working, I'm curious!

I'm looking up things about those batch files at the moment and will certainly share the solution if I can fix it.

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, kung.lao said:

Newer versions of windows don't support these anymore, so I won't be able to use it, but thanks for the tip.

I'm looking up things about those batch files at the moment and will certainly share the solution if I can fix it.

Ok, I was gonna let you solve it, but here, I solved it:

 

@echo off
set /p drive_s="Insert source of removable media: "
set /p drive_d="Insert destination of copied files: "

echo -------------------------------------------------------------------------------
echo Your programs from %drive_s% will be copied to %drive_d%

echo -------------------------------------------------------------------------------
pause
robocopy %drive_s% %drive_d% /e
pause

 

The above is code that you need to copy and paste into a notepad file. Then proceed to save the notepad file in a desired location, however - make sure to set 'Save as type:' to All Files (*.*) and name the file [insert desired name here].bat

 

Now, the only problem with this program is that if the destination is on the drive (C: or D: or E: and etc.) which contains this file, the files copied will be put in the same directory as this .bat file (batch file). Therefore, I would put it on a separate hard drive if possible or try to find the problem with this, I just don't have the time.

 

Continuing, you could make a task in task scheduler that runs this script every time when removable media is connected. Otherwise, you could run it yourself every time you connect removable media or need to copy stuff from removable media. It does take user input though - you will have to type in the source and destination of the files you want copied, with the exact paths. This means that everything inside but not including the last directory in the path will be copied.

 

So, it is not fully automated but it does speed up the process, except it doesn't use Python at all. Tell me what you think :)

||| Drakon (Desktop Build) |||

|| CPU: 3800X || Cooler: Kraken X63 || Motherboard: B450 Aorus M || Memory: HyperX DDR4-3200MHz 16G ||

|| Storage: 512GB 970 Pro + 500GB 850 EVO + 250GB 850 EVO + 1TB HDD + 2TB HDD || Graphics Card: RX 5700 XT Red Devil || Case: Thermaltake Core V21 || PSU: XFX XTR 750W 80+Gold || 

 

Link to comment
Share on other sites

Link to post
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

×