Conditional Statements inside Batch

Posted on 2008-11-18
Last Modified: 2012-05-05
I need to create some statements inside a batch file that is similiar to the following:

Set Jobname = %1

If the jobname contains "Weekly" and today is Sunday then

set WaitFile=E:\Console\Nightly_Finished_TodaysDate.txt
Call GenerateFile

If the jobname contains "Static" and today is Sunday then

set WaitFile=E:\Console\Weekly_Finished_TodaysDate.txt
Call GenerateFile

If the jobname contains "Nightly" then
set WaitFile=E:\Console\ALog_Finished_TodayDate.txt
Call GenerateFile

If the jobname contains "Validate" and today is Saturday then

set WaitFile=E:\Console\Nightly_Finished_TodayDate.txt

Call GenerateFile


@echo off

sleep 1
if not exist "%WaitFile%" goto loop
REM Additional code below here ..

Can anyone assist or offer a better way to handle?
Question by:earngreen
    LVL 67

    Expert Comment

    opposed to a vbscript solution?
    It's possible in a batch file to grab the day's name, but cumbersome...

    Author Comment

    Not oposed to vbscript
    LVL 67

    Accepted Solution

    Not sure what your script does beyond this, but this would be the start of it...let me know if you'd like the rest of the batch included here...
    Jobname = lcase(wscript.arguments(0))
    Today = lcase(WeekdayName(Weekday(Date))
    TodayDate = Year(Date) & Month(Date) & Day(Date)
    WaitFolder = "E:\Console\"
    If Jobname = "" Then wscript.quit
    Dim objShell : Set objShell = CreateObject("Wscript.Shell")
    Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
    If Instr(Jobname, "weekly") > 0 And Today="sunday" Then
    ElseIf Instr(Jobname,"static") > 0 And Today="sunday" Then
    ElseIf Instr(Jobname,"nightly") > 0 Then
    ElseIf Instr(Jobname,"validate") > 0 And Today="saturday" Then
    End If
    WaitFile = WaitFolder & Replace(WaitFile, "TodayDate", TodayDate)
    wscript.sleep 1000
    If Not objFSO.FileExists(WaitFile) Then
     'do something...
    End If

    Open in new window


    Author Comment

    The rest of the batch is just inserting a record into a database similiar to this
    Set CN = CreateObject("ADODB.Connection")

    CNString = "Driver={SQL Server}; Server=; Database=; UID=; PWD="
    CN.ConnectionString = CNString
    StrsqlQry = "INSERT INTO Job(Jobname, 'TodaysDate(dd/mm/yy hh:mm:ss)', 'Started','') values(StartTime,JobName,Status,EndTime)"
    cn.execute StrsqlQry

    The only question that I have with your code(Jobname = lcase(wscript.arguments(0))
    ) accept a parameter in the same manner as %1 for batch.

    The last thiing is there a way to do a check with the sleep function that this fails after n hours of checking for the file

    LVL 67

    Expert Comment

    Yes, it takes the first argument (similar to %1) and converts it to lowercase for comparison.

    For the checking, you could do something like:
    (will basically wait for 30 minutes)

    For x = 0 to 120
      wscript.sleep 15000  'sleep 15 seconds before retesting
      If objFSO.FileExists(WaitFile) Then Exit For


    Author Comment

    This does not seem to want to call the argument. When i run from command line it just does nothing.

    Author Comment

    Figured it out. Thanks for the help.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
    Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now