simple program for add delete some files in windows

hello respectful members of c++ this is my first post here, I always wonder how programmers do this programming job, I mean it's was always difficult for me. but now I determined to learn programming and would like to start it with c++.
so please help me to learn, members of this forum.
I want to start with simply thing like how to add delete and copy files using c++ for windows.
an old but useful thing to block ad's using host file.
guys please correct me if I am wrong I wrote this in windows cmd, to rename old host file and add New host file and add a short cutt file in startup folder so that file auto run on windows start up. and finally install a 3rd party exe file


this is abc.bat file

@ECHO OFF
::cd
echo %~n0%~x0 started from Directory: %~d0%~p0
%~d0
cd %~d0%~p0
:: ---------------chk for all files-----------------
IF NOT EXIST xyz.dll GOTO ReDownload
IF NOT EXIST HOSTS GOTO ReDownload
IF NOT EXIST kc.exe GOTO ReDownload
::copy a shortcut to run a 3rd party software at startup
COPY /Y kc.ink %USERPROFILE%\Start Menu\Programs\StartUp\
IF EXIST %USERPROFILE%\Start Menu\Programs\StartUp\kc.ink GOTO hostchk
COPY /Y kc.ink C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp\
IF EXIST C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp\kc.ink GOTO hostchk
COPY /Y kc.ink %APPDATA%\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\
IF EXIST %APPDATA%\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\kc.ink GOTO hostchk
COPY /Y kc.ink %ALLUSERPROFILE%\Start Menu\Programs\StartUp\
IF EXIST %ALLUSERPROFILE%\Start Menu\Programs\StartUp\kc.ink GOTO hostchk
::install a 3rd party software
:hostchk
kc.exe


::chk for host file to edit
IF "%OS%"=="Windows_NT" GOTO HostsFile
IF EXIST %winbootdir%\HOSTS*.* ATTRIB +A -H -R -S %winbootdir%\HOSTS*.*>NUL
IF EXIST %winbootdir%\HOSTS.MVP DEL %winbootdir%\HOSTS.MVP>NUL
IF EXIST %winbootdir%\HOSTS REN %winbootdir%\HOSTS HOSTS.MVP>NUL
IF EXIST %winbootdir%\NUL COPY /Y HOSTS %winbootdir%>NUL
GOTO EXIT
:HostsFile
IF EXIST %windir%\SYSTEM32\DRIVERS\ETC\HOSTS*.* ATTRIB +A -H -R -S %windir%\SYSTEM32\DRIVERS\ETC\HOSTS*.*>NUL
IF EXIST %windir%\SYSTEM32\DRIVERS\ETC\HOSTS.MVP DEL %windir%\SYSTEM32\DRIVERS\ETC\HOSTS.MVP>NUL
IF EXIST %windir%\SYSTEM32\DRIVERS\ETC\HOSTS REN %windir%\SYSTEM32\DRIVERS\ETC\HOSTS HOSTS.MVP>NUL
IF EXIST %windir%\SYSTEM32\DRIVERS\ETC\NUL COPY /Y HOSTS %windir%\SYSTEM32\DRIVERS\ETC>NUL
::flushDNScache
ipconfig /flushdns
::dnsok
:ReDownload
echo Please ReDownload ad blocker software and try to install again, or run it run as adminstrator or deactivate antivirus for 10 min.
EXIT





it will work with 3files hosts, kc.exe and finay a dll file.
I want to correct this code there is something wrong and how to do all this in c++?

besides learning c++ we will be able to make a open source adblocker.exe that will target host file and will protect us from harmful sites

Last edited on
It's important to note that this will only redirect URL's, it WILL NOT block pop-ups. The correct way to accomplish what you want is to write a browser extension, but that is much more ambitious than what you are capable of right now so just keep it in the back of your mind until later.

- This just replaces the host file so there is no need for the autorun, scrap that part. Furthermore, it's useless since not every user will have admin privileges. Don't worry about automatic propagation, if your application is worthwhile then the correct way to deploy it would be through Group Policy.

- The 'hosts' file doesn't have an extension, IDK what all of this "HOSTS.MVP" crap is but get rid of that too.

- 'hosts' isn't in the boot directory so there is yet another functionless part of code to get rid of.

Adding a file is done with 'CreateFile()': https://msdn.microsoft.com/en-us/library/windows/desktop/aa363858(v=vs.85).aspx and 'WriteFile()': https://msdn.microsoft.com/en-us/library/windows/desktop/aa365747(v=vs.85).aspx

Deleting a file is accomplished with 'DeleteFile()': https://msdn.microsoft.com/en-us/library/windows/desktop/aa363915(v=vs.85).aspx

Finally, copying a file is done with 'CopyFile()': https://msdn.microsoft.com/en-us/library/windows/desktop/aa363851(v=vs.85).aspx
thanks for reply.
above code is written for windows cmd, as far I just understand basis of cmd commands, just for get an idea what I want to do. my great intention is to write it in c++, because I want to learn c++.
for beginning redirect URL is easy then writing a browser extension.
you are right that only replacing the host file is necessary, but autorun will be used to tell user that you are enjoying ad free internet at every boot up.

for example an antivirus popup a alert box that you are protected by ABC antivirus.

another issue is that how to automatically get admin privilege, I have been googleing for grouppolicy. if you guys know how to do this, advices are welcome.

"HOSTS.MVP" the old host file will be renamed with .mvp so user replace it if any problem happen with new host file or for uninstall this program from users pc.
but autorun will be used to tell user that you are enjoying ad free internet at every boot up.


Please reconsider your logic here. You want to use the Autorun to advertise to the user that you are blocking advertisements to them, essentially a pop-up telling them that you are blocking pop-ups.

for example an antivirus popup a alert box that you are protected by ABC antivirus.

I'm pretty sure that Avast! is the only one that still does this, and people hate them for it.

another issue is that how to automatically get admin privilege, ...

There are a few ways to do this. The correct way in this case is to run that component of your application as a Windows Service. Doing this will also eliminate the need for the Autorun since the startup property would be built into the service.

"HOSTS.MVP" the old host file will be renamed with .mvp so user replace it if any problem happen with new host file or for uninstall this program from users pc.

In that case this is an acceptable precaution. But you're deleting it every time this script runs so the original backup is being destroyed on the second iteration. You should rethink that part.
I made similar thing but using cmd here sites.google.com/site/noadfree/
Topic archived. No new replies allowed.