Interactive command line

After you start crab, e.g. by scanning a directory

	C:\> crab myproject 

you'll get an interactive Crab command line:

	CRAB>

You can enter SQL queries or Crab commands. SQL queries need a semicolon on the end; commands do not.

Use %quit to finish the Crab session.
Use %show to see current Crab settings
Use %help to access Crab reference info

You can cancel a running scan or query with Ctrl+C or Ctrl+Break

Multi line queries

If you write each query as one long line, you can recall and edit queries from your history (with Ctrl+P). But it is difficult to see what is going on, especially when there are subqueries.

Another option is to edit multi-line queries in a text editor such as TextEdit or Notepad, then paste them into the Crab command line. You can paste in one go, you don't need to paste line by line. Don't forget the semicolon at the end of the last line.

A third option is to write the query in a text editor, save it as a Crab script file, and run the script from Crab using the %read command. e.g.

	CRAB> %read 'C:\somepath\myscript.crab'

Layout of query results

By default, query results at the Crab command line are shown as a set of field:value pairs, called a dictionary. This is convenient for displaying long fields such as paths. Set this output mode with

    %mode dict

CRAB> select * from files limit 3;
{fileid: 1, name: PYTHONDROPBOX, bytes: 782, depth: 0, accessed: 2015-08-15T17:52:12, modified: 2015-07-28T10:14:45, basename: PYTHONDROPBOX, extension: , type: d, mode: drwxr-xr-x, parentpath: C:\Users\johnsmith\Dropbox\, fullpath: C:\Users\johnsmith\Dropbox\PYTHONDROPBOX\}
{fileid: 2, name: PS_Store, bytes: 14340, depth: 1, accessed: 2015-08-15T14:42:25, modified: 2015-08-11T10:20:52, basename: PS_Store, extension: , type: f, mode: -rw-r--r--, parentpath: C:\Users\johnsmith\Dropbox\PYTHONDROPBOX\, fullpath: C:\Users\johnsmith\Dropbox\PYTHONDROPBOX\PS_Store}
{fileid: 3, name: getMssql.py, bytes: 694, depth: 1, accessed: 2015-08-07T17:31:52, modified: 2013-09-08T22:37:24, basename: getMssql, extension: .py, type: f, mode: -rw-r--r--, parentpath: C:\Users\johnsmith\Dropbox\PYTHONDROPBOX\, fullpath: C:\Users\johnsmith\Dropbox\PYTHONDROPBOX\getMssql.py}

Other layout modes for query results can be set with the %mode command.
Each field on a line of its own:

    %mode line

CRAB> select * from files limit 3;
    fileid = 
1
      name = 
PYTHONDROPBOX
     bytes = 
782
     depth = 
0
  accessed = 
2015-08-15T17:52:12
  modified = 
2015-07-28T10:14:45
  basename = 
PYTHONDROPBOX
 extension =
      type = 
d
      mode = 
drwxr-xr-x
parentpath = 
C:\Users\johnsmith\Dropbox\
  fullpath = 
C:\Users\johnsmith\Dropbox\PYTHONDROPBOX\

    fileid = 
2
      name = 
PS_Store
     bytes = 
14340
     depth = 
1
  accessed = 
2015-08-15T14:42:25
  modified = 
2015-08-11T10:20:52
  basename = 
PS_Store
 extension =
      type = 
f
      mode =
-rw-r--r--
parentpath = 
C:\Users\johnsmith\Dropbox\PYTHONDROPBOX\
  fullpath = 
C:\Users\johnsmith\Dropbox\PYTHONDROPBOX\PS_Store

    fileid = 
3
      name = 
getMssql.py
     bytes = 
694
     depth = 
1
  accessed = 
2015-08-07T17:31:52
  modified = 
2013-09-08T22:37:24
  basename = 
getMssql
 extension = 
.py
      type = 
f
      mode = 
-rw-r--r--
parentpath = 
C:\Users\johnsmith\Dropbox\PYTHONDROPBOX\
  fullpath = 
C:\Users\johnsmith\Dropbox\PYTHONDROPBOX\getMssql.py

One column per field, truncated to 10 chars (N.B. use the %width command to change number of characters per column):

    %mode column

CRAB> select * from files limit 3;
fileid      name        bytes       depth       accessed    modified    basename    extension   type        mode        parentpath  fullpath  
1           PYTHONDROP  782         0           2015‑08‑15  2015‑07‑28  PYTHONDROP              d           drwxr‑xr‑x  C:\Users\j  C:\Users\j
2           PS_Store    14340       1           2015‑08‑15  2015‑08‑11  PS_Store                f           ‑rw‑r‑‑r‑‑  C:\Users\j  C:\Users\j
3           getMssql.p  694         1           2015‑08‑07  2013‑09‑08  getMssql    .py         f           ‑rw‑r‑‑r‑‑  C:\Users\j  C:\Users\j

Comma delimited output fields (N.B. use the %separator command to change the delimiter):

    %mode list

CRAB> select * from files limit 3;
fileid,name,bytes,depth,accessed,modified,basename,extension,type,mode,parentpath,fullpath
1,PYTHONDROPBOX,782,0,2015-08-15T17:52:12,2015-07-28T10:14:45,PYTHONDROPBOX,,d,drwxr-xr-x,C:\Users\johnsmith\Dropbox\,C:\Users\johnsmith\Dropbox\PYTHONDROPBOX\
2,PS_Store,14340,1,2015-08-15T14:42:25,2015-08-11T10:20:52,PS_Store,,f,-rw-r--r--,C:\Users\johnsmith\Dropbox\PYTHONDROPBOX\,C:\Users\johnsmith\Dropbox\PYTHONDROPBOX\PS_Store
3,getMssql.py,694,1,2015-08-07T17:31:52,2013-09-08T22:37:24,getMssql,.py,f,-rw-r--r--,C:\Users\johnsmith\Dropbox\PYTHONDROPBOX\,C:\Users\johnsmith\Dropbox\PYTHONDROPBOX\getMssql.py

Interactive command line vs batch mode

The interactive command line is useful for exploring data and developing queries, but once you have a query that works the most efficient way to rerun it is to use a text editor to save it as a text file, and execute it with the %read command, or the -init start up option, e.g.

	CRAB> %read analyzetree.crab

You can run scripts without an interactive command line by using the -batch option at Crab startup. This tells Crab to scan any paths provided (if any), and to run any commands or queries provided (if any), and then quit.

	C:\> crab -batch -init analyzetree.crab

When running queries in batch, output is returned as comma delimited lists unless you use a command line switch at Crab start up. See the "Launching Crab" documentation.

Batch mode instant SQL

You can use Crab to extend Windows Command Line with SQL queries.

E.g. the following command scans the current directory, and lists the fullpath of all the files:

	C:\> crab -maxdepth 1 -batch . "select fullpath from files where type = 'f'"


To make this into a command available at the Windows command prompt, save this to a Windows .bat or .cmd script file. See next section.

Using Crab in Windows cmd scripts

Below is a oneliner which, when saved as a script, takes one or more paths as parameters, scans them and runs a query which returns the fullpaths of the contents.

	C:\somepath\CrabHome\CrabExe\crab -maxdepth 1 -batch %* "select fullpath from files where type = 'f'"


The scan paths are passed to Crab using %* which is a placeholder for the script arguments. E.g. If you save this line in a text file called fulldir.cmd you can invoke it like this:

	C:\> fulldir newproject oldproject


Both newproject and oldproject will be passed for scanning and querying.


Because cmd scripts use % to indicate script arguments, when you want to use the %-symbol in a different way, e.g. as a wildcard for the LIKE operator, you have to double them up. E.g.

	crab -batch . "select fullpath from files where name like '%%June%%'"

Default editing shortcuts

move cursor word left ctrl + left
move cursor word right ctrl + right
move cursor start of line Home
move cursor end of line End
delete word left ctrl + Backspace
delete word right ctrl + Del
clear input line Esc
undo ctrl + Z
complete keyword tab (after first two characters)
recall previous command (step back through history) up arrow
step forward through history down arrow
quit Crab or cancel scan ctrl + break
cancel query ctrl + c

Set up quick Copy and Paste for the Windows command line

On Windows 10, uncheck "Use legacy console" by right clicking on the console window's title bar and choosing Properties. More info here:
    https://www.laptopmag.com/articles/how-to-windows-10-command-prompt-copy

Now you can use Ctrl+C and Ctrl+V to copy and paste

On Windows 7 to Copy and Paste at the Windows command line, you should have "Quick Edit Mode" turned on. Right click on the title bar of the command line window, choose Properties and tick the Quick Edit Mode box. More info here:

     https://www.tekrevue.com/tip/boost-productivity-quickedit-mode-windows-command-prompt/

Now to copy text just highlight it using click-and-drag with the left mouse button, and press
Enter.

To paste text:

    Alt + Space    followed by   E   and then   P


© 2017 Etia UK