1.

Solve : Read from file named after the next day in the week?

Answer»

I'm stuck trying to do something in a batch file based on the day of the week. Here's some of the code:

Code: [Select]SET /P M=
:
IF %M%==1 (
SET M=1.Monday
GOTO :MONDAY
)
IF %M%==2 (
SET M=2.Tuesday
GOTO :TUESDAY
)
IF %M%==3 (
SET M=3.Wednesday
GOTO :WEDNESDAY
)
IF %M%==4 (
SET M=4.Thursday
GOTO :THURSDAY
)
IF %M%==5 (
SET M=5.Friday
GOTO :FRIDAY
)
IF %M%==6 (
SET M=Special
GOTO :SPECIAL
)
IF %M%==Q GOTO :QUIT_ADMIN
IF %M%==q GOTO :QUIT_ADMIN
:

:MONDAY
CLS
IF EXIST logs\templog.txt DEL /F /Q logs\templog.txt
For /F "Tokens=*" %%I In[b] (Tuesday.txt)[/b] Do (
C:\psexec \\<server> %W% %%I /passw >>logs\templog.txt
)
At the top of the script I set M=whatever day of the week the user inputs (1-5 corresponds to Mon-Fri). On Monday the batch file should read from a document named "Tuesday.txt" Instead of referring directly to this list (Tuesday.txt) I'd like to refer to it as a dynamic variable like "Tomorrow".

"Tomorrow" should equal %M%+1. How can I do this? This will eliminate the need to repeat the code for every day of the week which I think is a lot cleaner. Thanks for the help.

Thanks,

MJCode: [Select] set /a Tomorrow=%M%+1 Quote from: Lemonilla on July 25, 2012, 03:25:40 PM

Code: [Select] set /a Tomorrow=%M%+1
M is equal to 1.Monday.
So you end up with your code failing.
Code: [Select]C:\Users\Squash>set M=1.Monday

C:\Users\Squash>set /a tomorrow=%M% + 1
Missing operator.
But you could substring the variable
Code: [Select]C:\Users\Squash>set /a tomorrow=%M:~0,1% + 1
2What happens at :SPECIAL?

Also, you can replace these 2 lines

IF %M%==Q GOTO :QUIT_ADMIN
IF %M%==q GOTO :QUIT_ADMIN

with this one line

IF /I %M%==Q GOTO :QUIT_ADMIN

Salmon Trout - Special is used when the standard daily list changes. Rather than append the changes to Tuesday.txt (for example) a separate list is made. Thanks for the tip about Quit. I'll apply that for sure. I have several other scripts where I account for both upper and lower case entries from the user so this is nice to know.

Squashman - Thank you for your help. I didn't realize that what I asked for %M%+1 could work. Maybe it didn't. I added

SET /A Tomorrow=%M% + 1 and "2" was returned
SET /A Tomorrow=M + 1 and "2" was returned
SET /A Tomorrow=%M:~0,1% + 1, "2" was returned

Since my lists are named after the day of the week is it possible for %M% + 1 to equal "Tuesday" instead of "2"?

Thanks again,

MJ
Not really understanding what you are trying to do.
Code: [Select]SET /P M=

IF %M%==1 (
SET TOMORROW=TUESDAY
GOTO :MONDAY
)
IF %M%==2 (
SET TOMORROW=WEDNESDAY
GOTO :TUESDAY
)
IF %M%==3 (
SET TOMORROW=THURSDAY
GOTO :WEDNESDAY
)
IF %M%==4 (
SET TOMORROW=FRIDAY
GOTO :THURSDAY
)
IF %M%==5 (
SET TOMORROW=SATURDAY
GOTO :FRIDAY
)
IF %M%==6 (
SET M=Special
GOTO :SPECIAL
)
IF %M%==Q GOTO :QUIT_ADMIN
IF %M%==q GOTO :QUIT_ADMINIt could probably be streamlined if we knew the purpose and code.

I'm guessing that the code for the actual function is repeated 7 times with just a name changed for the file each daySquashman - thanks again. It appears that I was mentally over-complicating things and I'm not much of a script writer. SET TOMORROW= was what I was looking for. It allowed me to tidy up one script and cut 3/4 of another one out.

Every night I have to drain servers in two different farms. The script determines the day of the week and then references preconfigured lists named after the days of the week to run wlbs commands against. For example: on Monday the script determines that it's Monday and loads the servers from the Tuesday.txt list, emails the proper parties the list of servers that scheduled to be drained, drains them, creates a log, emails me the log and cleans up the temporary txt files it creates in the process.

Perhaps you can help me clean up another segment of the script:

Code: [Select]FINDSTR /V "WLBS" logs\templog.txt >>logs\format.txt
FOR /F "Tokens=* Delims= " %%A in (logs\format.txt) do (
SET /a N+=1
echo ^%%A^ >>logs\RebootLog.txt
This part of the script searches templog.txt and outputs every line that doesn't start with WLBS to format.txt. Blat is being used for emailing the logs. When Blat reads RebootLog.txt and copies the contents into the body of an email it isn't easily read. So I use the next lines of code to read the contents of format.txt and add a space between each line so the body of the email that Blat sends is easily read.

Is there a simpler way to add a CR at the end of every line of text in templog.txt so I can just echo it to RebootLog.txt and bypass the need for format.txt?

Thanks,

MJNot understanding you again. I am one of those people who needs to see examples. What the Input Looks like and what the desired output needs to look like. Then I code based on that information.The text that is output to the log report (templog.txt) looks like this:

Did not receive response from the cluster.
Did not receive response from the cluster.
Did not receive response from the cluster.
Did not receive response from the cluster.

But, when Blat uses the templog.txt file to create the email body the text in the email body looks like this:

Did not receive response from the cluster.Did not receive response from the
cluster.Did not receive response from the cluster.Did not receive response from the cluster.

I am ECHOING the contents of templog.txt to format.txt to correct the formatting so the email body looks like this:

Did not receive response from the cluster.

Did not receive response from the cluster.

Did not receive response from the cluster.

Did not receive response from the cluster.

I'd prefer not to have to use format.txt to finesses the data but there's something about how Blat views templog.txt (which is a plain text file btw) that is forcing the need. I just wonder if there is a less involved way to do what I've done than

Code: [Select]findstr /V "WLBS" logs\templog.txt >>logs\format.txt
FOR /F "Tokens=* Delims= " %%A in (logs\format.txt) do (
SET /a N+=1
echo ^%%A^ >>logs\RebootLog.txt

Thanks,

MJCan we see the entire batch file.I'm guessing that the line endings are not MSDOSsified.

Try this to see what it spits out.

Code: [Select]for /f "delims=" %%a in ('findstr /V "WLBS" logs\templog.txt') do cmd /c echo.%%a>>logs\RebootLog.txt


Discussion

No Comment Found