Move files from one folder to another

Good day, Q-Gods and newbies, 

Quick question: 

How would one move few files from one folder to another using q/kdb+? 

I have 2 folders with one splayed table in each. Sometimes, I need to move a few files between these two files. Example below:

key `:/db/tmp

s#0IKBKJKLKFEGGBIFKEJCHABJCBIIBKH`1LCEFECHIFGKEBDDHICBECIAFIFLCKI - these are the 2 folders with splayed table in each. 

 

First folder: 

key `:/db/tmp/0IKBKJKLKFEGGBIFKEJCHABJCBIIBKH

s#.dT0D18_51_05_383000001T0D18_51_05_384000002

Second folder: 

key `:/db/tmp/1LCEFECHIFGKEBDDHICBECIAFIFLCKI

s#.dT0D18_51_05_494000003T0D18_51_05_655000004

If I need to move the two files from the second folder (can copy or just move)

T0D18_51_05_494000003T0D18_51_05_655000004

to the first folder “0IKBKJKLKFEGGBIFKEJCHABJCBIIBKH” how would I do it using the command line? 

Thank you very much for your time and effort, 

Regards, 

VA. 

Hi VA,

You can see here - http://code.kx.com/wiki/Reference/SystemCommands#.5Cr\_src\_dst\_-\_rename

example:

:./db/t/ set ([]a:10?10;b:10?10) ; :./db/t2/ set (a:10?10;c:10?10;d:10?10) ;

f:{[dir;newdir] (system 0N!" " sv (1#“r”;;). 1_'string .Q.dd:[(dir;newdir)]@) each key[dir] except `.d;} ;

f[:t2;:t3]

(ps - this gives additional output that just shows you what is actually being ran).

(pps - this will not create the newdir folder, if not there already. You can use normal mkdir commands and/or use the kdb+ system command “cd” to create it if required)

As much as above will do what you requested, are you sure you want to do this?

A splayed table is saved to disk in a folder, the folder name representing the table name, and each file representing the columns, with the exception of the .d file which is a symbol list of the columns of that table.

 

When running this function above(and going by your requirements), you are moving all columns from dir to newDir. This can overwrite some of the previous columns that were already there. Are both sets of column files the same length now? If not, you will have a jagged splay, and hit errors further on down the line when querying. Are you updating the .d file to include the newly copied files? If not, your kdb+ process will never recognise these newly copied columns as actual columns in the splayed table, and so they are made redundant. Just some things to think about..

Btw - I’d rather not get into it here on this thread, and hopefully you already are aware, but the naming conventions of your columns, and table names go against several kdb+ principles.

 

HTH,

Sean

Hi Sean,

Once again, thank you for your quick reply. It’s very helpful, as always. And what a “tell off” :) 

I am just learning kbd+/q and there are many aspects of kdb+ which I am still not familiar with. For now, this will do. Unfortunately, most of the time it is not the best approach…

As you say, all cols are moved to one folder. Indeed, the /.d file in that folder is updated every time; all cols are empty - I am creating one absolutely massive empty splayed table which I will be filling in at next stage. 

All the problems I have right now come from the size of the files I work with. This is my way around this, not necessarily the best, but its working for now. I hope my kdb+ skills will get better soon. 

Good shout about the the naming - I will correct it now as I need col names to be valid identifiers. Thank you. 

Hi Sean, 
Sorry but your suggestion does not seem to work, if I understand it correctly. When I try it, it does nothing. Do you mind checking it please? 

Regards, 

VA. 

does for me…

//splay t to disk, load db, count each column, get t directory contents

q):./db/t/ set ([]a:10?10;b:10?10) ; system"l db" ; 0N!(count get@) each :t/a:t/b ; key:t

10 10

.da`b

//mkdir t2 dir(you already have it created), run function

q)f:{[dir;newdir] (system 0N!" " sv (1#“r”;;). 1_'string .Q.dd:[(dir;newdir)]@) each key[dir] except `.d;} ;

q)system"mkdir t2" ; f[:t;:t2]

“r t/a t2/a”

“r t/b t2/b”

//check count of t cols(should me moved), content of t dir, count of t2 cols(should be two cols and no .d file), contents of t2 dir

q)0N!@[(count get@);;{x}] each :t/a:t/b ; 0N!key:t ; 0N!(count get@) each :t2/a:t2/b ; key:t2

(“t/a: No such file or directory”;“t/b: No such file or directory”)

,`.d

10 10

ab