Creating a scheduled task to run minimised or hidden

Discussion in 'Software' started by davidm_uk, Mar 7, 2015.

  1. davidm_uk

    davidm_uk Private E-2

    I've created a batch file "TestCopy.bat" to copy some files from my hard drive to a NAS drive:

    @echo off
    REM Copies Membership nnnn.exe to NAS drive
    ROBOCOPY "D:\_Data\1 U3A\0 Membership Database" "\\EDMINI\share\David\From T-PC" "membership*.*" /M /NJS >nul
    exit

    and then created a shortcut to the batch file, "TestCopy" with it's properties set to run as minimised so that I don't see the comman window flash up on the screen. All good so far.

    Now I've added a job to Win7 Task Scheduler, by setting the Action to Start a program, with the program and defined as cmd with it's argument as

    /c start "" "D:\TestCopy"

    with everything else in Task Scheduler set up to run it once a day.

    It runs OK, and the file(s) get copied, but I now get the command window flash up on the screen rather than run minimised.

    Any idea how I can force it to run minimised (or hidden), I can't see a cmd / option to do this?
     
  2. GermanOne

    GermanOne Guest

    Run the script under the SYSTEM account instead (first tab in the task properties). This way the cmd window won't be visible for the user.
     
  3. davidm_uk

    davidm_uk Private E-2

    Tried that, and it doesn't run* at all, even if I manually trigger it. within the Task Scheduler.

    * ie the files don't get copied and the archive bit isn't unset on the source files.
     
  4. GermanOne

    GermanOne Guest

    Try to run the batch file itself (not its shortcut) or run "D:\TestCopy.lnk" because .LNK is the (invisible) file extension for shortcuts.
     
  5. davidm_uk

    davidm_uk Private E-2

    Nope, still not running, either with .bat or .LNK (or .lnk).

    All variants (.bat, .lnk, or just "D:\TestCopy") work ok when run under my name (both timed and manually triggered), but not under SYSTEM, with all other settings unchanged.
     
  6. GermanOne

    GermanOne Guest

    Maybe environment variables are not yet set if you run under SYSTEM.
    Change the program name from just cmd to C:\Windows\System32\cmd.exe
    Also use C:\Windows\System32\robocopy.exe in your batch script. Don't leave out any file extensions such as .exe or .bat.
     
  7. davidm_uk

    davidm_uk Private E-2

    I've deleted the old job and created a new one under my own account (which is an admin account) with those changes (full paths specified).

    The job runs fine, both on schedule and when run manually from with the Task Scheduler (except the blank window still flashes up on the screen).

    Exported the job, created a copy of the xml file with a different name, imported it back into Task Scheduler, and modified it to run under SYSTEM (no other chagnes). Disabled the old job.

    The new one doesn't run, either via schedule or manually.

    I wonder if it's possible to put the whole ROBO command into the scheduler:

    C:\Windows\System32\robocopy.exe "D:\_Data\1 U3A\0 Membership Database" "\\EDMINI\share\David\From T-PC" "membership*.*" /M /NJS >nul

    and not use a batch file? Still need to resolve the flashing window though.
     
  8. GermanOne

    GermanOne Guest

    I don't understand why it doesn't work for you.
    Here an XML export from a task that works for me (already customized for your pathes).
    Code:
    <?xml version="1.0" encoding="UTF-16"?>
    <Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
      <RegistrationInfo>
        <Date>2015-03-07T20:58:48.7735104</Date>
        <Author>GermanOne, MajorGeeks.com</Author>
        <Description>copy to server share</Description>
      </RegistrationInfo>
      <Triggers>
        <CalendarTrigger>
          <StartBoundary>2015-03-07T20:00:00</StartBoundary>
          <Enabled>true</Enabled>
          <ScheduleByDay>
            <DaysInterval>1</DaysInterval>
          </ScheduleByDay>
        </CalendarTrigger>
      </Triggers>
      <Principals>
        <Principal id="Author">
          <UserId>S-1-5-18</UserId>
          <RunLevel>LeastPrivilege</RunLevel>
        </Principal>
      </Principals>
      <Settings>
        <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
        <DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
        <StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>
        <AllowHardTerminate>true</AllowHardTerminate>
        <StartWhenAvailable>false</StartWhenAvailable>
        <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
        <IdleSettings>
          <StopOnIdleEnd>false</StopOnIdleEnd>
          <RestartOnIdle>false</RestartOnIdle>
        </IdleSettings>
        <AllowStartOnDemand>true</AllowStartOnDemand>
        <Enabled>true</Enabled>
        <Hidden>false</Hidden>
        <RunOnlyIfIdle>false</RunOnlyIfIdle>
        <WakeToRun>false</WakeToRun>
        <ExecutionTimeLimit>P3D</ExecutionTimeLimit>
        <Priority>5</Priority>
      </Settings>
      <Actions Context="Author">
        <Exec>
          <Command>C:\Windows\System32\robocopy.exe</Command>
          <Arguments>"D:\_Data\1 U3A\0 Membership Database" "\\EDMINI\share\David\From T-PC" "membership*.*" /M /NJS</Arguments>
        </Exec>
      </Actions>
    </Task>
    
    Please use the Notepad Editor for saving the file and don't forget to choose "Unicode" for the encoding (in the "Save as" dialogue) before.
     
  9. davidm_uk

    davidm_uk Private E-2

    Well I've imported your xml and it still doesn't work when run manually. I changed the schedule to run every 10 minutes, then left it. The scheduler timings show that it has run when expected, but no files have been copied.

    If I run the full ROBOCOPY command from the Open box from the RUN start menu item it copies the file ok, so nothing wrong with the command itself and it also works ok if I modify your sheduled task to run under my login id.

    It just doesn't like running as SYSTEM for some reason.

    I've given up with this approach and have now set up a SyncBackSE scheduled job to copy the files - that's working ok, and it doesn't display anything on the main screen, just a brief icon in the system tray. Not quite as efficient as a simple ROBOCOPY command, but I'll live with that.

    Thanks for your help anyway.
     
  10. Eldon

    Eldon Major Geek Extraordinaire

    If your only real problem is the flashing window, you must hide the task. Look at the bottom left of the snapshot.
     

    Attached Files:

  11. GermanOne

    GermanOne Guest

    @Eldon
    That would probably work provided that you also checked "Run whether user is logged on or not".

    @davidm_uk
    Glad to hear that you finally found a solution.

    One additional approach from my side:
    The Run method for Windows Scripting supports runing a program in hidden mode. The problem is that Batch doesn't belong to those languages. You need to write a hybrid script (e.g. Batch and JScript).

    D:\TestCopy.bat
    Code:
    @if (@a)==(@b) @end /* (harmless hybrid line to begin a JScript comment block)
    
    :: Batch commands
    @echo off
    C:\Windows\System32\robocopy.exe "D:\_Data\1 U3A\0 Membership Database" "\\EDMINI\share\David\From T-PC" "membership*.*" /M /NJS
    exit
    
    :: JScript Commands (end of JScript comment) */
    WScript.CreateObject('WScript.Shell').Run('\"' + WScript.ScriptFullName + '\"', 0);
    
    Scheduled Task
    Program: C:\Windows\System32\wscript.exe
    Arguments: //e:jscript "D:\TestCopy.bat"
     
  12. davidm_uk

    davidm_uk Private E-2

    Thanks GermanOne and Eldon - I'll save those posts for future reference!
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds