Saturday, November 21, 2009

Batch Program to Rename file with date & counter

One of my interface development requirements specified to take backups of a file with the prevailing sysdate appended.
Hence i sat down to see how this could be achieved with Batch Programming as my environment was Windows Platform.

An initial code was adapted from MSFN Forums & it behaved very well:

@Echo off
rem Original Batch File Code Location: http://www.msfn.org/board/lofiversion/index.php/t47812.html
rem Adapted By: Shalabhsneha Katdare (SHALABHKATDARE AT GMAILDOTCOM)
rem Reading input file
If "%1" EQU "" (
 Echo You must use parameter - name of file to rename.
 Goto :EOF
    ) ELSE (
 Set strFile2Rename=%1
    )
If not exist %strFile2Rename% (
    Echo %strFile2Rename% was not found!
    Goto :EOF
    )
For /f "usebackq delims=. tokens=1,2" %%i IN (`echo %strFile2Rename%`)    Do Set strFileExtension=%%j

rem Main code
call:ConvertDate
call:RenameFile
Goto:EOF
    
:ConvertDate    
rem Reading date format, converting and saving date
for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('echo.^|date') do (
 set A=%%a&set B=%%b&set C=%%c)
set t=2&if "%date%z" LSS "A" set t=1
for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('echo.^|date') do (
 for /f "tokens=%t%-4 delims=.-/ " %%d in ('date/t') do (
   set dd=%%d&set mm=%%e&set yy=%%f))
Goto:EOF

:RenameFile
ren %strFile2Rename% File_%dd%-%mm%-%yy%.%strFileExtension%
Echo File %strFile2Rename% is renamed to File_%dd%-%mm%-%yy%.%strFileExtension%

Usage:
cmd:\>rename2date filename.fileext
Output:
filename_sysdate.fileext

As you could see this was the perfect thing which i wanted. But soon i faced a major bottleneck!
The backup location was common for all the files. Hence in a given day, if i needed to take backup of the same file again, the batch program failed as the file formed upon renaming will be pre-existing from the backup taken earlier.

This evolved my initial requirement to a batch program doing rename with sysdate & counter added to it so that multiple renames in day could be supported for the same file.
Further modification to the initial above logic resulted into below code:

@Echo off
rem Original Batch File Code Location: http://www.msfn.org/board/lofiversion/index.php/t47812.html
rem Adapted By: Shalabhsneha Katdare (SHALABHKATDARE AT GMAILDOTCOM)
rem v1.1 [Added counter facility alongwith date. So we can rename a file multiple times on a given day!]
rem v1.0 [Plain rename facility without counter, i.e. on a given day the file can be renamed only once!]
rem Reading input file
set count=1
If "%1" EQU "" (
 Echo You must use parameter - name of file to rename.
 Goto :EOF
    ) ELSE (
 Set strFile2Rename=%1
    )
If not exist %strFile2Rename% (
    Echo %strFile2Rename% was not found!
    Goto :EOF
    )
For /f "usebackq delims=. tokens=1,2" %%i IN (`echo %strFile2Rename%`)    Do Set strFileExtension=%%j
For /f "usebackq delims=. tokens=1,2" %%i IN (`echo %strFile2Rename%`)    Do Set strFile=%%i
rem Main code
call:ConvertDate
call:RenFile
Goto:EOF
    
:ConvertDate    
rem Reading date format, converting and saving date
for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('echo.^|date') do (
 set A=%%a&set B=%%b&set C=%%c)
set t=2&if "%date%z" LSS "A" set t=1
for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('echo.^|date') do (
 for /f "tokens=%t%-4 delims=.-/ " %%d in ('date/t') do (
   set dd=%%d&set mm=%%e&set yy=%%f))
Goto:EOF

:RenFile
rem Renaming the file with date & counter
set FileCheck=%strFile%_%dd%-%mm%-%yy%_%count%.%strFileExtension%
If not exist "%FileCheck%" ( 
ren %strFile2Rename% %strFile%_%dd%-%mm%-%yy%_%count%.%strFileExtension% 
Goto :EOF
) ELSE (
set /a count=count+1 
call:RenFile)
Goto:EOF

Usage:
cmd:\>rename2date filename.fileext
Output:
filename_sysdate_counter.fileext

If you see the revision in code, i have just made a simple recursive call so that until a file (Filename formed post rename) is existing we would loop to increment the counter so that we finally arrive at unique filename with sysdate & a counter permitting us multiple renames on any given day for same file.

One thing which i struggled with Batch Programming is, its very sensitive to its syntax semantics. If a brace or two are not properly placed, the program would throw a annoying error of "The syntax of the command is incorrect"

It is difficult to trace where exactly is the syntax error coming from, but a quick echo ON helps & guides to correct directions.

So finally i have my batch program to rename a file with date & a counter :-D

Keywords:
how to rename multiple files, batch file rename, windows batch file rename, rename files, rename files windows, mass rename files, file rename program, file rename tool, quick file rename, batch file, batch file renamer, batch file renaming, batch file programming dos, windows batch file, file renamer, rename file, batch file commands, batch file command, bat files, bat file, batch files, multiple rename, batch script, batch scripting, batch program, windows scripting, windows scripts, DOS, etc

Friday, November 20, 2009

Embed Code Syntax Highlighting in Blog

Hi Friends,

If you are a developer & want to share your code over the blog with proper highlighting & syntax, then Blogger/Wordpress doesn't have the immediate solution out here!

A quick workaround for Blogger can be to embed your source between:
<blockquote></blockquote>
tags in order to prevent them from getting parsed.
E.g.


But for the formatting & highlighting used to quote above
example, an excellent open source tool called SyntaxHighlighter is available.

This is ready to use out of box 100% Javascript based SyntaxHighlighter with extensive features & multiple other options.

SyntaxHighlighter can be either downloaded & used on your own webserver or if you have a blog on Blogger, this can be directly used as well.

To downloaded SyntaxHighlighter for your own personal Webserver/Hosted Site: http://alexgorbatchev.com/wiki/SyntaxHighlighter:Download
Configuration Steps: http://alexgorbatchev.com/wiki/SyntaxHighlighter:Configuration
Brush Aliases:http://alexgorbatchev.com/wiki/SyntaxHighlighter:Brushes

In order to install & use it on Blogger, follow the below simple steps:
1) Go to your Template Layout->Download your template as a backup first
2)Now click Edit HTML for Layout & then paste the below code in your Template before your </head> tag ends. Note this is pointing to the Latest released version v2.1.364; older versions can be found at: http://alexgorbatchev.com/pub/sh/

<link href='http://alexgorbatchev.com/pub/sh/2.1.364/styles/shCore.css' rel='stylesheet' type='text/css'/> 
<link href='http://alexgorbatchev.com/pub/sh/2.1.364/styles/shThemeDefault.css' rel='stylesheet' type='text/css'/> 
<script src='http://alexgorbatchev.com/pub/sh/2.1.364/scripts/shCore.js' type='text/javascript'></script> 
<script src='http://alexgorbatchev.com/pub/sh/2.1.364/scripts/shBrushCpp.js' type='text/javascript'></script> 
<script src='http://alexgorbatchev.com/pub/sh/2.1.364/scripts/shBrushCSharp.js' type='text/javascript'></script> 
<script src='http://alexgorbatchev.com/pub/sh/2.1.364/scripts/shBrushCss.js' type='text/javascript'></script> 
<script src='http://alexgorbatchev.com/pub/sh/2.1.364/scripts/shBrushJava.js' type='text/javascript'></script> 
<script src='http://alexgorbatchev.com/pub/sh/2.1.364/scripts/shBrushJScript.js' type='text/javascript'></script> 
<script src='http://alexgorbatchev.com/pub/sh/2.1.364/scripts/shBrushPhp.js' type='text/javascript'></script> 
<script src='http://alexgorbatchev.com/pub/sh/2.1.364/scripts/shBrushPython.js' type='text/javascript'></script> 
<script src='http://alexgorbatchev.com/pub/sh/2.1.364/scripts/shBrushRuby.js' type='text/javascript'></script> 
<script src='http://alexgorbatchev.com/pub/sh/2.1.364/scripts/shBrushSql.js' type='text/javascript'></script> 
<script src='http://alexgorbatchev.com/pub/sh/2.1.364/scripts/shBrushVb.js' type='text/javascript'></script> 
<script src='http://alexgorbatchev.com/pub/sh/2.1.364/scripts/shBrushXml.js' type='text/javascript'></script> 
<script src='http://alexgorbatchev.com/pub/sh/2.1.364/scripts/shBrushPerl.js' type='text/javascript'></script> 
<script language='javascript'> 
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.config.clipboardSwf = 'http://alexgorbatchev.com/pub/sh/2.1.364/scripts/clipboard.swf';
SyntaxHighlighter.all();
</script>

3)Save the template & open you blog in IE & Firefox to test if any javascript errors are reported as this might conflict with various other embedded scripts in your template.

This completes the direct embedding of SyntaxHighlighter in your Blog. Now lets proceed how to use it in your posts:

Usage:
For SyntaxHighlighter 2.0 & above:
Any code you want to be placed & displayed using SyntaxHighlighter must be enclosed in:

<script type="syntaxhighlighter" class="brush: html"><![CDATA[
#######YOUR CODE############
]]></script>

Note that we have option of using different brushes for highlighting different type of codes..e.g. HTML/ASP/C++ etc
In order to choose the appropriate brush, following are the various brush aliases supporting the corresponding code highlighting:
actionscript3
bash, shell
c-sharp, csharp
cpp, c
css    
delphi, pas, pascal
diff, patch
groovy
js, jscript, javascript
java
jfx, javafx
perl, pl
php
plain, text
ps, powershell
py, python
rails, ror, ruby
scala
sql
vb, vbnet
xml, xhtml, xslt, html, xhtml

Above aliases can be used at: class="brush: alias" while embedding your code in the Blog.

Another easy way supported by SyntaxHighlighter is to enclose the code within:



The only catch involved out here is, you have to use encoded HTML while embedding within <pre>tags.
Your code can be converted to HTML Encoded using following easy to use Tool:
http://www.string-functions.com/htmlencode.aspx

Just copy paste your code & generate the encoded HTML Code. Paste the encoded HTML within <pre>tags for correct rendering. Failure to do so might render wrong code, especially < & > brackets.

Some examples of codes in my Blog:
http://geektalkin.blogspot.com/2009/07/historical-audit-trail-information.html

http://geektalkin.blogspot.com/2009/11/batch-program-to-rename-file-with-date.html

Keep Blogging!

Keywords: Blogger, Embed Code, Code, Syntax, Syntax Highlight, Syntaxhighlighter, Javascript, Blog Tricks, Blogs, Java, SQL, ASP, HTML, HTM, C#, XML, XSL, JS, CSS, PHP, PL, CGI, embed code blog,embed blogger blog, embed youtube blogger, swf embed code, you tube embed code, embed src code, embed wordpress blog, embed video blogger, myspace embed code,embed into, html code,rss feeds,blogger templates, blogger,syntax highlighting,script error,script,etc

Wednesday, November 18, 2009

Guide to Enable WPA2-PSK Support on Dlink DWL-G630 Wireless Card

Hi Friends,

If you are using DWL-G630 Wireless Card from Dlink, then you might have noted that it does not support WPA2-PSK Encryption.

I am talking of particular DWL-G630 Rev. D Card which are sold out here in India.
The driver that came along with the contents are v4.10 released in 2005 which are pretty old.

A quick search around google to find out the latest drivers from Dlink seems pretty painful task as there is no proper information available on Dlink Official Website.

Hence, i decided to crack this nut...as i did not want to lower my Wireless Router's security encryption anything below WPA2-PSK just because of lame non-availability of driver!

First, in order to get latest drivers from Dlink; finally landed me to the Dlink's Global US FTP Site, where dozens of driver zips are available.
You have to manually download & have a look if it will work for your card or not. Dont go for the filename to determine the driver model as it might support many cross cards.
You need to check the driver INF file post download to exactly determine which cards are supported.

DLink's Global FTP Driver Site: ftp://ftp.dlink.com/Wireless/

Now, after downloading couple of files, the only latest driver post v4.10 was v4.1.2.725 released on 06/27/2007.
ftp://ftp.dlink.com/Wireless/dwlG630_revE/Drivers/dwlG630_revE_drivers_514.zip

If you install these drivers from Dlink, don't expect it to support WPA2-PSK Encryption!!
Thus, the search for WPA2-PSK Support continued!!

Lets have a quick introduction over WPA2-PSK & its support on Microsoft Platform:
A very good article for Microsoft support can be read at:
http://www.microsoft.com/windowsxp/using/security/expert/bowman_wirelesssecurity.mspx

Note that you need XP Service Pack2 or the specific update in order to make sure that your OS Support's WPA2-PSK Encryption.
If you are already on Windows XP Service Pack3...you are on track ;-)

Two good articles to go over WPA2-PSK Encryption details:
http://technet.microsoft.com/en-us/library/bb878054.aspx
http://en.wikipedia.org/wiki/Wi-Fi_Protected_Access

So now to move ahead with our WPA2-PSK Support quest...i went around few articles which mentioned that the chipset used in DWL-G630 is manufactured by Atheros!

A quick search around points that Dlink DWL-G630 has AR5005 chipset. Now the catch out here is Atheros drivers support WPA2-PSK Encryption.

So finally after digging on the google, i got the below Atheros AR5005X chipset driver compatible for DWL-G630!
WPA2-PSK Supporting DWL-G630 Driver for Windows XP: http://www.filefactory.com/file/a1fcbea/n/atheros_ar5xxx_770406_xp.zip

Once i downloaded this & did an update driver, my Wireless card connected instantly to my Wireless Router Access Point!

If you are looking to configure this Card on Linux, then below is a good page on Ubuntu Forums for the same:
http://ubuntuforums.org/archive/index.php/t-176750.html

Finally a day along wait and precious R&D came to some useful conclusion.
I am not sure why DLink is not releasing an updated driver which would support the WPA2-PSK Encryption, but as long as we have a workaround we should not be complaining ;-)

I hope this documentation seals up the issue for anyone else as i did not find a conclusive dedicated document on the internet for this issue.

Go Wireless >>>>>>>>>

Keywords: Wireless, Wireless NIC, Wireless Card, DWL-G630 Rev D, Dlink, WPA2, WPA, WPA2-PSK, Atheros 5005X, Atheros 5xxx, Driver, Wireless Driver, etc