Archive for June, 2008

Find the latest or newest modified file in a folder or directory with Perl

#I’ve worked on and off with Perl, but really want to be more proficient with it. I will do more automation with Perl and will share it here for my reader and as a reference for myself. I just created a Perl tag on this blog.
#Here is one script I cooked out today. It takes a directory / folder name as input, and return the latest or newest modified file. Suggestions welcome!

#use File::Copy;
use File::stat;

$dirname = shift or die “Please provide a directory to search for”;
$timediff=0;

opendir DIR, “$dirname”;

while (defined ($file = readdir(DIR)))
{
if($file ne “.” && $file ne “..”)
{
$diff = time()-stat(”$dirname/$file”)->mtime;
if($timediff == 0)
{
$timediff=$diff;
$newest=$file;
}
if($diff<$timediff)
{
$timediff=$diff;
$newest=$file;
}
}
}

print $newest,”\n”;

#copy(”$dirname/$newest”, “c:/work/$newest”);

Comments (2)

Enter Chinese and do msn chat on Ubuntu

Version: Ubuntu 7.10, the Gutsy Gibbon released in Oct. 2007. I run it on VMWare Player.

Assumption: Ubuntu is connected to the web.

Do msn (Windows Live) chat on Ubuntu:

1. Application -> Add/Remove…

2. Click Internet tab on the left pane, then pick aMSN for install. As this writing, it has 4 stars under Popularity column.

3. Launch and enjoy.

By the way, so far I almost never do msn chat, but that can change if/when necessary. Once in a while, I do Skype chat.

Install and set up Chinese entry on Ubuntu, with a hat off to Nathan Sivin for his instruction. I followed his steps and just rehashed them here:

1. System -> Administration -> Language Support. Enter password as needed;

2. In Language scroll window, pick Chinese;

3. Click Apply button;

4. Check “Enable support to enter complex characters”;

5. Click Apply button. When done, close the window;

6. Open a shell, type:

locale | grep LANG=

Record the result because you will need it later. For US based Ubuntu, most likely it will be en_US.UTF-8;

7. Type:

sudo apt-get install scim-qtimm im-switch scim-pinyin

Enter password as necessary;

8. When done, type:

im-switch -z en_US.UTF-8 -s scim

Replace en_US.UTF-8 with whatever you get from step 6;

9. Reboot. For Chinese entry, press

Ctrl and Space bar

You should see a language bar in the lower right-hand corner. On the language bar, click the button immediately to the right of SCIM icon, and pick your Chinese entry methods. Press

Shift

to toggle between English (or whatever your default language) and Chinese. Press

Ctrl and Space bar

again to get rid of the language bar.

Hope this helps you. 希望这可以帮助你。

Comments (2)

Wasteful lifestyle and our environment

Begin rant

Riffing off on the recent reports of merchants starting charging for plastic bags in China and gas price increase all over the world: These days, 9 out of 10 times, I tell the cashier that I don’t need a bag while shopping here in the US. Plastic and paper bags are free here (the common question of “Paper or Plastic” when you check out). I do ask for one when I need it, nothing wrong with that. A close and reliable source has informed me that the Swedes have been charging for plastic bags for a while. Good for them, I say.

IMHO, the general population in the US are living an EXTREMELY wasteful lifestyle: the gas-guzzling SUVs (Sports Utility Vehicle, Hummer, anyone?); the aluminum and plastic cans and bottles; papers and junk mail; stupid and over-the-top product packaging; fast food packaging, disposable plates, chopsticks, forks, and knives; gadgets, laptops, and phones that use different power adapters; batteries; computer waste, the list goes on and on. It baffles me to no end why we behave in such a bone-headed way. In the last year or so, I’ve worked at a client in a western Chicago suburb, a training center in New York City, a financial firm in downtown Chicago. Of all the places, and I’ve looked for them, not a single one has paper/plastic/aluminum recycling receptacles.

There are efforts to curb that waste, but we need to do much more, such as: encouraging the purchase of fuel-efficient vehicles via tax credit or other incentives, remove the subsidy to oil companies and tax their record-breaking revenue for energy research and development, define sensible regulations for less wasteful yet still tasteful packaging, encourage and make it easy to recycle, etc..

So there you have it, my solution to solve environmental issues in the US. When I have time, I will propose solutions to totally resolve China’s pollution problem! In the meantime, go listen to what Chris Waugh has to say on this important subject.

End rant

Comments (3)

List Sql Server jobs and their owners and duration history

use msdb
go
select name, suser_sname(sysjobs.owner_sid) as owner from msdb..sysjobs
go

select sj.name,sjh.run_status,case sjh.run_status
when 1 then ‘Sucess’
when 2 then ‘Retry’
when 3 then ‘Cancelled’
when 4 then ‘In Progress’
when 0 then ‘Failed’
End
as ‘Run Status’,sjh.run_date as ‘Run Date’,
CASE len(run_time)
WHEN 1 THEN cast(’00:00:0′
+ cast(run_time as char) as char (8))
WHEN 2 THEN cast(’00:00:’
+ cast(run_time as char) as char (8))
WHEN 3 THEN cast(’00:0′
+ Left(right(run_time,3),1)
+’:’ + right(run_time,2) as char (8))
WHEN 4 THEN cast(’00:’
+ Left(right(run_time,4),2)
+’:’ + right(run_time,2) as char (8))
WHEN 5 THEN cast(’0′
+ Left(right(run_time,5),1)
+’:’ + Left(right(run_time,4),2)
+’:’ + right(run_time,2) as char (8))
WHEN 6 THEN cast(Left(right(run_time,6),2)
+’:’ + Left(right(run_time,4),2)
+’:’ + right(run_time,2) as char (8))
END as ‘End Time’,
CASE len(run_duration)
WHEN 1 THEN cast(’00:00:0′
+ cast(run_duration as char) as char (8))
WHEN 2 THEN cast(’00:00:’
+ cast(run_duration as char) as char (8))
WHEN 3 THEN cast(’00:0′
+ Left(right(run_duration,3),1)
+’:’ + right(run_duration,2) as char (8))
WHEN 4 THEN cast(’00:’
+ Left(right(run_duration,4),2)
+’:’ + right(run_duration,2) as char (8))
WHEN 5 THEN cast(’0′
+ Left(right(run_duration,5),1)
+’:’ + Left(right(run_duration,4),2)
+’:’ + right(run_duration,2) as char (8))
WHEN 6 THEN cast(Left(right(run_duration,6),2)
+’:’ + Left(right(run_duration,4),2)
+’:’ + right(run_duration,2) as char (8))
END as ‘Duration’ from msdb..sysjobs sj
join sysjobhistory sjh on sj.job_id=sjh.job_id
where name like ‘NameString%’
and sjh.run_date>=’yyyymmdd’
order by run_duration

Comments

坐火车真好

结束了郊区的一个业务,下一个客户在城里,并且离车站很近,所以又开始了坐火车上班的生活。 辞职前的近四年的时间里,我在芝加哥商品期货交易所上班,乘得是同样的火车。现在又碰上了不知姓名的老相识:有的变化大,有的变化小。

那个方脸、戴眼镜的哥们还在,不过胖了很多。他曾带过“弹劾布什”的徽章。我依稀记得一次一起下火车的时候我曾告诉他我喜欢徽章上的信息,现在已经忘掉他的回应了。那徽章圆形,和毛泽东的徽章在硬件设计上没什么差别。

还有那个红头发的清瘦的女子。该女曾怀孕,大腹便便地去上班。现在她的孩子也该有一两岁了吧。

也见到了那个通常在第二排左边的那个亚裔男性,四五十岁的样子。他带宽边眼镜,如不是在美出生的亚裔,想来其来美肯定不短了。由于我通常在第一排就座,面向他,他总是喜欢打量我在读什么书。我读的大部分都是英文书,因中文书不太容易搞到。朕长相平平,属中下等,很有乡土气息,皮肤粗糙黝黑,个也不算高,有愧于“山东大汉”的称号。记得当他第一次见我落座后,看到我从正儿八经的公文包里掏出有点小资情调文化人的英文书时,好像把他震了一下。所以他总喜欢看我在读什么书。

还有一个哥们,严肃,不苟言笑,从外表上看他一年来没大变化。我曾看到他读Ann Coulter的书。该妖女水平寥寥,但著作等身,经常上广播、电视煽动宗教仇恨,蛊惑并嘿唬(土话,吓唬的意思)民众,搞得人心惶惶。估计此女的金发白皮能吸引不少眼球。当然有很多美国人厌恶Ann Coulter之流。

一个曾跟我共事过的老兄很有意思。我们同在芝加哥商品期货交易所上班,但并不认识。我想此人受过不少江湖大片的影响。这哥们通常穿风衣,带墨镜,自我感觉非常良好。有时看到他在月台上踱着方步,火车呼啸而过,车后阵风吹其风衣飞舞,见该兄挥手,顿足,用无线耳麦和同事或喽罗通话,很有运筹帷幄、决胜于千里之外的豪情。

能方便地坐上火车,无堵车之烦耳,无驾驶之劳形。读读小书,写写小文,胡思乱想,岂不快载!

Comments (4)