perlwindows-server-2008-r2defragmentation

Can't call defrag from perl script on Windows 2008


I would like to run a perl script that runs the defrag command on windows. When I try to just use

system("defrag C:");

I get "'defrag' is not recognized as an internal or external command, operable program or batch file". I get the same error even if I give the full path of C:\Windows\System32\defrag.exe. Also, any test for the existence of defrag.exe fails. For example:

if(-f "C:\\Windows\\System32\\defrag.exe"),

if(-x "C:\\Windows\\System32\\defrag.exe"), and

if(-e "C:\\Windows\\System32\\defrag.exe")

all fail. In fact they fail when testing for any .exe file. This works fine on Windows 2003, does anyone know why it no longer works on Windows 2008?

Thanks!

Chris

Update: Backslashes weren't escaped originally. Fixed.


Solution

  • Newer versions of Windows (which may or may not include Windows Server 2008) virtualise certain system directories for executables that weren't built with a manifest instructing otherwise.

    Since 5.12.0, Perl is built with this instruction.

    perl.exe now includes a manifest resource to specify the trustInfo settings for Windows Vista and later. Without this setting Windows would treat perl.exe as a legacy application and apply various heuristics like redirecting access to protected file system areas (like the "Program Files" folder) to the users "VirtualStore" instead of generating a proper "permission denied" error.