?
Solved

Conditional Statements inside Batch

Posted on 2008-11-18
7
Medium Priority
?
423 Views
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




:GenerateFile

@echo off

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



Can anyone assist or offer a better way to handle?
0
Comment
Question by:earngreen
  • 4
  • 3
7 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 22990016
opposed to a vbscript solution?
It's possible in a batch file to grab the day's name, but cumbersome...
0
 

Author Comment

by:earngreen
ID: 22994331
Not oposed to vbscript
0
 
LVL 67

Accepted Solution

by:
sirbounty earned 2000 total points
ID: 22994422
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
  WaitFile="Nightly_Finished_TodaysDate.txt"
ElseIf Instr(Jobname,"static") > 0 And Today="sunday" Then
  WaitFile="Weekly_Finished_TodaysDate.txt"
ElseIf Instr(Jobname,"nightly") > 0 Then
  WaitFile=""ALog_Finished_TodayDate.txt"
ElseIf Instr(Jobname,"validate") > 0 And Today="saturday" Then
  WaitFile="Nightly_Finished_TodayDate.txt"
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

0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:earngreen
ID: 22995888
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
CN.Open
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


0
 
LVL 67

Expert Comment

by:sirbounty
ID: 22995971
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
Next

0
 

Author Comment

by:earngreen
ID: 23043554
This does not seem to want to call the argument. When i run from command line it just does nothing.
0
 

Author Comment

by:earngreen
ID: 23043981
Figured it out. Thanks for the help.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 …
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Integration Management Part 2
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month14 days, 3 hours left to enroll

807 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