tags:

views:

396

answers:

6

Hi,

I tried everything possible, but still failed. I thought I got it at the point which I'll post
as my final attempt, but still isn't good [enough].

A script is being passed three arguments. Domain name, username and password.
But the probles is that I need domain separated in "domain" + ".com" format. Two variables.
I tried to split it using name.extension cheat, but it doesn't work quite well.

Check the simple code:

@echo off
echo.
set domain=%~n1
set ext=%~x1
echo %DOMAIN%
echo %EXT%
echo.

When you try it, you get:

D:\Scripts\test>test.bat domain.com

domain
.com

D:\Scripts\test>test.bat domain.co.uk

domain.co
.uk

First obviously does work, but only because I'm able to cheat my way through.
String operations in DOS Shell are a pain in the ass. I might be able to convince
a script writer to pass me 4 arguments instead of 3... but in case that fails... HELP!

+1  A: 

Even if you could get the batch file commands to be greedy (i.e. pick up domain/.co.uk) you'd still get it wrong some of the time. Consider www.domain.co.uk: you'd end up with www/domain.co.uk.

What are actually trying to do?

Roger Lipscombe
A: 

Automatize website creation :)

www.domain.com is not an option. First script would always pass domain without www.

BTW, author of it will feed me four parameters as needed! YAY!

But still, I'd like to see if it's possible to do this kind of string manipulation using pure DOS command prompt. It seems highly unlikely, tho...

A: 

If you want to automatize something (as stated in another answer), my solution would be to use appropriate tools. Install a Perl runtime or something else you're comfortable with. Or use the Windows power shell

Also, unless you supply your script with a list of valid top level domains, there is NO WAY, in no language, that your script can decide whether test.co.uk should be splitted as text and co.uk or test.co and uk. The only feasible possibility would be to make sure that you get only second-level-domains without sub-domain parts. Simply split at the first dot in that case.

BTW: I'm curious to why you would want to automate website creation in a Windows shell script. You aren't doing anything nasty, are you?

sirprize
A: 

You are correct. I could do that in another programming language, but this all started as a bunch of shell commands for easier management, and grew quite large now. It would take more time than I can provide to make a swith to, say, Perl.

I'm not doing anything nasty, do not worry :) Just trying to help myself with automating certain processes :)

What I thought command prompt can do is to find a first dot in the domain name and separate a string into two at this place. Both top level and second level domains would be separated properly.

Like you said 'simply split at the first dot in that case'... but not sure Windows shell script can do that :)

Thanks for your thoughts!

A: 

Windows ships with the Windows Scripting Host which lets you run javascript.

Change the batch file to:

@echo off
cscript //Nologo test.js %*

Create test.js:

if (WScript.Arguments.Length > 0) {
    var arg = WScript.Arguments.Item(0);
    var index = arg.indexOf('.');
    if (index != -1) {
     var domain = arg.substring(0, index);
     var ext = arg.substring(index);
     WScript.Echo(domain);
     WScript.Echo(ext);
    } else WScript.Echo("Error: Argument has no dots: " + arg);
} else WScript.Echo("Error: No argument given");

And you can use it:

C:\Documents and Settings\Waqas\Desktop>test.bat domain.com
domain
.com

C:\Documents and Settings\Waqas\Desktop>test.bat domain.co.uk
domain
.co.uk

And that does what I think you wanted.

waqas
A: 

You did amazing job!
It would never occur to me I could use JS.
Not that I know the code, but still I was too narrowminded ;-)

Thanks for all your help and advices people, and thank you waqas for the ultimate solution!