windows 8 - Batch file that run on a special day

07
2014-07
  • Martin

    How do I create a batch file that once clicked on, starts an .exe file if it is a Sunday?

    I do not want it to run the .exe file if it is on another day. If for example it is Friday I just want it to close it self and do nothing.

    Also it would be nice if it was possible to run it in the background without showing any kind of command prompt?

  • Answers
  • Saurabh Sharma

    You can use Task Scheduler for this purpose. Open Task Scheduler > Create Task You can now define the properties, conditions for running the task, and what job/application to run, in the opened dialog.

    Task Scheduler

  • user2196728

    Tha fact is that you want to use an advanced feature of date command (Day Of Week) that Windows is unable to manage in DOS Batch.

    For this, i would use a third party binary file date.exe, that comes from Unix world and that works on Windows : UnxUtils (that is a collection of common GNU Unix utilities that runs on Windows).

    Download it from here (UnxUtils.zip).

    Then extract it, and delete all files except date.exe

    Then, write a batch file like this (assuming date.exe is located on c:\) :

    @ECHO OFF
    REM Sunday is 0
    for /f "delims=" %%a in ('c:\date.exe +%%w') do set DayOfWeek=%%a
    if %DayOfWeek% == 0 (
      cmd /c c:\path\to\exe\file.exe
    ) else (
      echo Do Nothing
    )
    

    You can put it in the Windows startup folder and will work on all Windows version, regardless where you are in the world.


    Also (much better), in those days, DOS Batch are a bit obsolete. As you are talking about Windows 8 i would consider Powershell (can be called by a batch file if needed) :

    $a = Get-Date
    if($a.DayOfWeek -eq "Sunday") {
        cmd /c c:\path\to\exe\file.exe
    }
    

    Here, no need of third party binary file : Powershell is Powerfull ;)

  • joejoe31b

    According to HERE, it is impossible to execute a batch file with the built in Command Prompt, and not show a window.

    So, eventually, I landed upon THIS page, and used the information to make a batch file that will do what you want it to. What the batch script will do, is check the time. If it is not the correct day, the Command Prompt will flash red, and exit. If it is the correct day, it will flash green, and launch your program, while exiting.

    All of the stuff in the above paragraph happens in less than a second (depending on your computers speed), and therefore won't be very noticeable.

    @echo off
    
    mode con: cols=16 lines=6
    
    :: The file you want to launch.
    set FILE=PATH_TO_FILE
    
    :: The day you want to launch the file on.
    set DAY=Sun
    
    for /F "tokens=1 delims= " %%A in ('Date /t') do ( Set TODAY=%%A )
    if %TODAY% == %DAY% goto START
    
    :STOP
    color 47
    goto END
    
    :START
    color 27
    
    start "" "%FILE%"
    goto END
    
    :END
    exit
    

    You will need to change two things in the batch file, to make sure it fits your needs.

    1. First, you will need to change PATH_TO_FILE on line 7 to the path of the executable you want to launch.
    2. Secondly, you may need to change Sun on line 10. This may need to be changed to the first 3 letters of the day you want to launch the executable on (if you don't want to launch it on Sunday).

  • Related Question

    windows - Run a completly hidden batch file
  • Moayad Mardini

    I'm looking for some way to run a batch file (.bat) without anything visible to the user (no window, no taskbar name, .etc..).

    I don't want to use some program to do that, I'm looking for something cleaner. I've found a solution that uses VBScript, but I don't really like using VBS, either.


  • Related Answers
  • Vlastimil Ovčáčík

    Solution 1:

    Save this one line of text as file invisible.vbs:

    CreateObject("Wscript.Shell").Run """" & WScript.Arguments(0) & """", 0, False

    To run any program or batch file invisibly, use it like this:

    wscript.exe "C:\Wherever\invisible.vbs" "C:\Some Other Place\MyBatchFile.bat"

    To also be able to pass-on/relay a list of arguments use only two double quotes

    CreateObject("Wscript.Shell").Run "" & WScript.Arguments(0) & "", 0, False

    Example: Invisible.vbs "Kill.vbs ME.exe"

    Solution 2:

    Use a command line tool to silently launch a process : Quiet, hidecon or hideexec.

  • joe

    use Cmdow is a Win32 commandline utility for NT4/2000/XP/2003 that allows windows to be listed, moved, resized, renamed, hidden/unhidden, disabled/enabled, minimized, maximized, restored, activated/inactivated, closed, killed and more.

    Run a batch file hidden passing it parameters:-

    cmdow /run /hid mybat arg1 "arg 2"

  • Paul Cheselske

    To Hide batch files or command files or any files.... Use Windows XP built in Iexpress.exe utility to build a .EXE out of the batch file. When using Iexpress make sure you check the run hidden option and check mark all the boxes about not showing anything. After you create your .exe place it in whatever run command folder you choose and you will never see it come up.

  • 8088

    Seems that somebody made a list of the 7-8 ways to run 'silent':

    http://forums.techguy.org/dos-other/644932-solved-howto-run-batch-file.html

  • John T

    Run the script via an at job without making it interactive:

    at 11:00 script.bat
    


    Another solution, if you don't mind installing something like Python, you could simply create a script and run it with pythonw (the linked version for GUI operations). Since you aren't using any graphical APIs, the window will not show. Simply use calls to os.system() and it will mimic a batch script, as it is the same as typing the strings into the command line.

    Example:

    import os
    
    os.system("tasklist > C:\tasks.txt")
    os.system("ipconfig /all > C:\netinfo.log")
    
  • Jack S

    If your Batch File can fit into one line and so many characters, then just use a Shortcut by right clicking inside a folder put in a random directory, and skip through the rest of the wizard, and lastly right click on the shortcut just created and click properties and under target, input what you would usually put in a bat file. And Bobs your uncle!

    Pros:

     No Command Window
     Ability to use an icon
     Same Functionality of bat file
    

    Cons:

     Limited to one line, so many characters
    
  • Brad Gilbert

    You can run it minimized easily.

    start /MIN batch.cmd
    
  • djangofan

    Using Windows7, you can use the "Scheduled Task" control panel to schedule a batch file to run in the background invisibly with specified permissions and even as the "SYSTEM" account. See screenshot below:

    enter image description here

    Furthermore, although with not as many capabilities, you can also create a invisible batch script task like this from the command line using the "AT.exe" command without the '/interactive' option.

  • BijaN-R

    I don't like VBScript solution.

    Download and copy nircmd.exe to your %systemroot%\system32 folder, then add this command to first line of your batch:

    nircmd.exe win hide ititle "cmd.exe"
    

    or make your batch title custom first with title command to avoid from hiding all cmd windows, like this:

    title MyBatch
    nircmd.exe win hide ititle "MyBatch"
    
  • BloodPhilia
    cmdow /run /hid "file.extention" arg1 "arg 2"
    
  • sgmoore

    You do not need to do any special.

    If you are running the batch files from a windows service, they should be hidden by default unless you enable the "Allow service to interact with desktop" option on the service.

  • npocmaka

    I'm experimenting with SCHTASKS. The idea is that if schedule a task as SYSTEM user the process will be started as hidden.And if you delete a scheduled task the process will be not stopped.Here's more info : http://ss64.org/viewtopic.php?id=1539

  • djangofan

    I started trying to figure this out and never really succeeded. This was as close as I got:

    @echo off
    if not defined PIL (
        set PIL=1
        start /min "" %~0
        exit /b
    )
    TITLE Hello World
    echo Hello world!
    pause>nul
    
  • Alan Panayotov

    I had the same problem, but figured it out:
    Shell("filehere" & "parametersforbatchfilehere", vbHidden)