Friday, April 29, 2011

Useful Batch Commands

Sometimes it's useful to have a few batch tricks up your sleeve. Here are two that I've used before but I had to dig around to remember/find.

The first trick is running a script file on Windows like it's executable. This is easy to do on *nix, but on Windows it requires a .bat file. So just create a .bat file the same name as the script, in this case script.php, put the two in the same directory, and then use the cryptic \$~dp0 to specify the current directory of the .bat file being run and viola! The script is now an executable.

```php %~dp0script.php
```

The second is I wanted to copy a text file to a network drive but CMD doesn't understand UNC paths. So use pushd and popd. Nothing revolutionary but useful if you need it.

```pushd \\computer\volume\
copy %~dp0\filetocopy.txt filetocopy.txt
popd
```