Page 1 of 1

PYTHON Copy a shortcut even if it already exists

Published: March 28, 2019 - 08:47
by skoizer
Good morning,
In an Open Office package, I need to copy shortcuts to the desktop for all users here: C:\Users\Public\Desktop\Software\

Code: Select all

    if os.path.isfile('C:\ProgramData\Microsoft\Windows\Start Menu\Programs\OpenOffice 4.1.5\OpenOffice Calc.lnk'):
        remove_file('C:\Users\Public\Desktop\Logiciels\OpenOffice Calc.lnk')
        shutil.move('C:\ProgramData\Microsoft\Windows\Start Menu\Programs\OpenOffice 4.1.5\OpenOffice Calc.lnk','C:\Users\Public\Desktop\Logiciels')
        print('OpenOffice Calc.lnk deplace')
If the file already exists, it displays an error message.

To replace this long line of code, is there a simpler way in Python?
Copy even if the file already exists. I haven't found any help on search engines or the forum


Installed WAPT version (1.5.1.26)
- Server OS (Linux) and version (CentOS 7 - )
- Operating system of the administration/package creation machine (Windows 7 / 10)

Re: PYTHON Copying a shortcut even if it already exists

Published: May 6, 2019 - 12:11
by gaelds
Perhaps try this:

Code: Select all

shutil.move('C:\ProgramData\Microsoft\Windows\Start Menu\Programs\OpenOffice 4.1.5\OpenOffice Calc.lnk','C:\Users\Public\Desktop\Logiciels\OpenOffice Calc.lnk'
Alternatively, I also create shortcuts to all my software on "C:\users\Public\Desktop\Software\" but I prefer to recreate them rather than copy them, for example:

Code: Select all

         if iswin64():
            prog_dir = programfiles64
        else:
            prog_dir = programfiles32

        if not isdir(makepath(common_desktop(),'Logiciels','Bureautique')):
            mkdirs(makepath(common_desktop(),'Logiciels','Bureautique'))
        create_shortcut(makepath(common_desktop(),'Logiciels','Bureautique','LibreOffice.lnk'),target=r'%s\LibreOffice 5\program\soffice.exe' %prog_dir, wDir=r'%s\LibreOffice 5\program\\' %prog_dir,icon=r'%s\LibreOffice 5\program\soffice.exe' %prog_dir)
        create_shortcut(makepath(common_desktop(),'Logiciels','Bureautique','LibreOffice Base.lnk'),target=r'%s\LibreOffice 5\program\sbase.exe' %prog_dir, wDir=r'%s\LibreOffice 5\program\\' %prog_dir,icon=r'%s\LibreOffice 5\program\sbase.exe' %prog_dir)
        create_shortcut(makepath(common_desktop(),'Logiciels','Bureautique','LibreOffice Calc.lnk'),target=r'%s\LibreOffice 5\program\scalc.exe' %prog_dir, wDir=r'%s\LibreOffice 5\program\\' %prog_dir,icon=r'%s\LibreOffice 5\program\scalc.exe' %prog_dir)
        create_shortcut(makepath(common_desktop(),'Logiciels','Bureautique','LibreOffice Draw.lnk'),target=r'%s\LibreOffice 5\program\sdraw.exe' %prog_dir, wDir=r'%s\LibreOffice 5\program\\' %prog_dir,icon=r'%s\LibreOffice 5\program\sdraw.exe' %prog_dir)
        create_shortcut(makepath(common_desktop(),'Logiciels','Bureautique','LibreOffice Impress.lnk'),target=r'%s\LibreOffice 5\program\simpress.exe' %prog_dir, wDir=r'%s\LibreOffice 5\program\\' %prog_dir,icon=r'%s\LibreOffice 5\program\simpress.exe' %programfiles)
        create_shortcut(makepath(common_desktop(),'Logiciels','Bureautique','LibreOffice Math.lnk'),target=r'%s\LibreOffice 5\program\smath.exe' %prog_dir, wDir=r'%s\LibreOffice 5\program\\' %prog_dir,icon=r'%s\LibreOffice 5\program\smath.exe' %prog_dir)
        create_shortcut(makepath(common_desktop(),'Logiciels','Bureautique','LibreOffice Writer.lnk'),target=r'%s\LibreOffice 5\program\swriter.exe' %prog_dir, wDir=r'%s\LibreOffice 5\program\\' %prog_dir,icon=r'%s\LibreOffice 5\program\swriter.exe' %prog_dir)
        create_shortcut(makepath(common_desktop(),'Logiciels','Bureautique','LibreOffice Web.lnk'),target=r'%s\LibreOffice 5\program\sweb.exe' %prog_dir, wDir=r'%s\LibreOffice 5\program\\' %prog_dir,icon=r'%s\LibreOffice 5\program\sweb.exe' %prog_dir)
        remove_desktop_shortcut('LibreOffice')