Link to home
Start Free TrialLog in
Avatar of John Ellis
John Ellis

asked on

Batch File to Prevent Launch of Another File Based on Date

Hello:

I have a *.dts file in a directory on the C drive that, when run either manually or automatically, imports data from a source file on the C drive into an application.

I want to create a batch file (*.bat file) that (a) determines if the date of the source file is older than today's date and (b) "tells" the *.dts file not to process the source file (or simply tell the *.dts file not to launch).

I doubt that such a *.bat file would be complicated to create.  But, I cannot find anything online that tells me how to create a *.bat file to conduct these two activities.

Can someone help me, please?

Thank you!

John
Avatar of John Ellis
John Ellis

ASKER

Would the following work?

FORFILES /P C:\scripts\input\source.txt /D = getdate() /C "CMD /C start C:\import.dts"

In the above batch file, I'm trying to say "review the source.txt file located in the C:\scripts\input directory and, if the date of the file is equal to today's date, then run the import.dts file located on the C drive.

John
ASKER CERTIFIED SOLUTION
Avatar of NVIT
NVIT
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you, NVIT!

On the last REM statement of line #13, what code (syntax) do I use?

John
>  On the last REM statement of line #13, what code (syntax) do I use?

Originally, you mention... I have a *.dts file in a directory on the C drive that, when run either manually or automatically, imports data from a source file on the C drive into an application.

You would put the code that imports data from a source file on the C drive into an application.

I have no idea about this .dts file or related application. ;-)
Hi NVIT:

Let's say that the file is simply named C:\filename.dts.  What would the syntax be?  I just want to launch the *.dts file.

John
> Let's say that the file is simply named C:\filename.dts.
REM Put your code to process the .dts code here...
C:\filename.dts

Open in new window


Since .dts is not, by default, a Windows executable file, this assumes your OS has associated the file type .dts with the appropriate program
Okay.  Thanks, again, NVIT.

I'm going to try this, as soon as I can.  I'll let you know how it goes.

Again, thanks for getting back to me and for getting back to me so quickly!  :)

John
Hi NVIT:

You used the following syntax, toward the end of line #11:  @fdate

Is that correct, or was that a typo?  I'm having trouble finding any information, on this syntax, online.

John
Hi:

Please disregard my question.  I found a reference to @fdate, online, just now.  :)

I'll let you know how this goes.

John
Hi NVIT:

This may have worked.  But, I will have to do another test tomorrow (Wednesday) to be sure.

Indeed, the .bat file did block the older source file from being processed and the job reported as failed.  That's good.

But, when I put in a source file dated today, the new source file was not processed either.

That may, however, have been because I did not turn on the application's "Auto Recovery" routine which has the application try to essentially re-do a job, after a failure, rather than keeping it in a failed state.

In the meantime, is -1 a good indicator?  I presume that this means files older than today's date.  Correct?

Thanks, again!  I'll let you know, as I continue to test.

John
> ... is -1 a good indicator?  I presume that this means files older than today's date.
Yes

I tested it here against various dated files and it worked fine.
Hi NVIT:

Actually, if possible, I need further help on this.

You programming works.  But, when I place it in the "pre-job section" of the Scribe interface of the job, the job doesn't run when the date stamp of the file is prior to today's date.  That's good, and that's what I wanted.

But, the Scribe job goes into a failed state and keeps trying to run the job until a source file matching today's date is put in place for the job.

I realize that this is an issue with the application and not your programming.  So, I could be getting "outside" your area.  But, I thought that I would ask if you have any thoughts on this.

Thanks, NVIT!  Your help has been great!

John
I don't know what the "pre-job section" step sees as a fail vs. success code. With that, try this revised code. I added a ExitSuccessCode line. Normally, 0 means success, which is what I have. See if that works. If not, try a different number... maybe 1

@echo off
setlocal enabledelayedexpansion

set ExitSuccessCode=0

REM if the date of the source file is older than today's date it says so
REM Else, it processes the .dts file
REM Change DaysOld, SourceDir and SourceFile values to your needs
set DaysOld=-1
set SourceDir=.
set SourceFile=filename.dts

forfiles /p "%SourceDir%" /m "%SourceFile%" /d %DaysOld% /c "cmd /c echo "%SourceFile%" @fdate is older than today. Quitting...&goto :NoProcessing"

REM Put your code to process the .dts code here...
REM Process .dts line 1
REM Process .dts line 2
exit %ExitSuccessCode%

:NoProcessing
exit %ExitSuccessCode%

Open in new window

Actually, I don't know if that revised code will even work because you say...

> ...the job doesn't run when the date stamp of the file is prior to today's date.

If you mean my code never even runs, then you're out of luck. Unless, at the Scribe level, there is a way to act on that result.