PDA

View Full Version : Batch Files Knowledge Base


AphexDreamer
03-29-2008, 02:56 AM
Baiscly I'm making this thread so people can post what commands they know for batch files. I am curious and would like to know more about what can be done with batch files. I don't expect many people to know a lot, but I do expect a few people to know something.

All I know is how to make a batch file that will open Command Prompt. All you have to do is make a new text file, insert something like "CMD" or "command.com" save it as (Anything).bat and you have yourself a batch file that now executes Command Prompt.

So if anyone knows anything else, please share it on this thread pleaes.:)

CrAsHnBuRnXp
03-29-2008, 03:17 AM
I myself have never really understood the whole batch file thing and would love to learn more about it.

What i have always had questions on is how you know what does what and how you go about writing it that way and why.

AphexDreamer
03-29-2008, 03:23 AM
I myself have never really understood the whole batch file thing and would love to learn more about it.

What i have always had questions on is how you know what does what and how you go about writing it that way and why.

Yeah it would be kool if someone could explain it to us. I myself know very little.

Like if you put

@eacho off
echo Hello World

It will open Command Prompt and display "Hello World". How or why it does that I have no idea. Google it.

I found this http://www.happyhacker.org/gtmhh/beginnineb.shtml

wc413
03-29-2008, 04:23 AM
The only thing I use batch files for is to run commands for me that I would normally have to do manually. Here's one I use alot - netsh interface ip set address "Local Area Connection" dhcp
If you can't tell, it sets your NIC to get a DHCP address.

Kreij
03-29-2008, 05:19 AM
Batch files are exactly that. They are a file that contain a "batch" of commands so that you do not have to type them consecutively at a DOS prompt.

Anything that you can do at a DOS prompt you can do in a BF, and more.

What batch files allow you to do is take arguments when you call the file using the % identifier within the batch file, and also to use conditional statements (if's) to cause code redirection based on user input.

Batch file syntax is really easy to learn, and in a day or two you can write very powerful batch files to do a lot of stuff.

They are not so much useful to the home user as they are to someone who is an administrator of a network.

A simple use of a batch file ...

Let's say that you have a system that has 3 hard drives. C:, D:, E:
You want to copy the contents of C & D to E.

You create a file called MyCopy.cmd (just make a text file in notepad and rename it)
Then type in two lines.
"xcopy C:\ E:\MyCopyOfC /c /e /r /k (etc. xcopy siwtches
"xcopy D:\ E:\MyCopyOfD (switches)

Save the file and run from a DOS prompt.

This will create two directories (if the switches are correct) called MyCopyOfC and MyCopyOfD on the E: partition.

This is a simple backup solution for multiple hard drives.

You can , of course, get as elaborate as you want.

oily_17
03-29-2008, 07:23 PM
Don't use batch files very much but one I find useful is for creating a list of files in a directory.
Just copy the text below into Notepad and save as Filelist.bat

dir /a /-p /o:gen >filelist.txt

When you run it from a folder it will create a Filelist.txt file,in the same folder, that lists all the files in that folder.Useful when you are too lazy to type all the files out yourself.

AphexDreamer
03-29-2008, 07:24 PM
Don't use batch files very much but one I find useful is for creating a list of files in a directory.
Just copy the text below into Notepad and save as Filelist.bat

dir /a /-p /o:gen >filelist.txt

When you run it from a folder it will create a Filelist.txt file,in the same folder, that lists all the files in that folder.Useful when you are too lazy to type all the files out yourself.

Very nice and usefull thanks for contributing.

Jizzler
03-30-2008, 01:50 AM
A feature I found out about recently, the substring operator: "%var:~position,length%"

Like, "set year=%DATE:~10,4%", year now equals 2008.

Or for a yyyymmdd of the current date, which I use in some of my scripts to name files: "set filename=file.%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%"

I'll then use the filename var where needed - backup programs, RAR, etc.

"C:\Program Files\WinRAR\rar.exe" a -m3 -df %filename%.rar %filename%

Solaris17
05-25-2008, 11:57 PM
I use batch files alot im actually working on something now thats a benchmark program possibly another pi calculator without the 32m digit limit ill also be using them in the 4.0version of my disk to get multiple things done stay tuned! TPU PI on the way!

AphexDreamer
05-26-2008, 12:10 AM
I use batch files alot im actually working on something now thats a benchmark program possibly another pi calculator without the 32m digit limit ill also be using them in the 4.0version of my disk to get multiple things done stay tuned! TPU PI on the way!

Wow sounds really kool, can't wait. Maybe you can teach us some stuff?

Solaris17
05-26-2008, 12:21 AM
o ill absolutely tech you some stuff kreij tought me some basics and i cought on pretty quickly its awsome because after you learn a couple variables they can all interact with one another so you can make some great stuff...pretty powerfull notepad docs let me tell you

AphexDreamer
05-26-2008, 12:27 AM
o ill absolutely tech you some stuff kreij tought me some basics and i cought on pretty quickly its awsome because after you learn a couple variables they can all interact with one another so you can make some great stuff...pretty powerfull notepad docs let me tell you

Sweet!

Solaris17
05-26-2008, 05:25 PM
i need some help

so im making a batch file it needs to open CMD so i get it to open the prompt w/e

CALL %SystemRoot%\system32\cmd.exe

but now i need to input a command i need to do this because i dont know the exact path of the file i want to open but command prompt does


so say i wanted to open calc i need to know how to input calc.exe into the prompt

CALL %SystemRoot%\system32\cmd.exe
<input> calc.exe
pause

is that right? because im not sure how to input but thats what i want to do

mrw1986
05-26-2008, 05:30 PM
I use DOS to create all my files and shit haha....

copy con name.bat
@echo off
<insert code>

Solaris17
05-26-2008, 05:36 PM
what?

i cant manually do things in CMD i need to make a batch that someone can run to do it for them by inserting a line the batch file im making is going to be a vista bench for TPU but before i send it out i kinda have a good idea that it will work as im not running vista i know what i should input just not how.

PVTCaboose1337
05-26-2008, 05:41 PM
Here is some of my batch knowledge. I even had a thread about it.

So I sometimes use my wireless, enough not to disable the process in startup. So I decided that I would wanna be able to enable and disable it on demand. Here is the disable code:

C:\WINDOWS\system32\taskkill.exe /F /IM WMP54Gv4.exe /IM WLService.exe

This disables two services; WMP54Gv4.exe and WLService.exe, which are both vital to wireless.

mrw1986
05-26-2008, 05:41 PM
I use DOS to create all my files and shit haha....

copy con name.bat
@echo off
<insert code>

is unrelated to what you're talking about, its just a batch trick.

Solaris17
05-26-2008, 05:43 PM
Here is some of my batch knowledge. I even had a thread about it.

So I sometimes use my wireless, enough not to disable the process in startup. So I decided that I would wanna be able to enable and disable it on demand. Here is the disable code:

C:\WINDOWS\system32\taskkill.exe /F /IM WMP54Gv4.exe /IM WLService.exe

This disables two services; WMP54Gv4.exe and WLService.exe, which are both vital to wireless.

hmm ill see if i can modify it to work thnx

Solaris17
05-26-2008, 07:22 PM
kk that didnt work but i think iv got something now

can anyone with vista test this out for me?

id really appreciate it :)

if it doesnt work please rename the extension to .bat and try again :)

Lillebror
05-26-2008, 07:35 PM
You can't just rename a batch file to .exe :D it wont ever work! =D and that "bench" is built into vista :) And you cant input text into another cmd window.. But if you want to open calc.exe from a batch file just type calc.exe :)

Solaris17
05-26-2008, 07:39 PM
i had to rename it to be able to UL and if this works though it is a bench built into vista their is some neat stuff that happens ;)

Lillebror
05-26-2008, 07:42 PM
Hehe ;) You can actualy make some ol'school text games in batch :D

Solaris17
05-26-2008, 07:49 PM
haha i should sometime...w0ot!!! i just tested it and it worked...i know alot of people may already know how to access the hidden benchmarks on vista but i just want to make it easier next im going to set it up so you press button 1-W/E and it will start a certain benchmark...this should be fun i hope you guys like it..i certainly cant benifit i dont have vista installed but i can test it on my server :)

ill upload an updated one soon.

Solaris17
05-26-2008, 09:10 PM
kk did it change the extension to .bat for it to work tell me what you think!!!!!! i can edit the command peramiters to stress the GPU's more if you wish just let me know! :)

SpookyWillow
05-26-2008, 09:19 PM
it works yeah, a few tests run but i pressed escape cos they seem to last forever lol.

Solaris17
05-26-2008, 09:24 PM
hey thnks for testing it!!!! ya i havent added a time stop yet i need to impliment it but i need to figure out how to make it stop than start another do you think i should change the graphics? maybe bump it? i want to try and make it a usefull card benchmark

DanTheBanjoman
05-26-2008, 10:16 PM
A batch file basically is a list of command line commands executed after eachother. It can also use variables and subroutines. Most common uses for batchfiles are quite basic. Though you can create a relatively complex piece of code, mainly by using external programs.
For example, back in the good old days you had tools like ask which would return an errorlevel which you could then read out and respond to, this allowed you to make it somewhat interactive. You can use goto to create subroutines. Ah, and simple if statements are support, ie "if exist c:\command.com echo Yay command.com exists!"

To explain the first example given. Echo basically is the output to the screen, ie everything typed is echoed to the display. echo off turns this off. Adding a @ in front of a line turns off the echo for that single line. Therefor @echo off turns off the echo and can't be seen itself. Quite a common first line in a batch file.

Characters like > and the likes are part of the command line, > basically redirects output to whatever you add, for example > nul removes any output, which is a handy thing in a string. However you can also output to a file, or use >> to add to an existing file, handy for logging.

I usually make batchfiles for simple workarounds. For example at my work there is an issue with a database, the service needs a reboot upon IP change. Sinec we use it on laptops cables are plugged in and out all the time. (the program was meant for actual servers) Eventually I just made a simple batchfile which I put on users desktops. It along the line of

@echo Restarting UltraGateway, please wait...
@net stop "ultra service" > nul
@net start "ultra service" > nul


Basically it outputs a message so the users feels like it does something. Then it uses simple command line commands to first stop a service, then start it again. It redirects output to a black whole in a completely different dimension because the output isn't relevant to the users. Of course I could let it output as normally and add the command pause at the end, it'll show a "press any key to continue" message so you can read the output of the commands.


Anyway, I'm just ranting without any decent structure in my crap, so I'll stop. Batchfiles are the most basic form of programming, really easy to learn as well. Just get to know the command line (or DOS).

Solaris17
05-26-2008, 10:25 PM
fancy stuff their dan thanks for that :)

Lillebror
05-26-2008, 10:25 PM
Batchfiles arent programming :) Its scripting.
I made a small script to make a .txt with all my games in :)

dir /b > games.txt

Just save that line in a .bat file and put the file in a folder with all your games or programs, and it will create a new text file called "games.txt" with all the folder names :)

Also, if you wanna try something a little anoying try this script :D

@echo off
set d=c:\iamtest\
md c:\iamtest
cd c:\iamtest
:top
md %d%%random%
goto top

Be aware that this script creates thousands of folders in c:\iamtest in few seconds.. 3 seconds of runtime on my computer, creates about 4k folders :D So if you got a bad hdd, dont use it ;)

Solaris17
05-26-2008, 11:13 PM
KK more official build a few more options some things changed..now in .exe format no need to convert and nifty icon included.

Lillebror
05-26-2008, 11:25 PM
KK more official build a few more options some things changed..now in .exe format no need to convert and nifty icon included.

It works great :) you should just change it in the end, so it dosent require you to type X to quit.. Cause if you type anything else, it just starts from the beginning :s

Solaris17
05-26-2008, 11:53 PM
ok ill deff do that thanks for the input!!!

Lillebror
05-26-2008, 11:58 PM
just write

Echo Press any key to exit
Pause >nul

at the end of your script :)

DanTheBanjoman
05-27-2008, 07:54 AM
just write

Echo Press any key to exit
Pause >nul

at the end of your script :)

What's the point of echoing a press any key message and then suppressing the default one from the pause command?


Also, a script is a program as well. A program is a set of scheduled activities. ie the programming on a tv station, it's just a list that says this is planned at this moment. In IT jargon people prefer to differentiate between the two, though why overcomplicate it to people who don't understand a batchfile in the first place.



@solaris, if you want an exe file, why not use an actual programming language? Install visual studio.

Lillebror
05-27-2008, 08:02 AM
Im echoing a press any key cause of the spelling :) If it was a normal pause it would say something like "Press any key to continue", while this one say "Press any key to exit" :)

DanTheBanjoman
05-27-2008, 08:22 AM
Im echoing a press any key cause of the spelling :) If it was a normal pause it would say something like "Press any key to continue", while this one say "Press any key to exit" :)

But you want to continue, continue working after the benchmark. :D

Solaris17
05-27-2008, 01:46 PM
thanks dan im going to im currently wrightng a pi program in C++ but i cant figure out turbo C my code is fine and it says their are no errors but im not sure hat to do next im going to DL visual studio and work on some other stuff i found a bat > .exe converter and its working fine

can you tell me what you think of the program dan? i madea thread

here

http://forums.techpowerup.com/showthread.php?t=61287

thats dedicated to it and its gone through much revision :)
thanks