0

My code is as below

set log_file = Automation_log.log

call :main > %log_file%
exit /b
:main

call :main > %log_file% produces a syntax error.

You might wonder why I do not pass the log file name directly next to >, I did it because I've put the date and time in the file name and it would get customised.

By following the comment, below this is how my code looks

@echo off

For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c-%%a-%%b)
For /f "tokens=1-2 delims=/:" %%a in ("%TIME%") do (set mytime=%%a%%b)

set log_file = Automation_Log__%mydate%_%mytime%.log

call :main > "%log_file%"
exit /b
:main

This too give error as The system cannot find the path specified.

JBJ
  • 173
  • 1
  • 1
  • 15
  • Try `Set "log_file=Automation_log.log"`, followed by `Call :main >"%log_file%"`. Currently you have a variable named `log_file` with a string value of `Automation_log.log`. – Compo Nov 21 '19 at 13:59
  • Using your comment, here is the code but that fails @echo off For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c-%%a-%%b) For /f "tokens=1-2 delims=/:" %%a in ("%TIME%") do (set mytime=%%a%%b) set "log_file = Automation_Log__%mydate%_%mytime%.log" call :main > "%log_file%" exit /b :main – JBJ Nov 21 '19 at 14:38
  • You have not followed the advice I gave, unless you do, the script will remain problematic. Additionally, using `Date /T` and `%TIME%`, as you have, can only be guaranteed to be correct under your specific login of your own PC. – Compo Nov 21 '19 at 15:03
  • I want to expand on the comment from @Compo. The error you are getting is because `log_file` *without a trailing space* **IS NOT DEFINED**. Thus `call :main > %log_file%` produces an error because with `log_file` not defined the command becomes `call :main > ` which is invalid. In CMD, you DO NOT want spaces around the `=` in a set command. All you need to do to fix your error is `set log_file=Automation_log.log` with no spaces on either side of the `=`. – avery_larry Nov 21 '19 at 16:20

1 Answers1

1

Here's your script with the correctly defined variable using a non local/PC dependent method of obtaining the date and time. This example should just create your text file with the content Test:

@Echo Off
Set "log_file="
For /F "Tokens=1-6Delims=/: " %%A In ('RoboCopy/NJH /L "\|" Null'
) Do If Not Defined log_file Set "log_file=Automation_Log__%%A%%B%%C_%%D%%E_%%F"
Call :Main>"%log_file%"
GoTo :EOF

:Main
Echo Test
Exit /B

You didn't indicate your expected date and time format, so I've used yyyyMMdd_hhmmss. If you wish to change that, rearrange the order on line 4, where %%A=yyyy, %%B=MM, %%C=dd, %%D=hh, %%E=mm and %%F=ss.

Compo
  • 36,585
  • 5
  • 27
  • 39
  • Excellent, Thanks Compo. Sorry for the delay, I was OOO. – JBJ Nov 25 '19 at 13:05
  • I wish to get the time seconds in the file name as there is chance of getting an additional process running to generate the log. What would happen If I have the Exit /B line before the :Main? I've next few steps that are getting executed, which will require the output be recorded in this file. In addition, Once my process is over I need to read this same log file and searc for 'Failed: 0' and then sent out an email. – JBJ Nov 25 '19 at 13:08
  • @JBJ, I have changed my answer to now include seconds in the log file name. – Compo Nov 25 '19 at 13:31
  • Thanks, got it and I tried similarly as I am able to understand a little more now. – JBJ Nov 25 '19 at 13:32
  • I've the log file containing the lines like this "Total tests: 1. Passed: 0. Failed: 1. Skipped: 0." \n If the Failed: contains any number other than 0, then I need to send a message in email with the log file as attachment. – JBJ Nov 25 '19 at 13:34
  • @JBJ, `GoTo :EOF` already acts in the same way as `Exit /B`, so it does not need to be moved. You are supposed to put your actual commands between the line containing `:Main` and the line containing `Exit /B`. My answer was to your question, providing it does not contract me in to a personal support role. If you have a new question, then it should be posted as a new question. Please note that when asking a question you must have attempted to code it yourself, and include that code within your question body, complete with an explanation of how that code fails to work as written and intended. – Compo Nov 25 '19 at 13:35
  • I wanted to read that log file and if I find a string "Failed: 0." then I should stop the execution. But if I find the value after "Failed: " is 1 or more, I need to send an email. – JBJ Nov 25 '19 at 13:52
  • Sorry that I was asking this repeatedly... I understand that you are not supposed to provide personal support role. I'll post a new question. Thanks for helping me a lot. – JBJ Nov 25 '19 at 13:54
  • `find "Failed: 0" file.log && goto :eof || goto :SendMail` – Stephan Nov 25 '19 at 15:38
  • @Compo Could you please help https://stackoverflow.com/questions/59036631/log-file-reading-and-finding-a-value-string – JBJ Nov 27 '19 at 14:22
  • @JBJ, I have seen that question, however you've changed it from simply sending the StdOut from the commands within `:Main` to a specifically named log file to something completely different. My only assumptions are therefore that you have not researched how to send an email with attachment from a [tag:batch-file], _which is outside of the scope of StackOverflow, because you're expected to have attempted it yourself_, or your issue is with [tag:findstr], not fully matching the pattern you require. Please explain within your question, what exactly your issue is, given that it has been answered. – Compo Nov 27 '19 at 14:49