Solved

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

Posted on 2009-05-19
17
3,299 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
  • 9
  • 7
17 Comments
 
LVL 68

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 68

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
 
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 68

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 68

Accepted Solution

by:
Qlemo earned 250 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 68

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 68

Expert Comment

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

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 68

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 68

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

762 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now