' Move_EPUB.IWB
'  Find Subdirectory and Move and rename files to another subdirectory

' Uses browse for folder (ibasic pro code) by Sapero

DECLARE IMPORT, RENAME ALIAS MoveFileA(lpExistingFileName AS STRING,lpNewFileName AS STRING),INT

DECLARE IMPORT,SHBrowseForFolder(param1:UINT),INT
DECLARE IMPORT,SHGetPathFromIDList(param1:UINT, param2:UINT),INT
DECLARE IMPORT,CoTaskMemFree(param1:INT),INT
DECLARE IMPORT,RtlZeroMemory(dat:UINT,length:INT),INT
DECLARE IMPORT,SendMessageA(hWnd:UINT, message:UINT, wParam:INT, lparam:UINT),UINT

TYPE BROWSEINFO
	UINT hOwner
	UINT pidlRoot
	POINTER pszDisplayName
	POINTER lpszTitle
	UINT ulFlags
	UINT lpfn
	UINT lParam
	UINT iImage
ENDTYPE

CONST WM_USER               = 0x400
CONST BFFM_SETSELECTION     = WM_USER + 102
CONST BFFM_INITIALIZED      = 1
CONST BFFM_VALIDATEFAILED   = 3
CONST BIF_RETURNFSANCESTORS = 8
CONST BIF_RETURNONLYFSDIRS  = 1
CONST BIF_NEWDIALOGSTYLE    = 0x40
CONST BIF_DONTGOBELOWDOMAIN = 2
CONST BIF_FLAGS             = WM_USER + BIF_NEWDIALOGSTYLE + BIF_RETURNONLYFSDIRS
'0x441

ENUM ControlIDs
	D1_B1 = 500
	D1_B2
	D1_S1
	D1_E1
ENDENUM

DEF w:WINDOW
DEF OnScreen[27]:STRING
DEF indx, row, i2:INT
DEF idir[261], odir[261], hdir[261]:ISTRING
DEF wext = "epub":STRING
DEF retval = 0, done = FALSE, wlen:INT

DEF BLACK, RED, GREEN, BLUE, YELLOW, CYAN, PINK, GREY, MAGENTA:UINT
DEF LBLUE, LYELLOW, WHITE, PURPLE, PURPLE2, GOLD, SILVER, BROWN:UINT

GOLD    = RGB(255,215,0)
YELLOW  = RGB(255,255,0)
LYELLOW = RGB(255,255,125)
GREEN   = RGB(0,255,0)
BLUE    = RGB(0,0,255)
RED     = RGB(255,0,0)
CYAN    = RGB(0,255,255)
PINK    = RGB(255,192,203)
GREY    = RGB(127,127,127)
LBLUE   = RGB(0,125,255)
MAGENTA = 0x8D38C9
PURPLE  = RGB(128,0,128)
PURPLE2 = RGB(230,25,230)
BROWN   = 0x60A4F4
SILVER  = RGB(128,128,128)
WHITE   = RGB(255,255,255)
BLACK   = RGB(0,0,0)

OPENWINDOW w,0,0,640,480,0,0,"Move Files for Specific Extension",&hnd
SETWINDOWCOLOR w, BLACK
SETWINDOWCOLOR w, WHITE
CONTROL w,@BUTTON,"Process",550,15,60,20,0x50000000,D1_B1
CONTROL w,@BUTTON,"Exit",550,50,35,20,0x50000000,D1_B2
CONTROL w,@STATIC,"Extension",400,17,70,20,0x5000010B,D1_S1
CONTROL w,@EDIT,wext,470,15,50,20,0x50800000,D1_E1
SETCONTROLCOLOR w,D1_B1,BLACK,GREEN
SETCONTROLCOLOR w,D1_B2,BLACK,RED

WAITUNTIL w=0
END

'============
SUB hnd(),INT
'============
'handler for the window w1
SELECT @CLASS
' when initial window is created center it
	CASE @IDCREATE
		CENTERWINDOW w
' X has been pressed on window, close it
	CASE @IDCLOSEWINDOW
	CASE& @IDDESTROY
		CLOSEWINDOW w
	CASE @IDCONTROL
		SELECT @CONTROLID
			CASE D1_B1
				IF @NOTIFYCODE = 0
					Process()
				ENDIF
			CASE D1_B2
				IF @NOTIFYCODE = 0
					CLOSEWINDOW w
				ENDIF
		ENDSELECT
ENDSELECT
RETURN 0
ENDSUB

'============
SUB Process()
'============
wext = "." + UCASE$(GETCONTROLTEXT(w,D1_E1))
wlen = LEN(wext)
idir = "C:\Documents and Settings\\Bill\\My Documents\\Calibre Library\\"
hdir = idir
retval = FolderRequest(0,"Starting Folder",idir,hdir,0)
IF retval <> TRUE THEN GOTO EOJ
odir = "C:\\Temp\\!NewStuff\\"
hdir = odir
retval = FolderRequest(0,"Output Folder",odir,hdir,0)
IF retval <> TRUE THEN GOTO EOJ
ProcessEXT(idir)
LABEL EOJ
RETURN
ENDSUB

'==========================
SUB ProcessEXT(sdir:string)
'==========================
DEF Wpathi[261]:istring
DEF Cfile[261]:istring
DEF dir, attrib, l, res:int

Wpathi = sdir + "*.*"
PrintW1("Wpathi="+Wpathi)
dir = FINDOPEN(Wpathi)
IF (dir)
	DO
		Cfile = FINDNEXT(dir,attrib)
		IF Cfile <> ""
			IF (attrib & @file_directory)
				IF Cfile <> "." AND Cfile <> ".." 
					ProcessEXT(sdir + Cfile + "\\")
				ENDIF
			ELSE
				IF UCASE$(RIGHT$(Cfile,wlen)) = wext
					res = RENAME(sdir + Cfile,odir + Cfile)
					IF (res = 1)
						l = 0
					ELSE
						PrintW1(Cfile + " Not Copied")
					ENDIF
				ENDIF
			ENDIF
		ENDIF
	UNTIL Cfile = ""
	FINDCLOSE dir
ENDIF
RETURN
ENDSUB

'========================================================================================
SUB FolderRequest(hWnd:uint, title:pointer, dir:pointer, initial:pointer, flags:uint),int
'========================================================================================
INT r = FALSE
ISTRING buffer[261]
UINT item_list
BROWSEINFO bi
RtlZeroMemory(&bi, LEN(bi))
bi.hOwner = hWnd
bi.lpszTitle = title
bi.ulFlags = flags
IF flags = 0 THEN bi.ulFlags = BIF_RETURNFSANCESTORS | BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE | BIF_DONTGOBELOWDOMAIN
bi.lpfn = &BrowseFolderCallback
bi.lParam = initial
' Display the browser.
item_list = SHBrowseForFolder(&bi)
IF item_list
	IF SHGetPathFromIDList(item_list, &buffer)
		IF MID$(buffer,LEN(buffer),1) <> "\\"
			#<STRING>dir = buffer + "\\"
		ELSE
			#<STRING>dir = buffer
		ENDIF
		r = TRUE
	ENDIF
	CoTaskMemFree(item_list)
ENDIF
RETURN r
ENDSUB

'===========================================================================
SUB BrowseFolderCallback(hWnd:uint, uMsg:uint, lParam:uint, lpData:uint),int
'===========================================================================
SELECT uMsg
	CASE BFFM_INITIALIZED
' Set start directory
		SendMessageA( hWnd, BFFM_SETSELECTION, TRUE, lpData )
	CASE BFFM_VALIDATEFAILED
		RETURN 1
ENDSELECT
RETURN 0
ENDSUB

'=============================
SUB PrintW1(ToPrint as string)
'=============================
indx++
row = 0
if (indx > 26)
	for i2 = 0 to 25
		OnScreen[i2] = OnScreen[i2 + 1]
	next i2
	OnScreen[26] = ToPrint
	indx = 26
else
	OnScreen[indx] = ToPrint
endif
SETWINDOWCOLOR w, BLACK
SETWINDOWCOLOR w, WHITE
for i2 = 0 to indx
	move w, 10, row
	row +=15
	print w, OnScreen[i2]
next i2
wait 1
RETURN
ENDSUB