Saari Development

Ali Rizvi's Technical Blog as a Professional Software Development Engineer

Archive for the ‘windows’ Category

Windows: Printing Date and Time on Command Line

with one comment

I am more of a *nix (unix, linux etc) command line person but I still use windows from time to time (in this case because my macbook pro crashed).

I often want to see the progress of output to a file over time using the following linux command line:

$ date; wc -l output.txt

On windows date does not print time (you have to do ‘date \t’ to get output from it otherwise it used for changing the system date).

I finally found what I was looking for:

> echo %DATE% & echo %TIME% & wc -l output.txt

Output:

Thu 07/21/2011
13:44:55.70
375 output.txt

 

The wc command and bunch of other unix commands come from the free UnixUtils opensource package for windows. I consider UnixUtils a must have for windows and among the first thing I install on my windows machine.

Written by imsaar

July 21, 2011 at 12:44 pm

Posted in windows

Ruby: Add a prefix to all files in a directory

leave a comment »

I found myself looking for a way to rename all files in a directory by appending a prefix and couldn’t find a utility to do such a renaming after quick search so I wrote my own.


# Quick script to bulk prepend prefix to filenames to all files in a directory
# Also strips any spaces in the filename
# Example usage : ruby prepend_rename.rb Disc1_ C:\AudioBook\Disc1\

raise "Prefix and Directory are required" if ARGV.size < 2

prefix = ARGV[0]
dir = ARGV[1]

raise "Non-word character prefix #{prefix}" unless prefix =~ /^\w+$/
raise "No such directory: #{dir}" unless Dir.exists?(dir)

Dir.chdir(dir)
Dir.entries(dir).each do |file|
 next if File.directory?(file)
 new_name = prefix + file.gsub(/\s+/, '')
 File.rename(file, new_name)
end

Written by imsaar

February 7, 2010 at 6:11 pm

Posted in code, ruby, windows

Windows: Making my laptop *nix friendly

leave a comment »

Update: Download UnixUtils. Extract to C:\ and then add to PATH by right clicking on My Computer, click Properties -> Advanced (tab) -> Environment Variables (button). This gets the best unix untilities available on command prompt.

I usually do most of my development on my Linux machine and only use my Windows XP laptop for using Outlook (mail and calendaring) and browsing (Firefox, of course).

I decided to work from home due to weather conditions and had to use my laptop more and finally took the steps to make my laptop more unix friendly.

I already had my favorite editor Vim installed on the machine and ruby one click install (which did not required the forbidden admin priviliges thank God) made quick experimenting easy while I was commuting to work. Also rubygems installation work pretty smoothly (similar to on a linux/mac osx system) on windows also.

I already had configured my laptop to use have tab completion for windows lesser shell (cmd or Command Prompt) so cd into directories was a breeze.

Two things that I missed the most still were ls (which I often typed and got error on windows shell) and typing vi filename and opening file and I was fed up with using edit to open files all the time which is far from a decent text editor at least by my standards.

Finally I was motivated enough to do something about these. Here is what I did:

Added the path to the directory where vim was installed to my environment variable:

1. Right Click on ‘My Computer’ and click ‘Properties’
2. Click on ‘Advanced’ tab and click ‘Environment Variables’ button, click ‘PATH’ if already exists or create new if it is not already there.
3. Add the absolute path of directory where vim.exe exists at the end (semi-colo {;} separated list).

Create shortcuts for vi and ls

1. Now open command prompt (Start -> Run -> cmd -> OK)
2. cd into the directory where vim is installed (e.g. C:\>cd “Program Files\Vim\vim70”)
3. edit vi.bat
4. type “C:\Progra~1\Vim\vim70\vim.exe %1 %2 %3” save and exit (Alt – F – X) the file
4a. %1, %2 … are the arguments passed to the bat (shell) script
Since I am lazy I will create the ls bat also here but you can create it in any directory in your path
5. vi ls.bat (now you can use your new shortcut)
6. type “dir %1” save and exit (:wq) the file

Enjoy!

Written by imsaar

November 29, 2006 at 1:41 am

Posted in windows