ethrbunny has asked for the wisdom of the Perl Monks concerning the following question:

Two part question:

1) I want to run a script after rebooting a windows box. Generally this just means putting it in the 'startup' folder. Better idea(s)?

2) After said script runs I want it to go away. Ive tried telling it to 'unlink' itself but this doesn't make it too happy. Better idea(s)?

Replies are listed 'Best First'.
Re: delete 'self' after running
by marto (Cardinal) on Jun 13, 2007 at 14:56 UTC
    If you want this script to run each time the machine boots another way would be to add it to the Windows registry. The key in question would be HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run. If you want the job to run once only then add it to the HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnce key.

    Further info on the registry keys in question is here. To manipulate the Registry via Perl see Win32::TieRegistry

    Update: Added link to Microsoft docs and Win32::TieRegistry, and the Runonce registry key information

    Hope this helps

    Martin
Re: delete 'self' after running
by kyle (Abbot) on Jun 13, 2007 at 14:56 UTC

    Ive tried telling it to 'unlink' itself but this doesn't make it too happy.

    What is the nature of its unhappiness? What error message do you receive? What happens when you try that?

Re: delete 'self' after running
by sago (Scribe) on Jun 14, 2007 at 11:49 UTC

    Create a batch file, for example test.bat
    which has the below line,

    del C:\file.txt
    (if file.txt is the file which you want to be deleted.)

    Create a scheduled task to run this batch file "test.bat".
Re: delete 'self' after running
by xiaoyafeng (Deacon) on Jun 14, 2007 at 07:14 UTC
    For item 2, you might fork a new process and delete the script like below
    .... do something .... if (my $pid = fork()){ exit; } else { sleep 20; unlink file; exit; }

    I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction
      I haven't tested it but I am guessing this will not work on Win32. The reason is that fork on Win32 is emulated, meaning as far as the OS is concerned the original script is still running and cannot be unlinked.
Re: delete 'self' after running
by Anonymous Monk on Jun 14, 2007 at 06:37 UTC
    batch files