A Simple Batch Script Explained

jcgriff2

Co-Founder / Admin
BSOD Instructor/Expert
Microsoft MVP (Ret.)
Staff member
Joined
Feb 19, 2012
Posts
21,541
Location
New Jersey Shore
This is hardly worthy to be included among the many great threads in Sysnative Programming Forum, but an OP asked, so. . .



Batch file explained -
Code:
[FONT=Lucida Console][PLAIN]@echo off

::  J. C. Griffith
::  Microsoft MVP
::  SET command
::  2008-2013
 
[/PLAIN][LIST=1]
[*]cd /d %homedrive%
[*]echo. > 0
[*]echo. >> 0
[*]echo [PLAIN][code][font=lucida console][/PLAIN] >> 0
[*]echo. >> 0
[*]set >> 0
[*]echo. >> 0
[*]echo   %date%    %time% >> 0
[*]echo   %date%    %time% >> 0
[*]echo   %date%    %time% >> 0
[*]echo [PLAIN]
[/FONT][/PLAIN] >> 0
[*]start notepad 0
[/LIST][/FONT][/CODE]
1. extraneous - left over from other batch scripts
2. create a file named 0 -- or write spaces to it if it exists (note single >)
-- >> = append to file v. overwrite
3,5,7 - write a blank line (append)
4,11 - write CODE & FONT BB code to output file to make pasting into post easier for you & easier for me to read!
8,9,10 - write current DATE & TIME to output file -- just a habit of mine to repeat 3x

The 2 "real" commands are 6, 12
6 - run SET command; write to output file
12 - open the file 0 with NOTEPAD

A period after the ECHO statement = ignore text after it; write blank line

You can bring up a CMD screen & type SET - same output as appeared in Notepad.

http://www.techsupportforum.com/forums/f217/cant-find-c-users-public-folders-689658.html#post4064209

SET ENV variables -
Code:
HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment

http://www.sysnative.com/apps/jcgriff2/ENVvar_System_jcgriff2.bat
 
Thank you very much John, very interesting for someone who knows no batch at all. A very useful tutorial/explanation.

Thanks again,
Stephen.
 
You should probably add a del command somewhere at the end so that it doesn't just stay on the user's PC, or request for deletion in case they would like the option. :)

You could probably get away with something like this:
Code:
[PLAIN]@echo off
::  J. C. Griffith
::  Microsoft MVP
::  SET command
::  2008-2013

echo This window will close when you close notepad, and the file will be deleted...
echo [code][font=lucida console] > 0
set >> 0
echo. >> 0
echo %date% %time% >> 0
echo
[/FONT] >> 0
notepad 0
del /f/q 0[/PLAIN][/CODE]

This will prompt the user with the message "This window will close when you close notepad, and the file will be deleted..." in the console, before opening a file with all the variables available to that session, along with one date time stamp at the bottom enclosed in bbcode. When the user closes notepad, the script will finish, and the file 0 will be deleted. The start command would prevent that and continue with the script.

This is nice though, I used the set command output to parse from the value of a variable, the name of the variable itself to match for a few of my more advanced scripts.
 
Last edited:
I've used DEL before, but found that often the 0 file would get deleted before Notepad opened it!

Not even /p switch seems to help.
 
Last edited:
I've used DEL before, but found that often the 0 file would get deleted before Notepad opened it!

Not even /p switch seems to help.

The problem is with the start command you're using.

I've removed start from my code if you look for that difference :)
 
I read about START in your previous post, but it didn't register until I tried it -- thanks!
 

Has Sysnative Forums helped you? Please consider donating to help us support the site!

Back
Top