Jump to content

Script to execute file activity when USB stick inserted?


Recommended Posts

Wanted to put this in the scripts area but don't have permission to make inquiries there.  I have a media server with no keyboard.  I want to occasionally copy movies to it and that would consist of:

 

1. Insert USB Stick

2. Delete contents of C:\Videos

3. Copy contents of <usb_stick>\VIdeos to c:\Videos

In theory I'd like to do this with no user interaction unless whatever needs to be done could work with my up and down and OK buttons on the remote.  Thoughts?

Thanks.

 

JR

Link to comment
Share on other sites

Hi jriker1

Something like this? :)

 

What my script does:
1. Waits for the USB stick to be inserted
2. Deletes all contents of C:\Videos
3. Moves all contents (including folders etc.) from your USB stick to C:\Videos
4. Waits for you to remove the USB stick again, and when you do, the script continues..

Just set the script to start up with windows.

 

#NoTrayIcon

$USB_Drive = 'E:\' ; <-- change to match the drive letter of your USB stick :-)
$Videos_Folder = 'C:\Videos'

While 1

$Check_drive = FileExists($USB_Drive)

Sleep(500)


If $Check_drive = 1 Then

   ConsoleWrite("USB Stick is active..." & @CRLF)


DirRemove($Videos_Folder, 1)
DirCreate($Videos_Folder)


SplashTextOn("", "Copying files...", 300, 55, -1 , -1, 1, "@calibri", "16","600")

FileMove($USB_Drive & "\*.*", $Videos_Folder, 1) ;Move all source files first
$hSearch = FileFindFirstFile($USB_Drive & "\*.*") ;Now find any remaining (in this case: folders)
if $hSearch = -1 then exit ;No folders
While 1
    $hFilename = FileFindNextFile($hSearch)
    if @ERROR then exitloop ;No more files
    DirMove($USB_Drive & "\" & $hFilename, $Videos_Folder,1);move subdir and all contents to new location
WEnd

SplashOff()
Sleep(500)
SplashTextOn("", "Copy complete", 300, 55, -1 , -1, 1, "@calibri", "16","600")
Sleep(5000)
SplashOff()

Do

$Check_drive = FileExists($USB_Drive)
ConsoleWrite("Waiting for USB Stick to be removed again..." & @CRLF)

Sleep(500)

Until $Check_drive = 0



Else

    ConsoleWrite("USB Stick is NOT active..." & @CRLF)

EndIf




WEnd

 

/David

Edited by david1337
Link to comment
Share on other sites

David, AWESOME!!!! Haven't tried it yet but looks good reading thru it.  Even a visual indicator of what's happening!!!  One other potential things I can see value in and will test what you provided in a day or two, there is potential that the videos will be in use as the application that they are watched thru is always running.  Or after the new videos are uploaded the application won't dynamically see the changes.  App being in use may not effect it but hard to say.  Is there a way to do one of two things?

 

1. Shutdown a given application that is running  when the USB stick is entered (waiting for it to end/kill) and then start it up after the USB stick is removed.

2. Or worst case, when the USB stick is removed reboot Windows so the app can auto restart and read in the changes.

 

Thanks.

 

JR

 

Edited by jriker1
Link to comment
Share on other sites

jriker1,

Yes that is pretty easy to achieve :)

Change $Application_ProcessName and $Application_FullPath to match your application.

 

#NoTrayIcon

$USB_Drive = 'E:\' ; <-- change to match the drive letter of your USB stick :-)
$Videos_Folder = 'C:\Videos'

$Application_ProcessName = "chrome.exe"
$Application_FullPath = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"

While 1

$Check_drive = FileExists($USB_Drive)

Sleep(500)


If $Check_drive = 1 Then

   ConsoleWrite("USB Stick is active..." & @CRLF)

ProcessClose($Application_ProcessName)
ProcessWaitClose($Application_ProcessName)

DirRemove($Videos_Folder, 1)
DirCreate($Videos_Folder)


SplashTextOn("", "Copying files...", 300, 55, -1 , -1, 1, "@calibri", "16","600")

FileMove($USB_Drive & "\*.*", $Videos_Folder, 1) ;Move all source files first
$hSearch = FileFindFirstFile($USB_Drive & "\*.*") ;Now find any remaining (in this case: folders)
if $hSearch = -1 then exit ;No folders
While 1
    $hFilename = FileFindNextFile($hSearch)
    if @ERROR then exitloop ;No more files
    DirMove($USB_Drive & "\" & $hFilename, $Videos_Folder,1);move subdir and all contents to new location
WEnd

SplashOff()
Sleep(500)
SplashTextOn("", "Copy complete", 300, 55, -1 , -1, 1, "@calibri", "16","600")
Sleep(5000)
SplashOff()

Do

$Check_drive = FileExists($USB_Drive)
ConsoleWrite("Waiting for USB Stick to be removed again..." & @CRLF)

Sleep(500)

Until $Check_drive = 0

ShellExecute($Application_FullPath)


Else

    ConsoleWrite("USB Stick is NOT active..." & @CRLF)

EndIf




WEnd

 

Link to comment
Share on other sites

Thanks David.  Last part of the equation.  To get this to run after the device boots up and Windows 10 logs in, do I need to convert this to an executable file and add it to the startup folder or something?  Based on your sleep command would assume it never ends?  Unless it does after a stick is inserted and removed.

Juvigy, are you referring to putting said tweaked version of the script in exe format on the usb stick and when it is plugged in have Windows popup a what do you want to do with this thing and select the executable and let it run rather than running all the time in the background?

Like to continue the convo, just here's what I tweaked based on David's script

 

#NoTrayIcon

$USB_Drive = 'D:\' ; <-- change to match the drive letter of your USB stick :-)
$USB_Drive_Movies = 'D:\Movies\' ; <-- path to movies
$Videos_Folder = 'C:\Movies'

$Application_ProcessName = "Launcher4Kodi.exe"
$Application_FullPath = "C:\Program Files (x86)\Launcher4Kodi\Launcher4Kodi.exe"
$Application_ProcessName2 = "kodi.exe"
$Application_FullPath2 = "C:\Program Files (x86)\Kodi\kodi.exe"

While 1

$Check_drive = FileExists($USB_Drive)

Sleep(500)


If $Check_drive = 1 Then

   ConsoleWrite("USB Stick is active..." & @CRLF)

ProcessClose($Application_ProcessName)
ProcessWaitClose($Application_ProcessName)

ProcessClose($Application_ProcessName2)
ProcessWaitClose($Application_ProcessName2)

DirRemove($Videos_Folder, 1)
DirCreate($Videos_Folder)


SplashTextOn("", "Copying files...", 300, 55, -1 , -1, 1, "@calibri", "16","600")

FileMove($USB_Drive_Movies & "\*.*", $Videos_Folder, 1) ;Move all source files first
$hSearch = FileFindFirstFile($USB_Drive_Movies & "\*.*") ;Now find any remaining (in this case: folders)
if $hSearch = -1 then exit ;No folders
While 1
    $hFilename = FileFindNextFile($hSearch)
    if @ERROR then exitloop ;No more files
    DirMove($USB_Drive_Movies & "\" & $hFilename, $Videos_Folder,1);move subdir and all contents to new location
WEnd

SplashOff()
Sleep(500)
SplashTextOn("", "Copy complete", 300, 55, -1 , -1, 1, "@calibri", "16","600")
Sleep(5000)
SplashOff()

Do

$Check_drive = FileExists($USB_Drive)
ConsoleWrite("Waiting for USB Stick to be removed again..." & @CRLF)

Sleep(500)

Until $Check_drive = 0

ShellExecute($Application_FullPath)

Else

    ConsoleWrite("USB Stick is NOT active..." & @CRLF)

EndIf

WEnd

So I added in an extra application as Launcher4Kodi has to be killed or it will restart Kodi, and then kill Kodi.  Plus if Launcher4Kodi isn't stopped, it won't start the app again so better to kill both and then relaunch the launcher which then launches Kodi.  I know I have an unused Application_FullPath2.  I also tweaked the script to point to all content within a Movies folder instead of the root.  Does what I did to take all the content in the <USB>:\Movies folder, and stick it into the C:\Movies folder make sense or will it still pull from the root of the usb stick?  My thought process I am going to have TVShows and Music folders over time as well so need to compartmentalize the content so the script knows what goes where.  Thinking at some point will need a "does the Movies folder exist on the USB Stick" check before deleting the C:\Movies folder, as for example if I only upload Movies once in a while but generally upload TV shows, won't want the movies being deleted until I am actually trying to upload new ones to the hard drive.

 

Maybe something like the below for the above thoughts?

#NoTrayIcon

$USB_Drive = 'D:\' ; <-- change to match the drive letter of your USB stick :-)
$USB_Drive_Movies = 'D:\Movies\' ; <-- path to movies
$USB_Drive_TVShows = 'D:\TVShows\' ; <-- path to TV Shows
$USB_Drive_Music = 'D:\Music\' ; <-- path to Music
$Movies_Folder = 'C:\Movies'
$TVShows_Folder = 'C:\TVShows'
$Music_Folder = 'C:\Music'

$Application_ProcessName = "Launcher4Kodi.exe"
$Application_FullPath = "C:\Program Files (x86)\Launcher4Kodi\Launcher4Kodi.exe"
$Application_ProcessName2 = "kodi.exe"
$Application_FullPath2 = "C:\Program Files (x86)\Kodi\kodi.exe"

While 1

$Check_drive = FileExists($USB_Drive)

Sleep(500)


If $Check_drive = 1 Then

   ConsoleWrite("USB Stick is active..." & @CRLF)

ProcessClose($Application_ProcessName)
ProcessWaitClose($Application_ProcessName)

ProcessClose($Application_ProcessName2)
ProcessWaitClose($Application_ProcessName2)

If FileExists($USB_Drive_Movies) Then

    DirRemove($Movies_Folder, 1)
    DirCreate($Movies_Folder)


    SplashTextOn("", "Copying Movie files...", 300, 55, -1 , -1, 1, "@calibri", "16","600")


    FileMove($USB_Drive_Movies & "\*.*", $Movies_Folder, 1) ;Move all source files first
    $hSearch = FileFindFirstFile($USB_Drive_Movies & "\*.*") ;Now find any remaining (in this case: folders)
    if $hSearch = -1 then exit ;No folders
    While 1
        $hFilename = FileFindNextFile($hSearch)
        if @ERROR then exitloop ;No more files
        DirMove($USB_Drive_Movies & "\" & $hFilename, $Movies_Folder,1);move subdir and all contents to new location
    WEnd

    SplashOff()
    Sleep(500)
    SplashTextOn("", "Copy complete", 300, 55, -1 , -1, 1, "@calibri", "16","600")
    Sleep(5000)
    SplashOff()
EndIf



If FileExists($USB_Drive_TVShows) Then

    DirRemove($TVShows_Folder, 1)
    DirCreate($TVShows_Folder)


    SplashTextOn("", "Copying TV Show files...", 300, 55, -1 , -1, 1, "@calibri", "16","600")


    FileMove($USB_Drive_TVShows & "\*.*", $TVShows_Folder, 1) ;Move all source files first
    $hSearch = FileFindFirstFile($USB_Drive_TVShows & "\*.*") ;Now find any remaining (in this case: folders)
    if $hSearch = -1 then exit ;No folders
    While 1
        $hFilename = FileFindNextFile($hSearch)
        if @ERROR then exitloop ;No more files
        DirMove($USB_Drive_TVShows & "\" & $hFilename, $TVShows_Folder,1);move subdir and all contents to new location
    WEnd

    SplashOff()
    Sleep(500)
    SplashTextOn("", "Copy complete", 300, 55, -1 , -1, 1, "@calibri", "16","600")
    Sleep(5000)
    SplashOff()
EndIf




If FileExists($USB_Drive_Music) Then

    DirRemove($Music_Folder, 1)
    DirCreate($Music_Folder)


    SplashTextOn("", "Copying Music files...", 300, 55, -1 , -1, 1, "@calibri", "16","600")


    FileMove($USB_Drive_Music & "\*.*", $Music_Folder, 1) ;Move all source files first
    $hSearch = FileFindFirstFile($USB_Drive_Music & "\*.*") ;Now find any remaining (in this case: folders)
    if $hSearch = -1 then exit ;No folders
    While 1
        $hFilename = FileFindNextFile($hSearch)
        if @ERROR then exitloop ;No more files
        DirMove($USB_Drive_Music & "\" & $hFilename, $TVShows_Folder,1);move subdir and all contents to new location
    WEnd

    SplashOff()
    Sleep(500)
    SplashTextOn("", "Copy complete", 300, 55, -1 , -1, 1, "@calibri", "16","600")
    Sleep(5000)
    SplashOff()
EndIf




Do
    $Check_drive = FileExists($USB_Drive)
    ConsoleWrite("Waiting for USB Stick to be removed again..." & @CRLF)
    Sleep(500)
Until $Check_drive = 0

ShellExecute($Application_FullPath)

Else

    ConsoleWrite("USB Stick is NOT active..." & @CRLF)

EndIf

WEnd

Thanks.

 

JR

Edited by jriker1
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...