1

I want to make my script run on startup automatically after converting it to EXE by pyinstaller tool , *once clicking on SPEED.EXE 'name of program' ,It copies itself to particular path on computer then makes a bat file in startup folder 'it contains code to start SPPED.EXE' but my problem that bat file doesnot run on start *

import os
import ftplib
import sys                   
import shutil                   
import getpass                
##################copy script into startup#######################
def copy_script():
    USER_NAME = getpass.getuser()
    src=sys.argv[0]
    dst = r'C:\Users\%s\AppData' % USER_NAME
    shutil.copy2(src,dst)
    dst='C:\Users\\"%s"\AppData\SPEED.exe' % USER_NAME                   ######name of script after making EXE
    add_to_startup(USER_NAME,file_path=dst)
    return None
######################################make a bat file to run on startup######
def add_to_startup(USER_NAME,file_path):
    if file_path == "":
        file_path = os.path.dirname(os.path.realpath(__file__))
    bat_path = r'C:\Users\%s\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup' % USER_NAME
    with open(bat_path + '\\' + "open.bat", "w") as bat_file:
        bat_file.write(r'@echo off'+ os.linesep)   ## to hide console batch file when it run
        bat_file.write(r'start "" %s' % file_path)

if __name__=='__main__':
    copy_script()
    start()     ##it is  function that i make it
  • Does it start, when you execute your bat-file manualy? You are missing an `r` in front of the string in line: `dst='C:\Users\\"%s"\AppData\SPEED.exe' % USER_NAME`. Furthermore, if your bat-file does not start: delete the `@echo off`-line and start it manualy in the console to get an stderr-print – Skandix Feb 05 '18 at 12:32
  • I write r but there is the same problem and when run batch file manually ,message says"fatal error failed to execute script SPEED" and the same problem when deleting the @echo off – mohamed elkordy Feb 05 '18 at 13:19
  • Any more details on your error? Could you may print(1,2,3, ...) between your lines to determine the line of code, where your programm is crashing? Furthermore, you should a add a check if you current location is allready the location in the Startup/Appdata-folder. – Skandix Feb 05 '18 at 13:35
  • I have not errors in debugging , this error only is shown when clicking on batch file manually – mohamed elkordy Feb 05 '18 at 13:43
  • Is there any more information than "fatal error failed to execute script SPEED"? You can also try to copy your executabel directly into the startup-folder, instead of refering to it with the bat-file. Finally, have a look at [this answer](https://stackoverflow.com/a/674654/4464653). – Skandix Feb 05 '18 at 14:00
  • thanks for your effort , I can solve the problem – mohamed elkordy Feb 06 '18 at 12:26

1 Answers1

1

thanks,I could solve my problem.

instead of creating a bat file in startup file " I delete add_to_startup method " ,I used registry method

    import winreg;
    key = winreg.OpenKey(winreg.HKEY_CURRENT_USER,
    r'SOFTWARE\Microsoft\Windows\CurrentVersion\Run', 0,
    winreg.KEY_SET_VALUE); winreg.SetValueEx(key, 'speed', 0,
    winreg.REG_SZ,'file_path'); # file_path is path of file after coping it

the error "fatal error failed to execute script SPEED " is result of the method shutil.copy2(src,dst) can't copy the same source to same destination at startup so I make exception

        try:
            shutil.copy2(src,dst)
        except:
                pass