generate a log file in task scheduler

Unitech Pt630d mobile computer, batch, 1...
July 7, 2010 at 20:25:33
Specs: Windows XP, 2gb
hi,

I did my script to backup daily files.But the script need to be run manually.Thus, I'm thinking of to do it run automatically in windows background. But if the script run automatically, it will not able to monitor the process either the files copied is successful or not. So, i need a technique that can log not just what the batch scripts are trying to do, but what the results are..

Below are my scripts..

@echo off

:: drive = destination %1 , source file = %2

set drive="%1"
set backupcmd=xcopy /s /d /e /h /i /f /y

%backupcmd% "%2" "%drive%"

@pause

I really don't have any idea to make it run automatically.

I did some reference: To log the result of this, I might try:

e.g:
net group MyGroup joe /add >> AddAccts.log

i got confused on this..can someone please guide and explain it to me clearly?


really need your guidance,
thanks.


See More: generate a log file in task scheduler

Report •


#1
July 7, 2010 at 22:41:42
You could add the following right after @echo off:

IF NOT %1!==/go! %0 /go %* 1>%0.results.txt 2>&1
SHIFT

Basically what is happening is when the script runs, it looks
for /go as it's first argument. If /go is not the first argument, it
executes the script (represented by %0), and passes /go and
any other arguments to the new instance, and logs the results
(both good and bad) to the script's name.results.txt. Now
whenever the script runs, the results will get captured.

Insomniac at large


Report •

#2
July 8, 2010 at 17:49:10
Thanks for the reply :)

the script works fine..but i wonder if there is any error in the process of backing up all files.. in my understanding,let say the user mistakenly type the source name wrongly.. (if mistakenly type the destination name,it will create new folder, thus there's no big deal)

I did try an error, for example i intentionally set wrong source name to run the backup process and the result that i received from .results.txt is " 0 file(s) copied " ... there are 2 meaning of "0 file(s) copied in this situation which is first, there are no file(s)/folder(s) that have been edited or additional files to be backed up and second, the files does not exist in the source (wrong source name assigned by the user)
..Thus i'm trying to upgrade the script in order for the script to differentiate between no new file(s) to be backed up and user wrongly insert wrong source name and print the result in log.. but it seems that i don't really understand the logic on how to manipulate the script and to output it as log.. (.results.txt)

fyi,
i did also try an error on the script and there are also other ways to output the result in log.Just wanna share with you :)

@echo off

set drive="C:\destination"
set backupcmd=xcopy /s /d /e /h /i /f /y

%backupcmd% "C:\source" "%drive%" >>.results.txt

@pause


thanks :)


Report •

#3
July 8, 2010 at 18:47:52
orangeboy, below are my script that i have edited..

@echo off

echo %date% %time% >>.results.txt

if exist "C:\Source" (
echo: File Exists
)

else (
echo: File Does Not Exist >>"C:\Results.txt")

IF NOT %1!==/go! %0 /go %* 1>%0.results.txt 2>&1
SHIFT

echo: >>.results.txt

set drive="C:\Destination"

set backupcmd=xcopy /s /d /e /h /i /f /y

%backupcmd% "C:\Source" "%drive%"


@paused

the script works fine except it doesn't print out the if statement either the file exist or not..

i wanted the output if there is no file to be copied like this;

1) if there is no new or updated file(s) to be copied:

0 file(s) copied
file exist

2) if there is wrong source folder:

0 file(s) copied
file does not exist

any idea?


Report •

Related Solutions

#4
July 8, 2010 at 18:54:28
Put:
IF NOT %1!==/go! %0 /go %* 1>%0.results.txt 2>&1 
SHIFT

immediately after @echo off

Insomniac at large


Report •

#5
July 8, 2010 at 19:04:55
i found that the script just can store the log for once..

Thus, i've change a bit the script in order for it to store the log everyday or every time the file are backed up..
but i still facing the same problem..the if statement doesn't work..

@echo off

echo:---------------------------------------------------------------------------------------------------->>.results.txt
echo: >>.results.txt
echo: >>.results.txt

echo %date% %time% >>.results.txt

echo: >>.results.txt

set drive="C:\Destination"

set backupcmd=xcopy /s /d /e /h /i /f /y

%backupcmd% "C:\Source" "%drive%" >>.results.txt

if exist "C:\Source" (
echo: File Exists
)

else (
echo: File Does Not Exist >>"C:\Results.txt")


@paused


Report •

#6
July 8, 2010 at 19:26:12
sorry, my bad..

i've corrected the syntax error..

@echo off

echo:---------------------------------------------------------------------------------------------------->>.results.txt
echo: >>.results.txt
echo: >>.results.txt

echo %date% %time% >>.results.txt

echo: >>.results.txt

set drive="C:\Destination"

set backupcmd=xcopy /s /d /e /h /i /f /y

%backupcmd% "C:\Source" "%drive%" >>.results.txt

@paused

if exist "%backupcmd%" (
echo. File Exists >>.results.txt

) else (
echo Destination Does Not Exist >>.results.txt

)

Hurm.. i just found that the script is wrong..
even the source is exist, the if statement still failed to recognized it..

Help me pls..


Report •

#7
July 8, 2010 at 20:02:32
below are my latest script :


@echo off


set source = "C:\Source"

echo:----------------------------------------------->>.results.txt
echo: >>.results.txt
echo: >>.results.txt

echo %date% %time% >>.results.txt

echo: >>.results.txt

set drive="C:\Destination"

set backupcmd=xcopy /s /d /e /h /i /f /y

%backupcmd% "C:\Source" "C:\Destination" >>.results.txt


if exist "%source%" (
echo. File Exists >>.results.txt
exit

)

else (
echo File Does not exist >>.results.txt


)

@paused


the script run smoothly except for the if statement..the output will always appear "File Does not exist " even though the file exist..below are the command i got from the prompt:

'else' is not recognized as an int
operable program or batch file.
'paused' is not recognized as an i
operable program or batch file.

:'(


Report •

#8
July 8, 2010 at 21:48:47
If you don't want the results file replaced, change this:
IF NOT %1!==/go! %0 /go %* 1>%0.results.txt 2>&1
SHIFT

to this, noting the double >>:

IF NOT %1!==/go! %0 /go %* 1>>%0.results.txt 2>&1
SHIFT

If you're not passing any arguments to the batch file, such as a
filename, you can also get rid of %* and SHIFT.

Insomniac at large


Report •

#9
July 8, 2010 at 22:59:21
great explanation!
thanks :)

Report •

#10
July 9, 2010 at 02:50:37

one more question...

let say the common prompt prompted
" invalid drive specification" when the destination path is invalid or not available .. the message will appear in a second and it will disappear..

is it possible if i wanted the message is printed out in .results.txt as well??

let say, other example, the disk is running out of space or disk is full and it will prompt the user through common prompt, it will be better if the message resides in .results.txt as it will log it and someone can review the problem occurred (review from log, .results.txt) in the future.


Report •

#11
July 9, 2010 at 05:36:12
All messages, good or bad, should be captured:

1>>%0.results.txt 2>&1

"1" represents the "stdout" stream, where "good" messages go
">>" says to redirect stream 1, and append all messages
"%0.results.txt" indicates a file, with %0 being the name of the batch
"2" represents the "stderr" stream, where "bad" messages go
">" says to redirect stream 2, but no appending
"&1" refers to the stdout stream's location

So 2>&1 put together says to send stream 2 wherever stream
1 is going, which is being appended to the end of
"%0.results.txt". Since both stdout and stderr are being sent
to the file, any error message encountered should be
captured.

Insomniac at large


Report •

#12
July 13, 2010 at 21:11:44
orangeboy,

i encountered some problem here..

the scripts run well...but i wanted to set date for the script to run... let say my last backup is on 7th july 2010..

and i wanted to backup again the folder and i just wanted to backup the new files starting on 8th july 2010..
but i did try to change the script,

set backupcmd=xcopy /s /d:07:07:2010 /e /h /i /f /y

or

set backupcmd=xcopy /s /d:(date) /e /h /i /f /y

but it did not print anything..
need your response asap..

thanks!


Report •


Ask Question