Welcome Guest.   Make a donation to an author on the site October 25, 2014, 08:27:22 AM  *

Please login or register.
Or did you miss your validation email?


Login with username and password (forgot your password?)
Why not become a lifetime supporting member of the site with a one-time donation of any amount? Your donation entitles you to a ton of additional benefits, including access to exclusive discounts and downloads, the ability to enter monthly free software drawings, and a single non-expiring license key for all of our programs.


You must sign up here before you can post and access some areas of the site. Registration is totally free and confidential.
 
The N.A.N.Y. Challenge 2010! Download 24 custom programs!
   
   Forum Home   Thread Marks Chat! Downloads Search Login Register  
Pages: [1] 2 Next   Go Down
  Reply  |  New Topic  |  Print  
Author Topic: DOS Batch Functions Tutorial  (Read 6838 times)
justice
Supporting Member
**
Posts: 1,889



Solve issues simply.

View Profile WWW Give some DonationCredits to this forum member
« on: June 16, 2011, 09:44:03 AM »

As part of the development of an app, I'm doing the build process using the batch file. I might write a bit more about this at a later date. As the batch file grew I was wondering if it could do functions, to avoid using the same code multiple times. This page is a really good resource (http://www.dostips.com/DtTutoFunctions.php):






Logged

Renegade
Charter Member
***
Posts: 11,639



Tell me something you don't know...

see users location on a map View Profile WWW Give some DonationCredits to this forum member
« Reply #1 on: June 16, 2011, 10:06:48 AM »

You might want to look into PowerShell as it has largely replaced batch files for a lot of admins.

But, batch files do have their appeal still! (I use some batch commands in build processes and whatnot.) Those look like some nice tuts! smiley
Logged

Slow Down Music - Where I commit thought crimes...

Freedom is the right to be wrong, not the right to do wrong. - John Diefenbaker
daddydave
Supporting Member
**
Posts: 818



see users location on a map View Profile Give some DonationCredits to this forum member
« Reply #2 on: June 16, 2011, 11:13:03 AM »

See http://www.donationcoder....m/index.php?topic=22441.0

EDIT: Sorry the above link was a bit off topic. You didn't really ask for other batch file resources, did you? I will check out the link you gave. It's very handy to be able to use functions in batch files. Thmbsup

One big advantage of batch files is you don't have to install anything, so you can use them in restricted environments (i.e., work).
« Last Edit: June 16, 2011, 12:57:12 PM by daddydave » Logged
IainB
Supporting Member
**
Posts: 4,791


Slartibartfarst

see users location on a map View Profile Give some DonationCredits to this forum member
« Reply #3 on: June 17, 2011, 12:41:10 AM »

IMHO, the best thing to do with DOS batch commands is avoid using them.
Try Take Command from JP Software.
I used to use the precursor to Take Command, called 4DOS. It was superb. I gather from Wikipedia that 4DOS is pseudo public domain now.
Logged
mwb1100
Supporting Member
**
Posts: 1,330


View Profile Give some DonationCredits to this forum member
« Reply #4 on: June 17, 2011, 02:15:50 AM »

One big advantage of batch files is you don't have to install anything, so you can use them in restricted environments (i.e., work).

That's the only advantage.  But it's a compelling one.

The book "Windows NT Shell Scripting" by Timothy Hill has about the best coverage of how to do complex processing with batch files that I've come across (get a used/remaindered one for the cost of shipping).

But I'd avoid doing anything too complex with batch files unless you really, really have to.  There are so many far better tools available (and most of them free).  But you do usually have to make sure they're installed (or that a runtime is) if the script you're coming up with has to be able to run on machines you don't control.

AutoHotKey (and friends) is one of the exceptions as I understand it - build the script to an exe and nothing else is needed on the end-user's machine (I imagine that's one of the reasons it's quite popular on DC's coding snacks).
Logged
ewemoa
Honorary Member
**
Posts: 2,482



View Profile Give some DonationCredits to this forum member
« Reply #5 on: June 17, 2011, 03:14:57 AM »

I found the following section tricky:

  Returning Local Variables - How to pass return values over the ENDLOCAL barrier
Logged
ewemoa
Honorary Member
**
Posts: 2,482



View Profile Give some DonationCredits to this forum member
« Reply #6 on: June 17, 2011, 04:00:31 AM »

The book "Windows NT Shell Scripting" by Timothy Hill has about the best coverage of how to do complex processing with batch files that I've come across (get a used/remaindered one for the cost of shipping).

FWIW, Chapter 5 appears to be online.

Quote from: mwb1100
AutoHotKey (and friends) is one of the exceptions as I understand it - build the script to an exe and nothing else is needed on the end-user's machine (I imagine that's one of the reasons it's quite popular on DC's coding snacks).

If I may digress a bit... smiley

Curious who you regard as "friends" -- AutoIt3 perhaps?  Any others?

Some things that I found attractive about AHK / AutoIt3 included:

  • Conditions of use (e.g. free to obtain and produced output freely usable)
  • Relatively small resulting exe
  • Actively developed with a community of users
  • Decent selection of reusable code / libraries

For the things I checked some months ago, other things didn't seem to match these  -- e.g. as I understand it, it's possible to create single exes for Perl and Python, but I don't think the results end up nearly as small as for AHK / AutoIt3.  IDLE (based on Lua) seemed to produce relatively small exes, but the last time I checked there didn't seem to be much of a community and development didn't seem terribly active.  Not sure about Factor yet -- still need to see how big the resulting exes are.

Curious to hear others thoughts on this topic...perhaps another topic / thread is in order...
Logged
justice
Supporting Member
**
Posts: 1,889



Solve issues simply.

View Profile WWW Give some DonationCredits to this forum member
« Reply #7 on: June 17, 2011, 04:08:34 AM »

I was looking to discuss the very same topic ewemoa.
Logged

oblivion
Supporting Member
**
Posts: 354


see users location on a map View Profile Read user's biography. Give some DonationCredits to this forum member
« Reply #8 on: June 17, 2011, 07:47:11 AM »

I was looking to discuss the very same topic ewemoa.
I've been using a little freebie called BatchCompiler for some years, as a way to lock down batchfiles against casual modification and ensure maximum portability. (Can't remember where I got it now, and the readme only quotes the author's name -- Ricardo Arias -- but hey, Google is your friend.)

There are batchy things that it doesn't do well -- can't remember for sure, now, but CALLs might be one of those things -- but there are usually more than one way to skin the relevant cat and I've never found another batch compiler that worked as well.

Add in something like Reshacker so you can plug in your icon of choice and Tah Dahhh!
Logged

-- bests, Tim

...this space unintentionally left blank.
ewemoa
Honorary Member
**
Posts: 2,482



View Profile Give some DonationCredits to this forum member
« Reply #9 on: June 17, 2011, 04:16:59 PM »

I've been using a little freebie called BatchCompiler for some years, as a way to lock down batchfiles against casual modification and ensure maximum portability. (Can't remember where I got it now, and the readme only quotes the author's name -- Ricardo Arias -- but hey, Google is your friend.)
Apparently, I wasn't Google's really good friend today Sad

But I came across the following instead on a thread that mentioned BatchCompiler:

  Bat To Exe Converter

Haven't tried it yet though.
Logged
justice
Supporting Member
**
Posts: 1,889



Solve issues simply.

View Profile WWW Give some DonationCredits to this forum member
« Reply #10 on: June 18, 2011, 05:30:44 AM »

What's the point of making them exe files.
Formatted for Autohotkey with the GeSHI Syntax Highlighter [copy or print]
  1. SetWorkingDir %A_ScriptDir%  
  2. BatchFile = test.bat
  3. FieInstall, %BatchFile%,%A_Temp%
  4. RunWait, %A_Temp%\%BatchFile%,%A_WorkingDir%
  5. FileDelete, %A_Temp%\%BatchFile%
Logged

ewemoa
Honorary Member
**
Posts: 2,482



View Profile Give some DonationCredits to this forum member
« Reply #11 on: June 18, 2011, 05:49:54 AM »

Never figured out how to have a batch file have a custom icon, whereas this is possible for an exe.
Logged
oblivion
Supporting Member
**
Posts: 354


see users location on a map View Profile Read user's biography. Give some DonationCredits to this forum member
« Reply #12 on: June 18, 2011, 02:31:10 PM »

Quote
Quote
I've been using a little freebie called BatchCompiler for some years, as a way to lock down batchfiles against casual modification and ensure maximum portability. (Can't remember where I got it now, and the readme only quotes the author's name -- Ricardo Arias -- but hey, Google is your friend.)
Apparently, I wasn't Google's really good friend today Sad
Obviously I should check these rash statements before I make them.

My copy of BatchCompiler's on a machine I'm not going to be anywhere near for another week, but if you want a look I've definitely got the original distribution zipfile on there, so if you don't find it beforehand, let me know your email privately and I'll get it to you sometime on the 27th. It's quite old, from memory, maybe 2003, so I guess the author decided he wasn't going to carry on supporting it...
Logged

-- bests, Tim

...this space unintentionally left blank.
ewemoa
Honorary Member
**
Posts: 2,482



View Profile Give some DonationCredits to this forum member
« Reply #13 on: June 18, 2011, 04:24:49 PM »

Thanks for the offer  smiley

Perhaps I'll attempt another round at searching, but I start to wonder whether it's such a wise idea for me to make a habit out of using a tool which doesn't appear to be maintained...hmmm...
Logged
IainB
Supporting Member
**
Posts: 4,791


Slartibartfarst

see users location on a map View Profile Give some DonationCredits to this forum member
« Reply #14 on: June 18, 2011, 09:24:19 PM »

@oblivion
@ewemoa:                Re: Batch Compiler.

From memory, there was a batch compiler in 4DOS (I referred to 4DOS in a post above), but I never actually needed to use it except to try it out, so cannot comment on it.
I did some googling using "Batch Compiler Ricardo Arias" search terms. I did find links/references to those terms - mostly in the Spanish language - but no download source for the Batch Compiler.
On one site Purebasic.com there was this "user opinion":
Quote
Ricardo Arias, Argentina
"I come from Visual Basic.
Then I was searching for something small, with no runtimes, speed and easy too.
Maybe PureBasic doesn't have that quantity of sourcecode available, but a great community that always help each other."
(By the way, reading about it on that site, I thought that PureBasic looked like a great Basic language + compiler. I might try it out,)     smiley

I also found this link: a Batch to EXE Converter on F2K0.de
You can download it from there, but it's probably not the Batch Compiler from Ricardo Arias.
Logged
oblivion
Supporting Member
**
Posts: 354


see users location on a map View Profile Read user's biography. Give some DonationCredits to this forum member
« Reply #15 on: July 04, 2011, 05:45:29 AM »

[
From memory, there was a batch compiler in 4DOS (I referred to 4DOS in a post above), but I never actually needed to use it except to try it out, so cannot comment on it.
I remember looking into it -- I used and loved 4DOS for years -- but it was a separate license fee, particularly if you wanted to distribute things you'd made with it, I think.

I have now found the source zip for Batchcompiler. If anyone wants it, PM me and I'll email it. (The zipfile is only 14.6k)
Logged

-- bests, Tim

...this space unintentionally left blank.
IainB
Supporting Member
**
Posts: 4,791


Slartibartfarst

see users location on a map View Profile Give some DonationCredits to this forum member
« Reply #16 on: July 15, 2011, 09:57:44 PM »

Batch Compiler 1.0.1 By Ricardo Arias
Oblivion kindly emailed me the batch compiler and I have put it in box.net for downloading from here: BatchCompiler.zip
Logged
kyrathaba
N.A.N.Y. Organizer
Honorary Member
**
Posts: 3,010



while(! dead_horse){beat}

see users location on a map View Profile WWW Read user's biography. Give some DonationCredits to this forum member
« Reply #17 on: August 07, 2011, 03:19:12 PM »

Thanks.  Downloaded this!
Logged

Win 7 Home Premium 64bit-SP1 AMD Athlon II X2 220 Socket AM3 (938) @ 2.1GHz 6GB RAM Firefox 26.0
_________________________________________________________________________________________

I'm fighting against patent trolls. Join me and tell your representative to support the #SHIELDAct: https://eff.org/r.b6JJ /via @EFF

My DC page: http://kyrathaba.dcmembers.com | My blog: http://williambryanmiller.com/ | Proofreading Service: http://bit.ly/1fQSqQP

db90h
Coding Snacks Author
Charter Member
***
Posts: 455


Software Engineer

View Profile WWW Read user's biography. Give some DonationCredits to this forum member
« Reply #18 on: August 07, 2011, 04:04:41 PM »

I guess nobody has taken to PowerShell yet, eh? Wink. It is quite powerful, though I never thought a command line scripting shell could consume so many resources, lol. It is one massive beast. I dunno if anyone, even network engineers, have 'taken' to it yet.

Personally, I love Cygwin on Windows. Let's me have linux style scripting in Windows. Of course, I still use plenty of BATCH file for automation to this day.
Logged
worstje
Honorary Member
**
Posts: 555



The Gent with the White Hat

View Profile Give some DonationCredits to this forum member
« Reply #19 on: August 07, 2011, 04:41:16 PM »

I'd love to learn / get a hang of PowerShell. The problem is I have no clue what I can do with it that will make a time investment worth it, and I personally would probably be best served a set of ready-made scripts and examples that just let me be productive right off the bat. smiley

Back on topic-ish now... Batch files I haven't really needed in years, and the looping facilities in it just make me cringe. Python usually sees to all my needs in that sense.
Logged
db90h
Coding Snacks Author
Charter Member
***
Posts: 455


Software Engineer

View Profile WWW Read user's biography. Give some DonationCredits to this forum member
« Reply #20 on: August 07, 2011, 04:50:41 PM »

Yes, BATCH files weren't designed for complex scripting at all. Not like *nix shells, which were designed that way from the start. I still prefer *nix.

PowerShell, despite it being a resource hog like you wouldn't imagine, is very powerful. It can let you script almost anything you could code in .NET, minus all the UI stuff maybe. At least, that's my understanding of it. I have not taken the time to learn, nor care to learn, the internals of it either. Whether the product even survives remains to be seen. I'm sure some network admins are using it for some automation tasks, but that's about it. Nobody really took to it. They'd do better off pushing their POSIX subsystem on admins, and give them an interface common to Windows and linux. Of course, the *nix style scripting came from decades ago and isn't the most efficient thing in the world, but with the help of numerous facilitating tools you can do pretty much anything you want. PowerShell could have been much more efficient, and might be for large and very complex scripts, though its .NET MSIL overhead makes it slow .. so who knows on that one. They *did* include some unix commands in PowerShell, ironically.. at least named the same, different options and output format -- just to confuse people.
Logged
kyrathaba
N.A.N.Y. Organizer
Honorary Member
**
Posts: 3,010



while(! dead_horse){beat}

see users location on a map View Profile WWW Read user's biography. Give some DonationCredits to this forum member
« Reply #21 on: August 07, 2011, 08:32:16 PM »

Quote
though its .NET MSIL overhead makes it slow ..


Ah, but slow relative to what?  I can't imagine PowerShell programs being speed-critical.
Logged

Win 7 Home Premium 64bit-SP1 AMD Athlon II X2 220 Socket AM3 (938) @ 2.1GHz 6GB RAM Firefox 26.0
_________________________________________________________________________________________

I'm fighting against patent trolls. Join me and tell your representative to support the #SHIELDAct: https://eff.org/r.b6JJ /via @EFF

My DC page: http://kyrathaba.dcmembers.com | My blog: http://williambryanmiller.com/ | Proofreading Service: http://bit.ly/1fQSqQP

IainB
Supporting Member
**
Posts: 4,791


Slartibartfarst

see users location on a map View Profile Give some DonationCredits to this forum member
« Reply #22 on: September 09, 2011, 11:29:47 PM »

Rather than a DOS batch functions tutorial, would it be of more current relevance to use Window Scripting?
There's an interesting post about it here: Better Than Batch: A Windows Scripting Host Tutorial

Just a thought.
Logged
kyrathaba
N.A.N.Y. Organizer
Honorary Member
**
Posts: 3,010



while(! dead_horse){beat}

see users location on a map View Profile WWW Read user's biography. Give some DonationCredits to this forum member
« Reply #23 on: September 10, 2011, 11:54:21 AM »

+1.

I think that, in general, WS would be preferable, although there might be so esoteric situations where a small batch file might be preferable.
Logged

Win 7 Home Premium 64bit-SP1 AMD Athlon II X2 220 Socket AM3 (938) @ 2.1GHz 6GB RAM Firefox 26.0
_________________________________________________________________________________________

I'm fighting against patent trolls. Join me and tell your representative to support the #SHIELDAct: https://eff.org/r.b6JJ /via @EFF

My DC page: http://kyrathaba.dcmembers.com | My blog: http://williambryanmiller.com/ | Proofreading Service: http://bit.ly/1fQSqQP

IainB
Supporting Member
**
Posts: 4,791


Slartibartfarst

see users location on a map View Profile Give some DonationCredits to this forum member
« Reply #24 on: September 11, 2011, 01:26:42 AM »

@kyrathaba: I didn't know anything about Windows Scripting until I read that post.
I am not sure, but maybe this is what @mwb1100 was referring to when he wrote in the above thread:
Quote
The book "Windows NT Shell Scripting" by Timothy Hill has about the best coverage of how to do complex processing with batch files that I've come across (get a used/remaindered one for the cost of shipping).

@justice: Could this (Windows Scripting) be a potential alternative answer to what your original Q was about? (I ask the question in gnorance as to what your full requirements might be or why they might have necessitated the use of DOS batch in the first place.)
Logged
Pages: [1] 2 Next   Go Up
  Reply  |  New Topic  |  Print  
 
Jump to:  
   Forum Home   Thread Marks Chat! Downloads Search Login Register  

DonationCoder.com | About Us
DonationCoder.com Forum | Powered by SMF
[ Page time: 0.064s | Server load: 0.07 ]