Unix-like ls command in PowerShell

by j2.nete 14. December 2011 15:31

PowerShell is powerful tool in Windows.  But the ls command is little bit sucks.  Following is my own ls command which write for PowerShell.

==Code======================================================

#Owen ls command
Remove-Item alias:\ls
function ls {
  $ps = Get-Location
  $cmd = "dir " + $args
  if(!($ps.Drive.Provider.Name -match 'FileSystem')){
    return (Invoke-Expression $cmd)
  }
  # defines
  $COLUMN = 3
  $exes = '.BAT', '.EXE', '.MSI'
  $zips = '.7Z', '.ZIP', '.GZ', '.TAR', '.RAR', '.CAB'
  
  $MAX = $Host.UI.RawUI.BufferSize.Width/$COLUMN - 1
  $index = 0
  foreach ($file in (Invoke-Expression $cmd)) {
    $name = $file.Name
    $isDir = ($file -is [System.IO.DirectoryInfo])
    if($name.Length -gt $MAX-2){
      $name = $name.SubString(0, $MAX - 5)
      $name += '...'
    }
    if($isDir) {
      $name = '[' + $name + ']'
      $clr = [ConsoleColor]::Cyan
    } else {
      if($exes -contains $file.Extension) {
        $clr = [ConsoleColor]::Green
      }elseif($zips -contains $file.Extension){
        $clr = [ConsoleColor]::Magenta
      }else{
        $clr = [ConsoleColor]::White
      }
    }
    
    if($file.Attributes.ToString() -match 'ReadOnly'){
      $clr = [ConsoleColor]::Yellow
    }
    
    if($file.Attributes.ToString() -match 'Hidden'){
      $clr = [ConsoleColor]::DarkGray
    }
    
    if($file.Attributes.ToString() -match 'System'){
      $clr = [ConsoleColor]::Red
    }
    
    # output
    if($name.Length -lt $MAX){
      $name += (' '*($MAX-$name.Length))
    }
    
    write-host $name -n -f $clr
    
    $index+=1
    if ($index % $COLUMN -eq 0) {write-host}
  }
  
  return ''
}

==Screenshot==================================================

Tags: ,

Labs

Open two explorers side by side via python

by j2.nete 17. September 2011 12:05

 

Open two explorers side by side via python.  Anyway, very useful for me.

python 2.7.2,  python win32 extension

windows 7

===================================================

  1. from win32gui import EnumWindows, GetClassName, FindWindow, SetWindowPos, GetDesktopWindow, GetWindowRect
  2. import os, time, sys, argparse
  3.  
  4. screen = 0
  5. tbar = 1
  6. paths = ['c:\\', 'c:\\']
  7.  
  8. def main():
  9.   global screen, tbar, paths
  10.   parser = argparse.ArgumentParser(description='Open 2 explorer side by side. (j2.nete(at)gmail.com)')
  11.   parser.add_argument('paths', metavar='path', nargs='*', help='The initilize paths of 2 explorer.')
  12.   parser.add_argument('-s', '--screen', nargs='?', type=int, choices=[-1,0,1], default=0, help='Screen want to show the explorers; -1 is left screen, 0 is default screen, 1 is right screen.')
  13.   parser.add_argument('-t', '--tbar',   nargs='?', type=int, choices=[1,0],    default=1, help='Minus taskbar area or not.')
  14.   args = parser.parse_args(sys.argv[1:])
  15.   if args.paths : paths = args.paths
  16.   screen = args.screen
  17.   tbar = args.tbar
  18.  
  19. def enumWindowsProc(hwnd, list):
  20.   if GetClassName(hwnd) == 'CabinetWClass':
  21.     list.append(hwnd)
  22.  
  23. def getExplorers():
  24.   list = []
  25.   EnumWindows(enumWindowsProc, list)
  26.   return list
  27.  
  28. def showWindow(hwnd, size):
  29.   SetWindowPos(hwnd,-1,size[0],size[1],size[2],size[3],0x40)
  30.   SetWindowPos(hwnd,-2,size[0],size[1],size[2],size[3],0x40)
  31.  
  32. def calcRect(r1, r2):
  33.   return(r2[1]>r1[0] and r2[1]<r1[1] and r2[1] or r1[0], r2[0]>r1[0] and r2[0]<r1[1] and r2[0] or r1[1])
  34.  
  35. def getAllSize():
  36.   (dl, dt, dr, db) = GetWindowRect(GetDesktopWindow())
  37.   if tbar:
  38.     (tl, tt, tr, tb) = GetWindowRect(FindWindow('Shell_traywnd', None))  
  39.    
  40.     (top, bottom) = calcRect((dt, db), (tt, tb))
  41.     (left, right) = calcRect((dl, dr), (tl, tr))
  42.     return(left, top, right-left, bottom-top)
  43.   else:
  44.     return(dl, dt, dr, db)
  45.  
  46. def openExplorer(path):
  47.   os.system('explorer.exe ' + path)
  48.  
  49. if __name__ == "__main__":
  50.   main()
  51.  
  52. wlist = getExplorers()
  53. if len(wlist) < 2:
  54.   if len(wlist) == 0:
  55.     openExplorer(paths[0])
  56.     while FindWindow('CabinetWClass',None) != 0:
  57.       time.sleep(0.2)
  58.   openExplorer(paths[1])
  59.   while len(wlist)<2:
  60.     wlist = getExplorers()
  61.     time.sleep(0.2)
  62.   time.sleep(1)
  63.  
  64. (x, y, w, h) = getAllSize()
  65. x += screen*w
  66.  
  67. showWindow(wlist[0],(x,y,w/2,h))
  68. showWindow(wlist[1],(x+w/2,y,w/2,h))
  69.  

Tags: ,

Labs

遗址公园偶得

by j2.nete 21. April 2011 14:24

遗址公园的早晨,恬静.晨练的人是有些熙攘的.小女孩,绝不肯撒开妈妈的手,踌躇着.铃声划破了平静.像是风铃,像是清晨的交响.青年在奔跑,肆意甩动着腰间的一串钥匙,如痴如醉.光影交错,仿佛间恍如隔世.小女孩早跑开了,回头望着.像在说:长大了,我也要挂一大串钥匙.还要指甲剪和耳屎勺.妈妈笑了,哼起了"驼铃".

Tags:

Life | Twitter

动漫歌曲三首

by j2.nete 17. October 2010 23:10

秒速五厘米中<One more time, One more chance>

及其不错

银魂中<万事屋Blues>

非常不错

猫的报恩中<幻化成风>

真的不错

Tags: ,

Twitter

2010-10-12

by j2.nete 12. October 2010 10:11
最近才发现挂WJ24XXXXXX的车才是最最 while(True): print("最") 最最最NB的~~

Tags:

Life | Twitter

2010-09-09

by j2.nete 9. September 2010 00:03

有很多年没有画过什么了, neal送的绘图板摆在桌上已经落了很多灰了

今天总算抓住了点兴趣和灵感, 草草的画了个素描

透明的绘图板底座, 透明的, 非常不好画

下次争取画些彩色, 绘图板和Painter也要再熟悉和实践

 

Tags:

Life

2010-08-24

by j2.nete 24. August 2010 13:32

把心思集中在“想干事”上,

把胆识体现在“敢干事”上,

把能力展现在“会干事”上,

把目标落实在“干成事”上。

    --《2010—2020年深化干部人事制度改革规划纲要》

Tags:

Twitter

2010-08-20

by j2.nete 20. August 2010 10:20

乌索普, 加油~~~

 

 

银san~~ T__T

Tags:

Twitter

python八荣八耻

by j2.nete 4. August 2010 23:48

以动手实践为荣 , 以只看不练为耻;

以打印日志为荣 , 以单步跟踪为耻;

以空格缩进为荣 , 以制表缩进为耻;

以单元测试为荣 , 以人工测试为耻;

 

以模块复用为荣 , 以复制粘贴为耻;

以多态应用为荣 , 以分支判断为耻;

以Pythonic为荣 , 以冗余拖沓为耻;

以总结分享为荣 , 以跪求其解为耻;

           -- 啄木鸟社区

Tags:

Twitter

好诡异的兵马俑~~

by j2.nete 21. July 2010 10:14

有赞为证, 赞曰:

头挽单髻,
体貌轻扬。
皂袍麻履,
形异寻常。
嘴如鹰鸳,
眼露凶光。

Tags:

Life | Twitter

Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen
陕ICP备08105618号

新浪微博

Calendar

<<  February 2012  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
2728291234
567891011

View posts in large calendar

RecentComments

Comment RSS