Learning .bat

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
tjsfury
Posts: 1
Joined: 19 Apr 2014 10:56

Learning .bat

#1 Post by tjsfury » 19 Apr 2014 11:28

Hey I am trying to learn some basic .bat programming and haven't had any luck with google.
I am using a windows 7 pc and this is what I am trying to do:
Create a program that looks at the current tasks using tasklist.
Saves the list to a file
Reads the file into an array
opens a browser (chrome/ie/w/e)
loads task list into a new file
compare the 2 files to find the new PID
end that PID

so basically the program opens a new window and only closes that window.

Any help would be appreciated I have been trying to do arrays to store the file and have had issues.

This is not a homework assignment or anything like that just trying to get familiar with batch because I think its useful so feel free to change what the program does to accomplish the task or just help with general arrays.

PS. I am familiar with C#, C++, C, Java, and Python along with many other random languages so if you compare batch to one of them i'll understand.

Thanks

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

Re: Learning .bat

#2 Post by Squashman » 19 Apr 2014 14:16

Comparing two outputs of Tasklist and using pseudo arrays seems kind of overkill for what you want to do.

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

Re: Learning .bat

#3 Post by aGerman » 19 Apr 2014 15:15

You could use WMIC to create a process. Simply extract the PID from its output.
This example opens its own code in notepad and closes the notepad instance after a 5 seconds delay ...

Code: Select all

@echo off &setlocal
for /f "delims==; tokens=2" %%i in (
  '2^>nul wmic process call create '"notepad.exe" "%~sf0"'^|findstr /i "\<ProcessId\>"'
) do set /a PID=%%i
echo Process ID: %PID%
timeout /t 5
taskkill /pid %PID% /f

Regards
aGerman

EDIT replaced outer double quotes with single quotes for the expression next to create

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

Re: Learning .bat

#4 Post by foxidrive » 19 Apr 2014 21:31

tjsfury wrote:I am using a windows 7 pc and this is what I am trying to do:
Create a program that looks at the current tasks using tasklist.


Do you want to exclude services, and only focus on the console category? EG:

Code: Select all

weather.exe                   6764 Console
IAStorIcon.exe                7404 Console
IAStorDataMgrSvc.exe          7668 Services
syncagentsrv.exe              7752 Services
TOTALCMD.EXE                  6376 Console
audiodg.exe                   4756 Services
officeclicktorun.exe          2796 Services
SearchIndexer.exe             5640 Services
wmpnetwk.exe                  7588 Services
taskhost.exe                  7872 Console
jucheck.exe                    876 Console
dwm.exe                       6672 Console

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Learning .bat

#5 Post by Aacini » 20 Apr 2014 12:21

If you want to review the advanced operations we can do in Batch files, compared vs. those of standard languages, you may see this post about arrays, pointers and structures. The SO link in that post give a more detailed explanation about how the array concept is simulated in Batch files.

Antonio

Post Reply