Wednesday, March 29, 2006

Directory management in... batch files!

Ok, consider it the only way I found to look original: It's 2006 and you're reading about cmd.exe and batch files tricks ;-)

I usually use JScript WSH scripts to automate tasks but occasionally batch files simply remain the best choice. It's especially true when what you need to do mostly consist in running a sequence of command-line programs.

I recently wrote a set of batch files that run programs located in the same directory (or in sub-directories, or in sibling directories. Whatever.) and the data files they manipulate are also in this directory. Pretty straightforward : use no paths (or relative paths) when specifying the programs and files locations.

The problem...

The problem is you assume that the current working directory (CWD) is the directory where your batch file is located.

How to make sure the current working directory is correctly set ?

One way is to invoke the batch files using a shortcut: You can set the directory in there.
Hmm... Not very general a solution! What I want is to be able to run the batch file by double-clicking it in Explorer or by typing its full path in a command prompt.

Let's look for something better, such as maybe batch files argument specifier %0. No, not good: It yields the name of the batch file but not the path.

The solution

Here's something better: %~dp0. This converts to the (drive and directory) path of the batch file. Kewl! We can now add this statement at the top of our batch file to set the current directory:

cd %~dp0
Not bad. But there's still a problem: if you start from another drive, cd won't change the current drive letter. You'll want to use the /d (d stands for drive) to fix this issue:

cd /d %~dp0
Note that %~dp0 contains a trailing backslash. Therefore, you can use it in such combinations

cd /d %~dp0MySubDir
This sets the CWD as sub-dir MySubDir of the directory where the batch file is located.

Did you receive a good education ?

If yes, you'll want to set the CWD back to its original value when you're done.

Solution1 (bad. Think of the mess when you have nested batch files.):

SET OriginalDir=%CD%
cd /d %~dp0
DoStuff
cd /d %OriginalDir%
%CD% is a pseudo environment variable that yields the curent directory.

Solution 2 (Much nicer):

pushd %~dp0
DoStuff
popd
pushd pushes the CWD and switches to the specified one (including drive letter).
popd restores the previously pushed directory.
Neat! No temp environment variables. Works like a charm with nested batch files.

Nested batch files

Just a reminder: don't forget to use call to invoke a batch file from another one. The trap is that you don't have to use call to execute another batch. But if you don't, the rest of the caller file will not be executed :-(

Note to Self

MONAD MSH. Will I really wait another year before giving it a look...

Thursday, March 23, 2006

Should you charge VAT ? To whom ?

I just can't believe it: I just bought some software from an ISV that uses Digital River as their e-commerce provider. It appears that Digital River doesn't get it right about whether they should charge VAT to me. What p... me off is that they charged me 17.5% VAT even though they shouldn't have to. Not such a big deal: I'll put it on my 3-monthly VAT declaration, which appears to be filled in early April. But still: It's like I lent them the money. Wait! Maybe this is the reason: They just want to make my money work for them for a few days or weeks and then say 'Oops! Sorry, our bad, here's the money back'. Giving the amount of business they do, I guess the profit might be interesting EVEN THOUGH ILLEGAL.

Come on! Even a moron like me (at least as far as accounting is concerned ;-) knows the rule:

You should charge VAT if:

1. You:
You are a registered EU company or individual, hence you have a VAT id (or whatever it's called in your country). If you are not located in the EU, you never charge VAT. If you are in EU but are not registered, you most likely don't sell anything on a professional base (or you are in a special field of activity (such as insurance) and I have no idea what kind of taxes you should or should not charge).

AND

2. Your client is either:
a) a non-registered EU individual or company (someone who doesn't have a VAT id. Typically a consumer, as opposed to a professional user). Note: Most EU companies do have a VAT id. There are some exceptions to the rule though such as government agencies, bankers and insurance agents (at least it's the case in Belgium).
or
b) any individual or company residing in your own country.

What rate should you charge ?

The rate in use in your own country.
e.g.: rate for software goods in Belgium = 21%

How do you know for sure your foreign EU client is a registered entity ?

That one is easy: They MUST give you their VAT id during the sales process.

BTW, Digital River never asked me for my VAT id during the purchase process! Other sites (such as ShareIt, which I use for appTranslator sales) do it and handle VAT the right way.

An example

Let's see who pays VAT when purchasing appTranslator.

1. You are American. Or Korean. Or Japanese. Or Canadian. Or Australian. I won't charge you VAT since you (or your company) is not located in the EU.

2. You work for a German company. I won't charge you VAT since you give me theVAT id of your company (assuming the company pays for the software) during the purchase process.

3. You are an Italian professional developer or translator. I won't charge you VAT since you give me your VAT id during the purchase process.

4. You are a French hobbyist developer. Sorry, you'll be charged 21% VAT.

5. You work in a Belgian company. You'll be charged 21% VAT (because my company is based in Belgium too). Of course, since you have a VAT id, you'll get the money back from the Belgian government when you declare the purchase on your VAT form.

You see, it's not that complicated. Which leads back to the question I asked earlier: What's the reason that leads Digital River to do it the wrong way ? I bet it's one they are not willing to confess...

Wednesday, March 22, 2006

IE Developer Toolbar

On its way to try to catch up with Firefox, IE now features a Developer Toolbar that gives immediate access to a set of fairly cool tools to debug your Web pages, including a DOM explorer that looks much like the one in Firefox (best used with a double monitor), tools to outline the different parts of your pages, to clear the cache,...

I've been playing with the beta1 for some time and it has showed pretty useful. The Beta2 has been available here since yesterday. I'll install it later today.

Friday, March 10, 2006

What will you drive when you're a billionaire ?

Need some advise ? You might want to ask your new friends' opinion.
Pretty disappointing, I must say...

Wednesday, March 01, 2006

Cyrillic encoding

Here's the story:

a Russian girl asked her French friend to send her a book. She sent him her address in an email. The friend put the book in an envelope and wrote her Russian friend's address on the envelope. Which is not an easy task because the address is spelled in Cyrillic. And Cyrillic is a strange animal: It consists only in vowels. And since you don't go very far with a few vowels, Russians use a lot of accents (diacritic marks) to differentiate them. Pfeewww! How difficult a language!

http://community.livejournal.com/velik_moguch/242083.html

(through roxfan)

"Une fusion utile pour la chute des prix"

Au chapitre de la saga Suez-GDF: une interview de Jean-Pierre Hanssens dans Le Soir:

Une poignée de pays maîtrisent les réserves mondiales de gaz naturel. Face à eux, il y a un nombre illimité de fournisseurs. Il est logique que si les acheteurs sont plus concentrés, ils auront un pouvoir de négociation accru face aux vendeurs.

Oups! A lire le titre, je pensais qu'il s'agissait du prix payé par le consommateur...