0

I have a c:\myapp folder which contains MyProcess.exe file. I also have c:\myapplog folder.

I have a cmd batch file named GetSuffix.cmd in c:\myapp folder which calculates a value and then echoes that value at the end. The value to return is of 8 characters length.

@echo off
rem logic to set suffixV variable 
echo %suffixV%

I want to schedule MyProcess.exe file to run every day at a specific time. And I want to writes its output in c:\myapplog folder as MyProcess_XXXXXXXX.log file where XXXXXXXX is a string whose value needs to come from the GetSuffix.cmd file.

So in Task Scheduler, I created a Task for this purpose. In the Action Start a program of this Task, I mention:

Program/script: cmd
Add arguments (optional): /c MyProcess.exe > c:\myapplog\MyProcess_GetSuffix.cmd.log
Start in (optional): c:\myapp

I ran the Task and it ran successfully but it created MyProcess_GetSuffix.cmd.log file in c:\myapplog folder. The GetSuffix.cmd was not evaluated. How can I do it?


Example

For example the cmd batch file named GetSuffix.cmd in c:\myapp folder contains these lines:

@echo off
for /f "skip=1" %%x in ('wmic os get localdatetime') do if not defined MyDate set MyDate=%%x
set suffixV=%MyDate:~0,4%%MyDate:~4,2%%MyDate:~6,2%
echo %suffixV%

So today if I call GetSuffix.cmd from cmd, it will return 20200812.

So today I want my Task to be run this way:

cmd /c MyProcess.exe > c:\myapplog\MyProcess_20200812.log

Tomorrow if I call GetSuffix.cmd from cmd, it will return 20200813.

So tomorrow I want my Task to be run this way:

cmd /c MyProcess.exe > c:\myapplog\MyProcess_20200813.log

How can I do that?

srh
  • 1,661
  • 4
  • 30
  • 57
  • It seems as if you want your output here, `"%~dp0..\myapplog\%~n0.log"` – Compo Aug 12 '20 at 10:42
  • It is not possible to specify a batch file name to execute by `cmd.exe` inside a log file name and expect that `cmd.exe` detects the batch file name inside the log file name, executes the batch file, and replaces the batch file name inside log file name by the output of the batch file. – Mofi Aug 22 '20 at 09:08
  • The solution is as follows. Configure the scheduled task with *Program/script* being `C:\Windows\System32\cmd.exe` and *Add arguments (optional)* being `/C RunMyProcess.cmd` and *Start in* being `C:\myapp`. The batch file `RunMyProcess.cmd` needs just one line: `@for /F "tokens=1-3 delims=/: " %%I in ('%SystemRoot%\System32\robocopy.exe "%SystemDrive%\|" . /NJH') do @MyProcess.exe >C:\myapplog\MyProcess_%%I%%J%%K.log & exit /B`. So the batch file gets current year, month and day from an error message output by `robocopy.exe` and runs `MyProcess.exe` with output redirected into the log file. – Mofi Aug 24 '20 at 05:11
  • The batch file `RunMyProcess.cmd` must be also in directory `C:\myapp`. For an explanation on how the current date is got region independent using `robocopy` for usage in log file name read Compo's or my answer on [Time is set incorrectly after midnight](https://stackoverflow.com/questions/60124351/) where this solution is explained in full detail. The command `exit` with option `/B` results in exiting the execution of batch file `RunMyProcess.cmd` after having processed first line output by `robocopy.exe` and running `MyProcess.exe`. – Mofi Aug 24 '20 at 05:13

1 Answers1

1

I would say, make a scheduled task that runs "run.cmd". Change getsuffix to

@echo off
for /f "skip=1" %%x in ('wmic os get localdatetime') do if not defined MyDate set MyDate=%%x
set suffixV=%MyDate:~0,4%%MyDate:~4,2%%MyDate:~6,2%
// OUTPUTS TO A FILE FOR FUTURE USE
echo %suffixV% > suffix.temp
// PRINTS SUFFIX VARIABLE
echo %suffixV%
// EXITS
exit

Then, use this for run.cmd:

@echo off
// WILL GET SUFFIX NUMBER
getsuffix.cmd
// SETS SUFFIX NUMBER AS VARIABLE
set /p suffix=<suffix.temp
// USES VARIABLE TO NAME FILE 
cmd /c myprocess.exe > c:\myapplog\MyProcess_%suffix%.log
// DELETES THE EXTRA FILE
del suffix.temp
// EXITS
exit

Or, you can copy-paste the getsuffix.bat code into the scheduled task to make it all one file.

Hope this is what you were looking for.

CLSullivan
  • 11
  • 2
  • I recommend to read what I wrote about `wmic` in my answer on [Time is set incorrectly after midnight](https://stackoverflow.com/a/60126994/3074564) and next [Batch file datetime windows settings](https://stackoverflow.com/questions/63584718/) and the comments below this question. Then you should know that your code could not work in some cases, especially on taking into account that the environment variable `MyDate` is not explicitly undefined above the `for` loop. So if this variable is defined by chance outside the batch file, the current date is never assigned to the variable. – Mofi Aug 27 '20 at 07:31
  • Thanks Mofi for that. – Anonymous Aug 28 '20 at 14:29