No announcement yet.

Defragment system disks (Python)

  • Filter
  • Time
  • Show
Clear All
new posts

  • Defragment system disks (Python)

    You can trigger system defragment with below script:

    import subprocess;
    process=subprocess.Popen(('defrag /C'),shell=True,stdout=subprocess.PIPE);
    print stdout

    You can customize it to run over other drives as well.

    Example output would be:

    Click image for larger version

Name:	Screenshot_4.png
Views:	1
Size:	35.1 KB
ID:	4344

  • #2
    Hi Ilker

    I keep getting an error running this script
    'defrag' is not recognized as an internal or external command, operable program or batch file.


    • #3
      Let us check this out again. Could you please share which OS version you tried as well?


      • #4
        Hi Ilker

        I've tried this on Windows 10, Server 2008/2012


        • #5
          It looks like problem on X64 systems rather than specific OS. We are looking what can be done to resolve the issue.


          • #6
            Hi Ilker

            Ok thanx


            • #7
              Can I ask where the output appears? Does it appear in report to admin or on-screen to user or into a sys or other log file? Thanks.


              • #8
                Hello azon2111

                The results are shown here: Device List -> Click on the device -> go to Procedure tab -> you will see an entry there for the procedure that you ran. Click on the Details option to show the results.


                • #9
                  Thank you.


                  • #10
                    I got this as well on a test computer, is there a script work around or is there an issue with running some scripts on x64 computers (which I am pretty sure is all the ones I look after)


                    ok so I tried a few different things and got the same result

                    process=subprocess.Popen(('C:\Windows\System32\def rag.exe /C'),shell=True,stdout=subprocess.PIPE);

                    'C:\Windows\System32\defrag.exe' is not recognized as an internal or external command, operable program or batch file. Do I assume this is a bug then and that this will just not work or should something else be changed in the script?
                    Last edited by dougaust; 08-29-2016, 04:16 AM.


                    • #11

                      Its a windows 64bit problem if you run the defrag command from within python it expects the defrag.exe file to be in the c:\windows\syswow64 folder for 64bit systems if this script can be updated to automatically copy the defrag.exe from c:\windows\system32 to c:\windows\syswow64 before running it should work or alternatively copy the defrag.exe file to the same folder path as the python executable before running.


                      • #12
                        Or one could add a conditional statement to the script to check for the file in system32, if it doesn't exist, it would look in the syswow64, or better yet, since most systems are now 64 bit do it the other way around.


                        • #13
                          Hi BOSS

                          you would think that would be a solution but even if you stipulate the file must be run from c:\windows\system32 it somehow expects it in the c:\windows\syswow64 folder and it is not in their by default even on 64bit systems I think its a Python/windows 64 issue but i decided to use a 3rd party Defrag utility that i run using the (download/install/run python script) in the Script library


                          • #14
                            Hi all,

                            We have another Script that can run the Defrag on both 32 and 64 bit.



                            • #15
                              Hi Ilker

                              Thank you for the update but we now use Auslogics Disk Defrag they have a command line utility cdefrag.exe that works really well we made our own script that downloads and runs it from our own web host. it also gives a bit of detail instead of just pass/fail and does a really good job of an optimized defrag. here is a link to their parameters if anyone is interested