Showing posts with label cmd-bat. Show all posts
Showing posts with label cmd-bat. Show all posts

Wednesday, 4 December 2013

Cygwin Here

This short article explains how to open a specific folder in Cygwin.

Tuesday, 2 October 2012

Continuing long lines in a batch script

Unintentionally I found for myself that a batch script has a nice feature to break long lines without loosing of possibility of reading them.

Imagine, you have some weird combination of commands as below that is not put completely in one line of a screen.

some-command-with-many-arguments | another-lomg-command-with-its-own-arguments | etc | etc | etc

If the line of a code cannot be shown within one line on a screen it will be i) either turned and the rest of line will be shown below on one or more lines or ii) continued out of edges of the screen with a horizontal scrolling bar. Both of them are not convenient because we not able to observe the code entirely.

Fortunately, modern batch scripts (in XP, Vista, Win7) have blocks implemented by brace:

(
    some-command-with-many-arguments 
) | (
    another-long-command-with-its-own-arguments 
) | etc | etc | etc

I known this feature (because I used brace in the if-else and for statements) but did not realised right now.

So, this gives us good possibility to break the long line into a several lines without loosing of functionality. This is better than nothing and is better than undocumented feature using the DOS-like escaping character ^ at the very ending of the line because you have to look after that this character is in the lastest character of the line. See the same example with the esacaping character:

some-command-with-many-arguments | ^
another-long-command-with-its-own-arguments | ^
etc | etc | etc

Unix shell scripts are featured to continue long commands using by backslashes since the begining of the Unix wolrd.

some-command-with-many-arguments \
| another-long-command-with-its-own-arguments \
| etc | etc | etc

Saturday, 29 September 2012

How to concatenate multiple lines in console (bash, cmd)

If for some reasons you need to have concatenated lines try one of the recipes below.

Sunday, 25 March 2012

Universal way to identify your external IP and more

There are many such kind of resources in Internet but this is the best. It allows the numerous structured data and it has nice name. Meet http://ifconfig.me/.
Command line interface
Using curl
curl ifconfig.me
or wget
wget -qO - ifconfig.me/ip
JScript
JS
var xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
xmlhttp.open('GET', 'http://ifconfig.me/ip', false);
xmlhttp.send();
 
var ip = xmlhttp.responseText;
JS+XML
var xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
xmlhttp.open('GET', 'http://ifconfig.me/all.xml', false);
xmlhttp.send();
 
var ip = xmlhttp.responseXml.getElementsByTagName('ip_addr')[0].text;
Having JSON library you can read and parse JSON data:
var xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
xmlhttp.open('GET', 'http://ifconfig.me/all.json', false);
xmlhttp.send();
 
var data = JSON.parse(xmlhttp.responseText);
var ip = data.ip_addr;
VBScript
Dim xmlhttp, ip
 
Set xmlhttp = CreateObject("Microsoft.XMLHTTP")
xmlhttp.open "GET", "http://ifconfig.me/ip", False
xmlhttp.send
 
ip = xmlhttp.responseText

Monday, 29 March 2010

Windows Scripting Command Interpreter

Консольный интерпретатор JS/VBS-скриптов в автоматическом и интерактивном режимах.

Friday, 12 February 2010

CMD/BAT A few useful routines

Analogues of pwd, basename and dirname.

Thursday, 28 January 2010

CMD/BAT: Repeat a string multiple times

If you want to get a string repeated multiple times -- you can use this routine. I have realised this as an attempt to implement this optimal algorithm using CMD/BAT only. It accepts three arguments: the name of a variable to store the resulting string, the multiplier (number of time the input string should be repeated), and the input string.

Tuesday, 3 November 2009

Cross-platform date/time definition

Earlier here (in Russian) i have investigated ways how to obtain date and time parts using the CMD.EXE features. After that here (in Russian too) i have simplified this way until two lines of a code. But both ways are platform-dependent - the order of date parts depends on the locale settings.

Recently i found the excelent link (in Russian too) with the description how to obtain the subject platform-independently.

I slightly modified the established method and present it now.

Thursday, 29 October 2009

CMD/BAT Librarian

Introduction
Unfortunately CMD.EXE, the command line interpreter in Windows, does not allows to collect favorite scripts under appropriate folders and use them multiple times in the future. So we have to use the "copy-and-paste" technology to migrate some needed functionalities from one script to another.

The script below allows to store debugged scripts under the determined folder, use them mutiple times, compile and link automatically to the final script.

Logfile rotation by CMD/BAT

The script below allows to rotate file (e.g.: logfiles). By default, the length of rotation is 5. It means that this script rotates some file no more than for 5 times.

Let's consider the permanently growing file of some application named as something.log and we'd like to keep records in this file for the long time as much as possible. But for some reason this file is cleaning up periodically or a size of the file affects on the application's performance.

The recent records are available from the file something.log and the previously saved records are available under the name something.log.1, etc until the something.log.5. After the following rotation the content of the last file will be lost and replaced by the content of the previous file (something.log.4 will be copied to the something.log.5) and the first file something.log will be copied to the something.log.1. At the same time the something.log will be truncated.

rotate something.log 10

Sunday, 4 October 2009

WHICH: UNIX-to-NT ported

WHICH is unix-world command, implemented using CMD/BAT feature only.

Friday, 2 October 2009

Simple Batch Progress Bar

Example 1. Print the bar within the window with '#' as the default filling character
call :progressbar 50
Example 2. The same as above but with an another filling character
call :progressbar 50 *
Example 3. Print the bar in the window title
set progressbar_t=1
call :progressbar 50

Tuesday, 8 September 2009

CMD/BAT: GetOptions

This is flexible and useful library to manipulate with arguments in CMD/BAT scripts.

Wednesday, 19 August 2009

Substring extraction by CMD

Once i needed to solve the next task. I have a batch script with name corresponding to the following conditions:
  1. the script filename has to be matched the pattern (see below).
  2. this script has to be located within directory tree of free nestion but one of directories of the tree has to be macthed the same pattern.

Thursday, 9 July 2009

JS2BAT converter 2

Earlier to embed javascript codes into batch files the JS2BAT tool has ben implemented with batch features only. In this topic the same tool has been implemented in pure javascript. As opposed to the previous version the present one is the more functional and has optional parmeters. Previously the js2bat.js script has been implemented in javascript. After that it has been converted to the js2bat.bat batch script by itself with the following command:
cscript js2bat.js js2bat.js

Tuesday, 7 July 2009

JS2BAT converter

This script allows you to embed Javascript codes into the batch-script immediately. So you can stop to worry about creating of an additional batch-wrapper or lost of some specific parameters. Perl distribution has the same functional script pl2bat but for wrapping of perl-scripts with batch-scripts. I am not author of this idea, i just implemented this feature as the standalone tool. The main feature is to write the prolog code that is valid both in a batch and javascript. When launching the batch it runs as batch and calls the javascript interpreter with the same file as javascript file. But the second one skips this prolog as valid javascript comments.

Friday, 3 July 2009

Finest code syntax highlighter

Quotation from the Code Syntax Highlighter, the homepage of the project, allowing to embed colorizing of text and fully written on Javascript.
SyntaxHighlighter is a fully functional self-contained code syntax highlighter developed in JavaScript.
It is really true! It is very useful, very flexible and easy to customize. The main idea is to use so-named brushes to colorize codes within <pre /> tags (it is defined by default and customizable too!). Colorizing is simple. You have to link scripts and styles to your pages and add brush via class attribute (like this class="brush:js" for colorizing of Javascript codes).
I decided to use it in my blog and found that it works fine. Unfortunately there is no brushes for cmd/bat scripts and i have implemented this embedding to the blog's layout the code from below.

HOWTO turn a character case via CMD/BAT

Introduction

This example is amazing that fact that it can be extended by non-Latins with no pain. Just add your owned translation table at the end of script in the format UPPER lower. For example, the translation table for Cyrillic characters is following:
А а
Б б
...
Ю ю
Я я
Just remember that the capitals have to be the first with following lowercase letters.

Examples of usage

Capitalize all characters. Results to QWERTY.
CAPS qwerty /u
The first character is capital, and others are in lower case. Results to Qwerty.
CAPS qwerty /uf
Invert the result of the previous example. Both result to qWERTY.
CAPS qwerty /uf /l
CAPS qwerty /lf

Monday, 15 June 2009

WC: Emulation of the unix command

This is curious, joke. It is attempt to emulate the unix-commad wc. Do not consider this as fully featured application. It has essential shortcomings - slower than analogs and it gives great mistakes when counting the number of words. This is related with features of processing of special characters when passing them as arguments.

Validate number arguments within CMD/BAT

@echo off

if "%~1" == "" (
    echo EMPTY
    goto :EOF
)

if "%~1" == "0" (
    echo ZERO
    goto :EOF
)

set /a number_var=%~1 2>nul

if errorlevel 2 (
    echo ILLEGAL
    goto :EOF
)

if %~1 neq %number_var% (
    echo ERROR
    goto :EOF
)

set number_var
goto :EOF