Read output from VBS in CMD scripts

Here is a sample VB script, test.vbs:
  1. wscript.echo "Testoutput"
  2. wscript.echo "This is a test"
The strings echoed from the VBS script can be read from a calling CMD script, with a standard FOR loop using the options /F and delims as follows, test.cmd:
  1. @echo off
  2. FOR /F "usebackq delims=" %%i IN (`cscript test.vbs`) DO Set myvar=%%i
  3. echo Srcipt result = "%myvar%"
Notice that only the last line echoed from the VB script is stored. If you want to store multiple lines, use the FOR loop token option also. See Get date in Windows CMS script for an example.
Posted in Batch | Leave a comment

Get date in Windows CMD script

Apparently it depends on the Windows installation language, how the date function or %DATE% environment variable is formatted. So here is a very ugly hack to get a julian date (YYYYMMDDHHiiss) in a Windows batch script, independent off system locale settings:

  1. @echo off
  2. FOR /F "usebackq tokens=1-10 delims=+|NOW():- " %%a IN (`mysql -e "SELECT NOW();" -u root -ptest`) DO set datetime=%%a%%b%%c%%d%%e%%f
  3. echo %datetime%

Notice that this requires MySQL. If you do not like to use your MySQL root code, you can create a new user with absolutely no rights granted.

But again, this solution is stupid, I hope you will never need it or use it. I just thought I would share, since it was driving my co-worker crazy, and we ended up using it to name a MySQL dump.

Posted in Batch, MySQL | Tagged , , , , | 2 Comments

Add or remove magic quotes in PHP

When you make a PHP script, but you do not know how PHP is configured on the server the script is being executed on, you might not know whether or not to add or strip quotes from get and post data. Notice that the $_FILES array is not affected by magic quotes.

Here is a script that strips away magic quotes, if they were added:

  1. < ?
  2. if(get_magic_quotes_gpc()) {
  3. function undo_magic_quotes($array) {
  4. return is_array($array) ? array_map('undo_magic_quotes', $array) : stripslashes($array);
  5. }
  6. $_GET = undo_magic_quotes($_GET);
  7. $_POST = undo_magic_quotes($_POST);
  8. $_COOKIE = undo_magic_quotes($_COOKIE);
  9. $_REQUEST = undo_magic_quotes($_REQUEST);
  10. }
  11. ?>

And likewise, here is a script that adds magic quotes, if they were not already applied:

Continue reading
Posted in PHP | Tagged , , , , | Leave a comment

Force pure HTML editor in WordPress

After installing WordPress for this blog, I realized that I was forced to use the stupid TinyMCE WYSIWYG editor. I smiled when i found out that I was able to set a HTML editor only flag in my account settings. But this did not turn the editor into a pure HTML editor, since every line i wrote was replaced by <p>{the_line}<p>, which is not practical when you want to paste programming code.

Here is how you disable this unacceptable behavior the easy way (WP kindly asks you to find a plugin that undo the paragraphs, but you know…).

Continue reading
Posted in HTML, PHP | Tagged , , | Leave a comment

Select count from multiple tables

Sometimes a parent table have more than one table with multiple references to a field in the parent table. Then the question is how to count references from more than one table in a single query. I have found 5 different ways to achieve this result, and therefore I have run a few tests, to see which was better.

To test this I made this small PHP script, that creates a parent table and child table. Then a loop populates the tables with 1,000 parent rows, approximately 15 children of type 1 and approximately 75 children of type 2. This is done before each query, to make sure that all queries have the same conditions.

Continue reading
Posted in MySQL, PHP | Tagged , , , , , | 2 Comments

Foreign keys and MySQL (errno: 150)

Today I tried to execute the SQL

mysql> ALTER TABLE parent_child ADD FOREIGN KEY (parent_id)
	REFERENCES parent(id) ON DELETE CASCADE;

in MySQL, and got the error

Can't create table 'db.#sql-aa1_1' (errno: 150)

I tried to look up error number 150 on the internet, but found many reasons why error no. 150 could occur; none was my case. Common mistakes that causes this error are:

Continue reading
Posted in MySQL | Tagged , , , | Leave a comment

Only a few short .dk domains left

Hello world! This blog is simply named 5p, because it was one of the few domains left with less than three characters. I chose 5p among these, since p can stand for many good things, for example: Professionalism, Problemsolving, Planning, Procedures or Performance. There are actually 3,205 different words that starts with the letter p in the danish language, without conjugations.

Actually 5p was the only one left out of 6 domains. Unfortunately it is not easy to search for domain names, so to make this conclusion i made what some might call a “dictionary attack” at the .dk provider. I simply calculated all combinations of danish domains with 2 characters and looked them up, with a small php script i executed in a cmd prompt. The other 5 domains left was (notice that the first two starts with zero, not the letter o):

Continue reading
Posted in PHP | Tagged , , , | 1 Comment