Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

start /wait command not working in batch file with sfc.exe /scannow

Posted on 2009-05-19
17
Medium Priority
?
3,380 Views
Last Modified: 2012-05-07
HI all

Im trying to utilise the start /wait command in my batch file so the batch will wait until sfc.exe finishes before it carries on. At the moment I have to use 'pause' so it waits for me to click any key to continue after if has run.

I cant seem to get it to work. Im trying

Start /wait "" "c:\windows\system32\sfc.exe" /scannow

echo has it worked?

pause

In theory this should run sfc.exe and not give me the message "has it worked" until it has finished scanning. But it doesnt, it gives me that message straight away.

The long story is i want to perform a few other tasks after sfc.exe has run but I want it to run the scan before the other tasks run.

Ive tried re-wording it, I recently found out about adding the dummy string in at the front with the "", Im at a loss, can anyone help please?
0
Comment
Question by:ScumPuppy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 7
17 Comments
 
LVL 71

Expert Comment

by:Qlemo
ID: 24422558
If you call sfc without start, does it wait? It does on my Vista PC.
0
 
LVL 7

Expert Comment

by:Hubasan
ID: 24423588
Hi ScumPuppy,

Unfortunately this will not work since SFC.exe , or Windows File Protection scan (in windows XP at least) is not running under SFC.exe but under different service.

SFC.exe just executes Winlogon.exe function that scans the files for Windows File Protection, so the result you see where START /WAIT command is not working is actually true since SFC.exe just executes the function and then closes immediately. Winlogon.exe is the process that actually shows you the Windows File Protection scan and progress screen. And you can't really wait for Winlogon to close in your batch file since this process is a core system process and is always on.

What you can use is this:

tasklist /FI "WINDOWTITLE eq Windows File Protection" | FIND "winlogon.exe"

Command above will look for a window title called "Windows File Protection" and will PIPE that command to FIND command that will look for "winlogon.exe" and if it DOESN'T find it, it will be at ERRORLEVEL 1

Example script below can demonstrate this:

Hope this helps :-)



@echo off
 
:check
CLS
ECHO Checking...
ping -n 2 127.0.0.1>nul
tasklist /FI "WINDOWTITLE eq Windows File Protection" | FIND "winlogon.exe"
IF ERRORLEVEL 1 GOTO :execute
goto :wait
 
:wait
CLS
ECHO Windows File Protection still running...
ECHO Waiting 5 seconds
ping -n 2 127.0.0.1>nul
CLS
ECHO Windows File Protection still running...
ECHO Waiting 4 second
ping -n 2 127.0.0.1>nul
CLS
ECHO Windows File Protection still running...
ECHO Waiting 3 seconds
ping -n 2 127.0.0.1>nul
CLS
ECHO Windows File Protection still running...
ECHO Waiting 2 seconds
ping -n 2 127.0.0.1>nul
CLS
ECHO Windows File Protection still running...
ECHO Waiting 1 second
ping -n 2 127.0.0.1>nul
goto :check
 
:execute
ECHO Executing command
 
goto :end
 
:end

Open in new window

0
 
LVL 71

Expert Comment

by:Qlemo
ID: 24424025
1. Using colon in goto is not recommended by MS. It does not harm, however.
2. the goto :wait is superfluous. No code between goto and the label
3. The wait part could be a bit compressed (see below)
4. I would check every second, and instead count up the wait time so you have visual feedback how long SFC went.


:wait
for /L %%C in (5,-1,0) do (
  CLS
  ECHO Windows File Protection still running...
  ECHO Waiting %%C seconds
  ping -n 2 127.0.0.1>nul
)
goto :check

Open in new window

0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 1

Author Comment

by:ScumPuppy
ID: 24428696
Hubasan and Qlemo

Thanks for the comments, this sounds very promising.. I will try this when Im next back at work on friday and will update my findings on here :)

Sounds VERY promising :)

Thanks.. will get back to you both!!
0
 
LVL 1

Author Comment

by:ScumPuppy
ID: 24448095
Hi guys

Ive tried both versions and it still doesnt work. It runs sfc.exe but the batch file continues to progress whilst sfc is running.

To test its working Im putting a line after it like:

ECHO Test to see if its worked
pause

so if this is working it shouldnt say that line on the screen OR pause the batch and ask to 'press any key' to continue until after the sfc.exe utility  has finished running. unfortunately it goes straight to that point as soon as it starts running :(

any other ideas?
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 24449203
It works for me, but I've a German system, so window title is not the same. Please execute the tasklist command alone
tasklist /FI "WINDOWTITLE eq Windows File Protection"  /v
while sfc runs, to see if the filter is correct.

0
 
LVL 71

Accepted Solution

by:
Qlemo earned 1000 total points
ID: 24449237
Additionally, I would prefer using this code

@echo off
 
:check
ECHO Checking...
ping -n 2 127.0.0.1>nul
set sec=1
tasklist /FI "WINDOWTITLE eq Windows File Protection" | FIND /i "winlogon.exe" >nul || goto execute
 
ECHO Windows File Protection still running...
ECHO Waiting %sec% seconds yet
set /A sec += 1
ping -n 2 127.0.0.1>nul
goto :check

Open in new window

0
 
LVL 1

Author Comment

by:ScumPuppy
ID: 24449611
when i run the tasklist on its own whilst sfc is running I get:

ERROR: not found

so when it works for you does the batch file stay open and not move to the next command until the sfc.exe progress bar has finished? Its not working for me :(
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 24449702
This is more than strange. tasklist without anything is bringing a list of running processes, I hope? I can't sort that error message into any category I would know of:
- if tasklist would not be found, a message   command not found ...   would appear
- if tasklist would not found the filtered process, it outputs an INFORMATIONAL message, no ERROR.

0
 
LVL 1

Author Comment

by:ScumPuppy
ID: 24449759
I run sfc.exe /scannow

whilst its running i then type:

tasklist /FI "WINDOWTITLE eq Windows File protection" /v

The following appears directly under it:

ERROR: Not found
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 24449769
Please try
tasklist
alone!
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 24449773
And if that works,
tasklist /v
while sfc is running.
0
 
LVL 1

Author Comment

by:ScumPuppy
ID: 24449896
if i run sfc.exe /scannow and then type:

tasklist

I get:

ERROR: not found.

Rather than look for the window named Windows File Protection would it be an idea to look for the process of winlogon.exe?

I dunno.. Im scratching at straws :(

All I want it to do is not go to the next line down until the scan has finished, so if you are testing it can you put a pause at the end of everything and see if it gives you the pause once the scan has completed or if it gives it you straight away while the scan is in progress (as it does currently for me)?

thanks for your time by the way :)
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 24449939
Are we talking about Windows XP here? Please try to search for tasklist.exe, it should be found in your windows\system32 folder.

And no, WinLogon is a process always running, it just opens a message box with the progress bar, and that window we have to catch. At the moment, I do not know of other means than tasklist to find the window from commandline.
0
 
LVL 1

Author Comment

by:ScumPuppy
ID: 24449972
Yes, sorry, I should have said, it is windows XP and yes tasklist.exe is present in sys32

0
 
LVL 71

Expert Comment

by:Qlemo
ID: 24449984
In this case you have serious trouble. It appears to me that some required DLLs are not working. Have a look into your EventLog if some errors report there.
0
 
LVL 1

Author Closing Comment

by:ScumPuppy
ID: 31582953
Qlemo - legend, expert help, very fast, I was really pleased, thanks for your help :)
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question