Pause / Time out breaks batch file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Quisquose
Posts: 41
Joined: 13 Jul 2016 19:25

Pause / Time out breaks batch file

#1 Post by Quisquose » 24 Jul 2016 22:34

I am using a batch file to clear out all System Event logs. It's basically a long list (approximately 500 lines) of the same command for each individual log file. The batch works fine when only these commands are included, but seeing as clearing all logs is quite a drastic action, it's something that I don't want to do inadvertently by clicking on the list.

I therefore thought I'd throw in a simple choice menu, just to make it clear what the batch was doing to do, and also to give the opportunity to back out if the batch was launched by accident.

I have no DOS batch skills, so I basically copied bits of other coder which I added prior to the long list of instructions that clears the logs. However, as soon as I added this menu choice, it causes the list of command to fail with a 'too many arguments error'.

I'd appreciate it if someone could help and advise me what I've done wrong when I pieced together commands to make this choice menu.

Here is the code that I am using. I've truncated the long list of 'wevtutil.exe' commands, but as I said above, there are nearly 500 of them and they work fine when they are the only commands in the batch file.

Thanks!

Code: Select all

@ECHO OFF

title Clear All Windows Event Log Files
:choice
cls
echo.
echo.
echo    DELETE ALL WINDOWS EVENT LOG FILES
echo   ------------------------------------
echo.
echo.
echo   1. Proceed with Deletion    (all event logs will be wiped)
echo   2. Cancel and Exit          (no changes wil be made)
echo.
echo.
echo.
set /P c=Type the number for option you want, then press Enter:
if /I "%c%" EQU "1" goto :clearlogs
if /I "%c%" EQU "2" goto :exit
goto :choice


:exit
exit


:clearlogs
echo.
echo.
timeout /t -1


wevtutil.exe cl  Analytic
wevtutil.exe cl  Application
wevtutil.exe cl  DirectShowFilterGraph
wevtutil.exe cl  DirectShowPluginControl
wevtutil.exe cl  Els_Hyphenation/Analytic
wevtutil.exe cl  EndpointMapper

douglas.swehla
Posts: 75
Joined: 01 Jun 2016 09:25

Re: Pause / Time out breaks batch file

#2 Post by douglas.swehla » 25 Jul 2016 00:33

Change "goto :choice" to "goto :clearlogs", and remove the "timeout" line.
Edit: No, don't do this. I misread your code when I answered this last night. See penpen's response: there's probably something you're not showing us.

Right now, entering a choice of "1" sends flow control back to the ":choice" label, so you never get as far as the TIMEOUT (false). When TIMEOUT is set to -1 seconds, it will wait forever for a keypress before continuing (true). The SET /P command already waits forever for the user to make a choice (true); once the choice is made, there's no need for another wait (opinion).

Edit: This advice is still legit, if you want to learn more about the tools you're working with.
Review the documentation for the SET, GOTO, and TIMEOUT commands to understand how your script works currrently. See the CHOICE and PAUSE commands for alternatives to the SET /P and TIMEOUT /T -1 approaches, respectively. (The CHOICE command is not native to Windows XP.)

You shouldn't have to list 500 separate files--that's nuts. You can use the FORFILES command (or the FOR and DIR commands) to repeat a command for a set of files.

To view a command's documentation, enter "HELP COMMANDNAME" or "COMMANDNAME /?" at the command prompt:

Code: Select all

C:\>help goto

or

Code: Select all

C:\>goto /?
Last edited by douglas.swehla on 25 Jul 2016 09:54, edited 1 time in total.

penpen
Expert
Posts: 1996
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Pause / Time out breaks batch file

#3 Post by penpen » 25 Jul 2016 05:53

douglas.swehla wrote:Change "goto :choice" to "goto :clearlogs", and remove the "timeout" line.
The "goto :choice" ensures, you don't leave the menu until you've typed in a valid choice:
So this was intentional and you shouldn't change it.

I'm unsure about the "timeout /t -1", but i think it probably is there for debug:
To be able to abort the batch file using ctrl+C.
So i think this is also intentional.


Quisquose wrote:Here is the code that I am using. I've truncated the long list of 'wevtutil.exe' commands, but as I said above, there are nearly 500 of them and they work fine when they are the only commands in the batch file.
Your above code works error free (well i've changed cl to qe, but that shouldn't hide that error), so the error (probably) is somewhere in the code you haven't posted.


penpen

douglas.swehla
Posts: 75
Joined: 01 Jun 2016 09:25

Re: Pause / Time out breaks batch file

#4 Post by douglas.swehla » 25 Jul 2016 09:46

penpen wrote:
douglas.swehla wrote:Change "goto :choice" to "goto :clearlogs", and remove the "timeout" line.
The "goto :choice" ensures, you don't leave the menu until you've typed in a valid choice

Well, that's what I get for late-night phone posting. I misread "if /I "%c%" EQU "1" goto :clearlogs" as "if /I "%c%" EQU "1" goto :choice", and thought there was an infinite loop. My bad. Penpen is right; the structure is fine.

penpen wrote:I'm unsure about the "timeout /t -1", but i think it probably is there for debug:
To be able to abort the batch file using ctrl+C.

Quisquose wrote:I therefore thought I'd throw in a simple choice menu, just to make it clear what the batch was doing to do, and also to give the opportunity to back out if the batch was launched by accident.

I think the SET /P command by itself satisfies the stated goal, but I can also see wanting a debugging pause or one last safety check before running. It certainly doesn't hurt anything.

Out of curiosity, I compared "timeout /t -1" to "pause" to see what, if any, differences there are. There are a couple, but they're pretty subtle:

Code: Select all

C:\>timeout /t -1

Press any key to continue ...

Code: Select all

C:\>pause
Press any key to continue . . .


TIMEOUT inserts a blank line before the message, while PAUSE does not. The dots in TIMEOUT are all together, while in PAUSE they're spaced out. Those are the only differences I see.

penpen wrote:Your above code works error free . . ., so the error (probably) is somewhere in the code you haven't posted.

I'm inclined to agree.

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

Re: Pause / Time out breaks batch file

#5 Post by Squashman » 25 Jul 2016 10:06

douglas.swehla wrote:
penpen wrote:Your above code works error free . . ., so the error (probably) is somewhere in the code you haven't posted.

I'm inclined to agree.

I will pass that motion.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Pause / Time out breaks batch file

#6 Post by aGerman » 25 Jul 2016 10:34

Just an alternative

Code: Select all

@echo off &setlocal
title Clear All Windows Event Log Files

echo    DELETE ALL WINDOWS EVENT LOG FILES
echo   ------------------------------------

choice /m "Do you want to continue" /c YN /d N /t 10
if %errorlevel%==2 exit /b

wevtutil.exe cl  Analytic
wevtutil.exe cl  Application
wevtutil.exe cl  DirectShowFilterGraph
wevtutil.exe cl  DirectShowPluginControl
wevtutil.exe cl  Els_Hyphenation/Analytic
wevtutil.exe cl  EndpointMapper

choice
/m "..." - message
/c YN - only the keys Y and N can be pressed to let the script continue
/d N /t 10 - defaults to N after 10s

if %errorlevel%==2 - Errorlevel is 2 if the 2nd choice (N) was entered.
exit /b - quit the script

Regards
aGerman

Quisquose
Posts: 41
Joined: 13 Jul 2016 19:25

Re: Pause / Time out breaks batch file

#7 Post by Quisquose » 25 Jul 2016 19:03

Thank you all for your kind replies.

Unfortunately, I still get the same error message.

I therefore started again from scratch and tried to keep things as simple as possible. I took a working copy of the batch, ran it to double check that it was working, and then pasted aGerman's code at the beginning. As soon as I did that I got the same errors as before.


Image


If I selected the text that I had just inserted and cut it out (and re-saved the batch), then it would run fine again.

So that you can see exactly what I am working with, I attach the actual file itself.

I am loathed to post all 500 lines of code into this thread because I don't even know whether the code window will scroll or just end up being stupidly huge. I can't see any way to attach files to this post, so I'll have to link to my online box account.

https://app.box.com/s/3784tme6jaltnkdwgccof74ossd0jk3r (The file has been renamed to a .txt extension). Box will automatically display a preview of the file's full contents, but you can ignore that and use the 'Download' button at the top right of the page to save the text file.

I'm not sure how any of you would be able to test it without wiping all your logs, but hopefully viewing the attachment might shed some light as to the issue.

Thanks again

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Pause / Time out breaks batch file

#8 Post by foxidrive » 25 Jul 2016 23:28

I didn't follow the thread but if you want to protect against the possibility of launching your script manually then you could use this as the first line of code:

Code: Select all

for /L %%a in (1,1,5) do timeout 300


The 5 is too much protection for the way I run my code, but that should give you a timeout prompt that lasts for 300 seconds, and five times, before continuing.

To make your code run immediately you would have to press 5 keys and the following code would run.
That gives you oodles of time to close the cmd window.

elzooilogico
Posts: 128
Joined: 23 May 2016 15:39
Location: Spain

Re: Pause / Time out breaks batch file

#9 Post by elzooilogico » 26 Jul 2016 04:39

After a quick view of the full file...
NOTE: there was relevant information missing in your first post, so previous posts didn't have a spot of the real problem (as some people pointed out). Please read http://www.dostips.com/forum/viewtopic.php?f=3&t=6108

The too many arguments error occurs in lines where the name of the log contains spaces, ie

Code: Select all

wevtutil.exe cl  Microsoft-Windows-AppLocker/EXE and DLL

Try the same inside quotation marks, ie

Code: Select all

wevtutil.exe cl  "Microsoft-Windows-AppLocker/EXE and DLL"

not sure, but I think it may work as expected
EDIT: (tested here in win 8 ES-es)

douglas.swehla
Posts: 75
Joined: 01 Jun 2016 09:25

Re: Pause / Time out breaks batch file

#10 Post by douglas.swehla » 26 Jul 2016 08:46

I agree with elzoologico: Put quotes around the names with spaces (or all of them, it doesn't hurt), and you should be fine. That's a lot of hunting and typing, of course, which means you probably want to use a loop instead.

Code: Select all

::If you're sure that you want to clear ALL the logs, use this:
for /f "tokens=* delims=" %%F in ('wevtutil el') do wevtutil cl "%%F"

::You can run this first to see the commands that _would_ be run:
for /f "tokens=* delims=" %%F in ('wevtutil el') do @echo wevtutil cl "%%F"

::You can also test by just querying the logs, instead of clearing them:
for /f "tokens=* delims=" %%F in ('wevtutil el') do wevtutil qe "%%F"

::To keep your custom list, without having to add quotes to it,
::cut and paste it into a text file, then reference the file in the command:
for /f "usebackq tokens=* delims=" %%F in ("path\to\logs to clear.txt") do wevtutil cl "%%~F"


To run any of the above from a command line instead of a batch file, replace the double percent signs (%%) with single ones (%). Review the FOR documentation to get familiar with loops, if you're not already. There's some funky stuff in there you might not expect if you're coming from a regular programming language.

Quisquose wrote:I took a working copy of the batch, ran it to double check that it was working, and then pasted aGerman's code at the beginning. As soon as I did that I got the same errors as before.

If I selected the text that I had just inserted and cut it out (and re-saved the batch), then it would run fine again.


I have no idea why lack of quotes would not be a problem before adding in new code, and would be a problem after. Are you adding the new code to the exact same file, or to a file that's supposed to have the same content? If the first file has quoted names, and the second doesn't, that would solve the mystery. I'd be interested in seeing a copy of the "before" file.

A good rule for debugging is to leave ECHOing turned on. This lets you see which commands are returning errors. In aGerman's code, break "@echo off &setlocal" line into two lines, and comment out the ECHO line:

Code: Select all

::@echo off
setlocal

Post Reply