Jump to content

problem with comspec


Recommended Posts

Hi. I'm facing a problem with my code.

#NoTrayIcon
$var=Call("_IsWindowsVersion")

If $var=True Then ; If OS Version is above Windows XP
DllCall("kernel32.dll", "int", "Wow64DisableWow64FsRedirection", "int", 1)
RunWait(@ComSpec & " /c " & "schtasks /create /xml" & "Wise Memory Optimizer Task.xml" "/tn" "Wise Memory Optimizer Task")    
DllCall("kernel32.dll", "int", "Wow64EnableWow64FsRedirection", "int", 1)

Else ; If OS Version is Windows XP or lower
FileInstall('.\WinXP\Wise Memory Optimizer Task.job', @WindowsDir & '\Tasks\Wise Memory Optimizer Task.job')
EndIf

Func _IsWindowsVersion() ;If OS is newer than Windows XP
   $ver=RegRead('HKLM64\SOFTWARE\Microsoft\Windows NT\CurrentVersion\', 'CurrentVersion')
      If $ver="" Then
         $ver=RegRead('HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\', 'CurrentVersion')
      EndIf
Return $ver > 5.1
EndFunc

If I execute batch command

schtasks /create /xml "Wise Memory Optimizer Task.xml" /tn "Wise Memory Optimizer Task"

Everything works fine and a new task is created. But in my script, it doesn't work. I assume it's this line:

RunWait(@ComSpec & " /c " & "schtasks /create /xml" & "Wise Memory Optimizer Task.xml" "/tn" "Wise Memory Optimizer Task")

I've never been good at executing ComSpec. Usually I find ways to do what I need, but this time I'm sitting here for half a day and no luck.

 

I'd appreciate if someone could spot an error in my code. Thank you (and I'm on Windows 7).

Link to comment
Share on other sites

if you need quoted strings you need a way to add the quotes in addition to quoting the string

it needed more spaces

ConsoleWrite(@ComSpec & " /c " & "schtasks /create /xml " & $sQuote & "Wise Memory Optimizer Task.xml" & $sQuote & " /tn " & $sQuote & "Wise Memory Optimizer Task" & $sQuote)

RunWait(@ComSpec & " /c " & "schtasks /create /xml " & $sQuote & "Wise Memory Optimizer Task.xml" & $sQuote & " /tn " & $sQuote & "Wise Memory Optimizer Task" & $sQuote)

 

 

Link to comment
Share on other sites

This is how I did it before (also supplied WMOT.xml file with it)

RunWait(@ComSpec & " /c " & 'schtasks /create /xml "WMOT.xml" /tn "Wise Memory Optimizer Task"', "", @SW_HIDE)

But somewhere down the line I messed up. Maybe similar names aren't allowed, or i forgot to put XML with my script. I'll figure this out. But I appreciate all the help.

Link to comment
Share on other sites

7 minutes ago, Subz said:

RunWait(@ComSpec & ' /c schtasks /create /xml "Wise Memory Optimizer Task.xml" /tn "Wise Memory Optimizer Task"')

Well, it doesn't work for whatever reason. Also my guess with the names was wrong. If worst c0omes to worst, I'll just use my old, working script. I just wanted to clean it up a bit.

Link to comment
Share on other sites

Ok, this nis the final, cleaned up script. Everything else I tried didn't work. No idea why.

#NoTrayIcon
$var=Call("_IsWindowsVersion")

If $var=True Then ; If OS Version is above Windows XP
RunWait(@ComSpec & " /c " & 'schtasks /create /xml "WMOT.xml" /tn "Wise Memory Optimizer Task"', "", @SW_HIDE)
Else
FileCopy('WMOT.job', @WindowsDir & '\Tasks\Wise Memory Optimizer Task.job')
EndIf


Func _IsWindowsVersion() ;If OS is newer than Windows XP
   $ver=RegRead('HKLM64\SOFTWARE\Microsoft\Windows NT\CurrentVersion\', 'CurrentVersion')
      If $ver="" Then
         $ver=RegRead('HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\', 'CurrentVersion')
      EndIf
Return $ver > 5.1
EndFunc

 

Edited by supraspecies
Link to comment
Share on other sites

Check/Edit your file path:

 

#NoTrayIcon
#RequireAdmin

Global Const $WinVersion = _GetWinVer()
Global $IsWin64 = False, $IsNewOS = False
If StringInStr(@OSArch, "64") > 0 Then $IsWin64 = True
If $WinVersion > 5.2 Then $IsNewOS = True

If $IsWin64 Then _SysNative(0)

Global $TasksPath = @WindowsDir & '\Tasks\Wise Memory Optimizer Task.job'
Global $TasksCMD = 'schtasks /create /xml "' & 'WMOT.xml"' & ' /tn "' & 'Wise Memory Optimizer Task"'


If $IsNewOS Then ; If OS Version is above Windows XP
    _RunCmd($TasksCMD)
Else ; If OS Version is Windows XP or lower
    FileInstall('WinXP\Wise Memory Optimizer Task.job', $TasksPath)
    If @error Or (Not FileExists($TasksPath)) Then
        MsgBox(0, "", "Install file Error!")
    Else
        MsgBox(0, "", "Install  OK")
    EndIf
EndIf


#Region Function#
; * -----:|
Func _GetWinVer($sType = 0)
    Local $_WinVer = FileGetVersion(@SystemDir & "\ntoskrnl.exe")
    If @error Then $_WinVer = FileGetVersion(@SystemDir & "\winver.exe")
    $_WinVer = StringLeft($_WinVer, 3)
    If $sType Or $_WinVer < 1 Then
        Local Const $tagOSVERSIONINFO = 'struct;dword OSVersionInfoSize;dword MajorVersion;dword MinorVersion;dword BuildNumber;dword PlatformId;wchar CSDVersion[128];endstruct'
        Local $tOSVI = DllStructCreate($tagOSVERSIONINFO)
        DllStructSetData($tOSVI, 1, DllStructGetSize($tOSVI))
        Local $aRet = DllCall('kernel32.dll', 'bool', 'GetVersionExW', 'struct*', $tOSVI)
        If @error Or Not $aRet[0] Then Return SetError(@error, @extended, 0)
        $_WinVer = BitOR(BitShift(DllStructGetData($tOSVI, 2), -8), DllStructGetData($tOSVI, 3))
        If $sType = 2 Then Return $_WinVer
        Return BitAND(BitShift($_WinVer, 8), 0xFF) & '.' & BitAND($_WinVer, 0xFF)
    Else
        Return $_WinVer
    EndIf
EndFunc   ;==>_GetWinVer
; * -----:|
Func _SysNative($xEnableFsRedirection = 0)
    If $xEnableFsRedirection Then
        Local $iResult = DllCall("kernel32.dll", "int", "Wow64EnableWow64FsRedirection", "int", 1)
        Return SetError(@error, @extended, $iResult)
    Else
        Local $aResult = DllCall("kernel32.dll", "int", "Wow64DisableWow64FsRedirection", "int", 1)
        Return SetError(@error, @extended, $aResult)
    EndIf
EndFunc   ;==>_SysNative
; * -----:|
Func _RunCmd($sCommand)
    ConsoleWrite("+Execute: " & $sCommand & @CRLF)
    Local $sOutput = '', $iPID = Run('"' & @ComSpec & '" /c ' & $sCommand, '', @SW_HIDE, 0x6)
    Do
        $sOutput &= StdoutRead($iPID)
    Until @error
    Do
        $sOutput &= StderrRead($iPID)
    Until @error
    $sOutput = StringStripWS($sOutput, 7)
    ConsoleWrite("!" & $sOutput & @CRLF)
    ConsoleWrite("!--------------------------------------------------------------" & @CRLF)
    Return $sOutput
EndFunc   ;==>_RunCmd

#EndRegion Function#

 

GoodLucky

Regards,
 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...