Batch file not working as expected...

Pages: 12
Hi,

I am using the following code to start a batch file with a C++ console application:
 
WinExec("removeOneDrive.bat", SW_SHOWNORMAL);


However, it starts and opens up but the code that is run in the batch file does not do anything, it is supposed to remove some files and registry keys etc... But it does not do any of these, no errors are shown as well.

Furthermore, when I start the batch file myself without C++ starting it, the batch file works as expected.

All the batch file is supposed to do is kill some running tasks, delete a couple of files and then remove some registry keys. Any ideas? I have tried running as admin but still no luck.

I have also tried this way but still does not work:
 
ShellExecuteA(NULL, "open", "removeOneDrive.bat", NULL, NULL, SW_NORMAL);
Did you check return value to see if bat file is actually executed?

1
2
HINSTANCE returnCode = ShellExecute(NULL, _T("open"), _T("removeOneDrive.bat"), NULL, NULL, SW_SHOWNORMAL);
assert(returnCode > 32);


It might be a problem with access rights too. Registry manipulation and file deletion are often blocked by UAC. You can solve this later as soon as you make sure that your bat file is executed.
The batch file does execute as the console window comes up and runs. Also I added the code as you did above and output the returncode to the console and got 0000002A.

I do run the program as admin and the console window which the batch file runs under is system32/cmd.exe so I thought admin rights are not the problem there?
2A is 42 so batch file is indeed executed.

Just a question, do you ned to use ShellExecute/WinExec fo this? In your case system("removeOneDrive.bat"); would do the same and be easier to read, write and debug.
I have always been told never to use system anything, because it is buggy and has a huge security hole. So I thought it would be better to learn a different way and do it the correct way.

I tried to use system but it does the same thing, the batch file runs but it does not do what it is supposed to do. It is really weird as I run the program as admin and even turn off UAC, but still not change and if I were to not let C++ start the batch file everything works as expected.

I normally use system in c++ , even if many programmer warns you to use it.

There is other altenatives as well.
Yeah it was just when I first started learning C++ I used this forum to help me in some areas and they always said never use system and always try to use another method, as it was deemed as a better way.
Can you show content of batch file? Does it expect to be working from specific directory?

Add echo commands to the beginning and end of file and pause to the end. Do they work?
Here is the batch file code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
@echo off
cls

set x86="%SYSTEMROOT%\System32\OneDriveSetup.exe"
set x64="%SYSTEMROOT%\SysWOW64\OneDriveSetup.exe"

echo Closing OneDrive process.
echo.
taskkill /f /im OneDrive.exe > NUL 2>&1
ping 127.0.0.1 -n 5 > NUL 2>&1

echo Uninstalling OneDrive.
echo.
if exist %x64% (
%x64% /uninstall
) else (
%x86% /uninstall
)
ping 127.0.0.1 -n 5 > NUL 2>&1

echo Removing OneDrive leftovers.
echo.
rd "%USERPROFILE%\OneDrive" /Q /S > NUL 2>&1
rd "C:\OneDriveTemp" /Q /S > NUL 2>&1
rd "%LOCALAPPDATA%\Microsoft\OneDrive" /Q /S > NUL 2>&1
rd "%PROGRAMDATA%\Microsoft OneDrive" /Q /S > NUL 2>&1

echo Removeing OneDrive from the Explorer Side Panel.
echo.
REG DELETE "HKEY_CLASSES_ROOT\CLSID\{018D5C66-4533-4307-9B53-224DE2ED1FE6}" /f > NUL 2>&1
REG DELETE "HKEY_CLASSES_ROOT\Wow6432Node\CLSID\{018D5C66-4533-4307-9B53-224DE2ED1FE6}" /f > NUL 2>&1


Also yes the echo and pause works.
when you launch the exe the environment is different probably so try echo %cd% at the beginning of your batch file to see if this is the issue.
Tried it and does not work. Thanks for trying thought. I really cannot see why this happens. It must be something to do with permissions when starting the batch file with C++.
are you the administror in your computer ?

try remove the line containing these HKEY_CLASSES_ROOT
I removed the HKEY lines and still not working. It doesn't even remove the onedrivesetup.exe.

And yes I am admin.
Remove redirection of output, remove turning off echo lines. Ge the full info on what happens. All those operations are probably trying to tell why they are not able to work, but you silenced them.
I hate to say this but I tried what you said and it still does not work. It does not even give any error, just performs like it should when echo off is not there. But does not do any of the actions it is supposed to do.

Thanks for trying to help me though.
Does anyone know the solution to this problem? Been experiencing multiple errors. Thank you!

Create a class named DentalAppointment. Include fields for a patient’s data (use the Person class),
the date (using the Date class), the time (use the Time class), and the duration of the appointment
in minutes. Also include a field that contains the ending time of the appointment; this field will be
calculated based on the start time and the duration using the Time class function that adds minutes
to a Time object. The DentalAppointment constructor requires a first and last name, and a month,
day, year, hour, and minute for the appointment. Allow a DentalAppointment to be constructed
with or without an additional argument for appointment duration, and force the duration to 30
minutes when no argument is supplied. The constructor does not allow any appointment over 240
minutes. The constructor calculates the appointment ending time based on the start time and the
duration. Also include a display function for the DentalAppointment class. Write a main()function
that loops at least three times, prompting the user for DentalAppointment data and displaying all
the information. (Note, if you use the Person class display function, the zip code will be “X”; this is acceptable.)

Note: We already made a file in notepad, ClassData.txt
So, am I going to use instream infile ofstream outfile? Thank you again
@jaraniv: please do not hijack another threads.

@SoftMOUNT: did you remove output redirection too? If you remove /Q from rd will it ask for confirmation? Will it work if you rename bat file to foo.bat?
What is exact output of that bat file execution? (with all echo commands and pause at the end)
When I remove the /Q it does not work but it makes the process longer with no confirmation.

Here is the output with all echo commands and pause at the end... Ignore the /Q, I did try that before but I put them back in, the output is still the same as below without or with the /Q:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
C:\Users\Base\Desktop>set x86="C:\Windows\System32\OneDriveSetup.exe"

C:\Users\Base\Desktop>set x64="C:\Windows\SysWOW64\OneDriveSetup.exe"

C:\Users\Base\Desktop>echo Closing OneDrive process.
Closing OneDrive process.

C:\Users\Base\Desktop>echo.


C:\Users\Base\Desktop>taskkill /f /im OneDrive.exe  1>NUL 2>&1

C:\Users\Base\Desktop>ping 127.0.0.1 -n 5  1>NUL 2>&1

C:\Users\Base\Desktop>echo Uninstalling OneDrive.
Uninstalling OneDrive.

C:\Users\Base\Desktop>echo.


C:\Users\Base\Desktop>if exist "C:\Windows\SysWOW64\OneDriveSetup.exe" ("C:\Windows\SysWOW64\OneDriveSetup.exe" /uninstall )  else ("C:\Windows\System32\OneDriveSetup.exe" /uninstall )

C:\Users\Base\Desktop>ping 127.0.0.1 -n 5  1>NUL 2>&1

C:\Users\Base\Desktop>echo Removing OneDrive leftovers.
Removing OneDrive leftovers.

C:\Users\Base\Desktop>echo.


C:\Users\Base\Desktop>rd "C:\Users\Base\OneDrive" /Q /S  1>NUL 2>&1

C:\Users\Base\Desktop>rd "C:\OneDriveTemp" /Q /S  1>NUL 2>&1

C:\Users\Base\Desktop>rd "C:\Users\Base\AppData\Local\Microsoft\OneDrive" /Q /S  1>NUL 2>&1

C:\Users\Base\Desktop>rd "C:\ProgramData\Microsoft OneDrive" /Q /S  1>NUL 2>&1

C:\Users\Base\Desktop>echo Removeing OneDrive from the Explorer Side Panel.
Removeing OneDrive from the Explorer Side Panel.

C:\Users\Base\Desktop>echo.


C:\Users\Base\Desktop>REG DELETE "HKEY_CLASSES_ROOT\CLSID\{018D5C66-4533-4307-9B53-224DE2ED1FE6}" /f  1>NUL 2>&1

C:\Users\Base\Desktop>REG DELETE "HKEY_CLASSES_ROOT\Wow6432Node\CLSID\{018D5C66-4533-4307-9B53-224DE2ED1FE6}" /f  1>NUL 2>&1

C:\Users\Base\Desktop>pause
Press any key to continue . . .


When I change the name to foo it does not open at all. Also remember this batch file works when it is not started by C++.
Last edited on
You still not removed output redirection: remove all those 1>NUL 2>&1

Ohh so sorry, I did not know I needed to remove them as well.

Ok so what happened is, it says it cannot find the registry key. However when you run the batch file on its own and not with C++ it works and find and deletes the reg key after asking me. But it does not do this when I run it with C++.

Below is the output when I run the batch file with C++:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
C:\Users\Base\Desktop>set x86="C:\Windows\System32\OneDriveSetup.exe"

C:\Users\Base\Desktop>set x64="C:\Windows\SysWOW64\OneDriveSetup.exe"

C:\Users\Base\Desktop>echo Closing OneDrive process.
Closing OneDrive process.

C:\Users\Base\Desktop>echo.


C:\Users\Base\Desktop>taskkill /f /im OneDrive.exe
ERROR: The process "OneDrive.exe" not found.

C:\Users\Base\Desktop>ping 127.0.0.1 -n 5

Pinging 127.0.0.1 with 32 bytes of data:
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128

Ping statistics for 127.0.0.1:
    Packets: Sent = 5, Received = 5, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms

C:\Users\Base\Desktop>echo Uninstalling OneDrive.
Uninstalling OneDrive.

C:\Users\Base\Desktop>echo.


C:\Users\Base\Desktop>if exist "C:\Windows\SysWOW64\OneDriveSetup.exe" ("C:\Windows\SysWOW64\OneDriveSetup.exe" /uninstall )  else ("C:\Windows\System32\OneDriveSetup.exe" /uninstall )

C:\Users\Base\Desktop>ping 127.0.0.1 -n 5

Pinging 127.0.0.1 with 32 bytes of data:
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128

Ping statistics for 127.0.0.1:
    Packets: Sent = 5, Received = 5, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms

C:\Users\Base\Desktop>echo Removing OneDrive leftovers.
Removing OneDrive leftovers.

C:\Users\Base\Desktop>rd "C:\Users\Base\OneDrive"
The system cannot find the file specified.

C:\Users\Base\Desktop>rd "C:\OneDriveTemp"
The system cannot find the file specified.

C:\Users\Base\Desktop>rd "C:\Users\Base\AppData\Local\Microsoft\OneDrive"
The directory is not empty.

C:\Users\Base\Desktop>rd "C:\ProgramData\Microsoft OneDrive"
The directory is not empty.

C:\Users\Base\Desktop>echo Removeing OneDrive from the Explorer Side Panel.
Removeing OneDrive from the Explorer Side Panel.

C:\Users\Base\Desktop>REG DELETE "HKEY_CLASSES_ROOT\CLSID\{018D5C66-4533-4307-9B53-224DE2ED1FE6}"
Permanently delete the registry key HKEY_CLASSES_ROOT\CLSID\{018D5C66-4533-4307-9B53-224DE2ED1FE6} (Yes/No)? Yes
ERROR: The system was unable to find the specified registry key or value.

C:\Users\Base\Desktop>REG DELETE "HKEY_CLASSES_ROOT\Wow6432Node\CLSID\{018D5C66-4533-4307-9B53-224DE2ED1FE6}"
Permanently delete the registry key HKEY_CLASSES_ROOT\Wow6432Node\CLSID\{018D5C66-4533-4307-9B53-224DE2ED1FE6} (Yes/No)? Yes
ERROR: The system was unable to find the specified registry key or value.

C:\Users\Base\Desktop>pause
Press any key to continue . . .
Pages: 12