VB icon

Move Files

Email
Submitted on: 10/27/2019 8:54:33 AM
By: Philippe RIO  
Level: Intermediate
User Rating: Unrated
Compatibility: C
Views: 195
author picture
 
     I have many cheerleaders images everywhere in my pc, they all were into one folder. I could not access to them using an image viewer, too many files (>200 000 !). So, I decided to create folders and to store 1000 files into each one. Making by hand was too long, so I wrote this program.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Move Files
// Description:I have many cheerleaders images everywhere in my pc, they all were into one folder. I could not access to them using an image viewer, too many files (>200 000 !). So, I decided to create folders and to store 1000 files into each one. Making by hand was too long, so I wrote this program.
// By: Philippe RIO
//**************************************

File : main.h
------------------------------------
// INCLUDE FILE generated by "Pelles C for Windows, version 1.00".
#define DLG_MAIN			1001
#define IDR_ICO_MAIN		2001
#define IDC_EDIT_01			4001
#define IDC_EDIT_02			4002
#define IDC_EDIT_03			4003
#define IDC_EDIT_04			4004
#define IDC_EDIT_05			4005
#define IDC_EDIT_06			4006
#define IDC_EDIT_07			4007
#define IDC_EDIT_08			4008
#define IDC_EDIT_09			4009
#define IDC_EDIT_10			4010
#define IDC_EDIT_11			4011
#define IDC_BUTTON_01		5001
#define IDC_BUTTON_02		5002
#define IDC_BUTTON_03		5003
#define IDC_BUTTON_04		5004
#define IDC_BUTTON_05		5005
#define IDC_BUTTON_06		5006
-----------------------------------------------------
File : MoveFiles.c
-----------------------------------------------------
#define WIN32_LEAN_AND_MEAN
#define NOCRYPT
#define NOSERVICE
#define NOMCX
#define NOIME
#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>
#include <tchar.h>
#include <Objbase.h>
#include <ShlWApi.h>
#include <Shlobj.h>
#include <Richedit.h>
#include "main.h"
char	szSourceFolder[MAX_PATH + 4] ;
char	szDestinationFolder[MAX_PATH + 4] ;
char	szError_001[]	=	"You must enter a valid source folder!" ;
char	szError_002[]	=	"You must enter a valid destination folder!" ;
char	szError_003[]	=	"You must enter a valid number!" ;
char	szError_004[]	=	"You must enter a valid folder prefix!" ;
static HANDLE hInstance ;
int Error(HWND __hWnd,LPSTR __lpszMessage)
{
	FLASHWINFO	_Flash ;
	_Flash.cbSize = sizeof(FLASHWINFO) ;
	_Flash.hwnd = __hWnd ;
	_Flash.dwFlags = FLASHW_ALL ;
	_Flash.uCount = 20 ;
	_Flash.dwTimeout = 0 ;
	FlashWindowEx(&_Flash) ;
	MessageBeep(MB_ICONERROR) ;
	MessageBox(__hWnd,__lpszMessage,"Are you dreaming?",MB_OK|MB_ICONERROR|MB_APPLMODAL|MB_TOPMOST) ;
	return (0) ;
}
LRESULT DlgCtlColor(HWND __hWnd,UINT __uMsg,WPARAM __wParam)
{
	switch (__uMsg)
	{
		case	WM_CTLCOLORSTATIC :	SetTextColor((HDC) __wParam,RGB(255,0,0));
							SetBkColor((HDC) __wParam,RGB(0,0,0));
									return ((LRESULT) GetStockObject(BLACK_BRUSH)) ;
		case	WM_CTLCOLORBTN :	SetTextColor((HDC) __wParam,RGB(255,0,255));
							SetBkColor((HDC) __wParam,RGB(0,0,0));
									return ((LRESULT) GetStockObject(BLACK_BRUSH)) ;
		case	WM_CTLCOLORLISTBOX :SetTextColor((HDC) __wParam,RGB(255,0,0));
							SetBkColor((HDC) __wParam,RGB(0,0,0));
									return ((LRESULT) GetStockObject(BLACK_BRUSH)) ;
		case	WM_CTLCOLOREDIT :	SetTextColor((HDC) __wParam,RGB(0,255,255));
							SetBkColor((HDC) __wParam,RGB(0,0,0));
									return ((LRESULT) GetStockObject(BLACK_BRUSH)) ;
		case	WM_CTLCOLORDLG :	SetTextColor((HDC) __wParam,RGB(0,0,255));
							SetBkColor((HDC) __wParam,RGB(0,0,0));
									return ((LRESULT) GetStockObject(BLACK_BRUSH)) ;
	}
	return (FALSE) ;
}
static int CALLBACK BrowseCallbackProc(HWND hwnd,UINT uMsg, LPARAM lParam, LPARAM lpData)
{
	// If the BFFM_INITIALIZED message is received
	// set the path to the start path.
	switch (uMsg)
	{
		case BFFM_INITIALIZED:
		{
			if (NULL != (void *) lpData)
			{
				SendMessage(hwnd, BFFM_SETSELECTION, TRUE, lpData);
			}
		}
	}
	return 0; // The function should always return 0.
}
// HWND is the parent window.
// szCurrent is an optional start folder. Can be NULL.
// szPath receives the selected path on success. Must be MAX_PATH characters in length.
BOOL BrowseForFolder(HWND hwnd,LPCTSTR szCurrent,LPTSTR szPath)
{
	BROWSEINFObi = { 0 };
	LPITEMIDLIST pidl;
	TCHARszDisplay[MAX_PATH];
	BOOL retval;
	CoInitialize(NULL);
	bi.hwndOwner = hwnd;
	bi.pszDisplayName = szDisplay;
	bi.lpszTitle = TEXT("Please choose a folder.");
	bi.ulFlags= BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE;
	bi.lpfn= BrowseCallbackProc;
	bi.lParam = (LPARAM) szCurrent;
	pidl = SHBrowseForFolder(&bi);
	if (NULL != pidl)
	{
		retval = SHGetPathFromIDList(pidl, szPath);
		CoTaskMemFree(pidl);
	}
	else
	{
		retval = FALSE;
	}
	if (!retval)
	{
		szPath[0] = TEXT('\0');
	}
	CoUninitialize();
	return retval;
}
void Reset(HWND __hWnd)
{
	SendDlgItemMessage(__hWnd,IDC_EDIT_01,EM_LIMITTEXT,MAX_PATH,0) ;
	SendDlgItemMessage(__hWnd,IDC_EDIT_02,EM_LIMITTEXT,MAX_PATH,0) ;
	SendDlgItemMessage(__hWnd,IDC_EDIT_03,EM_LIMITTEXT,4,0) ;
	SendDlgItemMessage(__hWnd,IDC_EDIT_04,EM_LIMITTEXT,4,0) ;
	SendDlgItemMessage(__hWnd,IDC_EDIT_05,EM_LIMITTEXT,8,0) ;
	SendDlgItemMessage(__hWnd,IDC_EDIT_06,EM_LIMITTEXT,8,0) ;
	SendDlgItemMessage(__hWnd,IDC_EDIT_07,EM_LIMITTEXT,5,0) ;
	SetDlgItemText(__hWnd,IDC_EDIT_01,"") ;
	SetDlgItemText(__hWnd,IDC_EDIT_02,"") ;
	SetDlgItemText(__hWnd,IDC_EDIT_03,"1000") ;
	SetDlgItemText(__hWnd,IDC_EDIT_04,"0") ;
	SetDlgItemText(__hWnd,IDC_EDIT_05,"0") ;
	SetDlgItemText(__hWnd,IDC_EDIT_06,"0") ;
	SetDlgItemText(__hWnd,IDC_EDIT_07,"Dir_") ;
	SetDlgItemText(__hWnd,IDC_EDIT_08,"0") ;
	SetDlgItemText(__hWnd,IDC_EDIT_09,"0") ;
	return ;
}
void StartMove(HWND __hWnd,LPSTR __lpszDestinationFolder,LPSTR __lpszSourceFolder,int __NbFilesPerFolder,int __NbCounterStart,LPSTR __lpszFolderPrefix)
{
	WIN32_FIND_DATA	_Wf ;
	HANDLE			_hFind ;
	char			_szSrc[MAX_PATH] ;
	char			_szDst[MAX_PATH] ;
	char			_szDir[MAX_PATH] ;
	char			_szInitialDirectory[MAX_PATH] ;
	int				_iCountFile ;
	int				_iCountDir ;
	int				_iNbFilesOk ;
	int				_iNbFilesKo ;
	_iNbFilesOk = 0 ;
	_iNbFilesKo = 0 ;
	_iCountFile = 0 ;
	_iCountDir = __NbCounterStart ;
	if(_iCountDir < 0)	_iCountDir = 0 ;
	memset(_szSrc,0,MAX_PATH) ;
	memset(_szDst,0,MAX_PATH) ;
	memset(_szDir,0,MAX_PATH) ;
	memset(_szInitialDirectory,0,MAX_PATH) ;
	GetCurrentDirectory(MAX_PATH,_szInitialDirectory) ;
	SetCurrentDirectory(__lpszSourceFolder) ;
	_iCountFile = __NbFilesPerFolder ;
	_hFind = FindFirstFile("*.*",&_Wf) ;
	if(_hFind == INVALID_HANDLE_VALUE)
		return ;
	do
	{
		if(_iCountFile == __NbFilesPerFolder)
		{
			_iCountFile = 0 ;
			wsprintf(_szDir,"%s\\%s%8.8d",__lpszSourceFolder,__lpszFolderPrefix,_iCountDir) ;
			CreateDirectory(_szDir,NULL) ;
			_iCountDir++ ;
			SetDlgItemInt(__hWnd,IDC_EDIT_05,_iCountDir,0) ;
		}
		if(_Wf.cFileName[0])
		{
			if(_Wf.cFileName[0] != '.')
			{
				if(_Wf.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY)
				{
					wsprintf(_szSrc,"%s\\%s",__lpszSourceFolder,_Wf.cFileName) ;
					wsprintf(_szDst,"%s\\%s",_szDir,_Wf.cFileName) ;
					if(MoveFileEx(_szSrc,_szDst,MOVEFILE_REPLACE_EXISTING|MOVEFILE_WRITE_THROUGH|MOVEFILE_COPY_ALLOWED))
					{
						_iNbFilesOk++ ;
						SetDlgItemInt(__hWnd,IDC_EDIT_08,_iNbFilesOk,0) ;
					}
					else
					{
						_iNbFilesKo++ ;
						SetDlgItemInt(__hWnd,IDC_EDIT_09,_iNbFilesKo,0) ;
					}
					SetDlgItemInt(__hWnd,IDC_EDIT_06,_iCountFile + 1,0) ;
					_iCountFile++ ;
				}
			}
		}
	} while(FindNextFile(_hFind,&_Wf)) ;
	CloseHandle(_hFind) ;
	SetCurrentDirectory(_szInitialDirectory) ;
	Reset(__hWnd) ;
	SetDlgItemText(__hWnd,IDC_EDIT_01,"...Finished...") ;
	wsprintf(_szSrc,"%d files copied successfully",_iNbFilesOk) ;
	Error(__hWnd,_szSrc) ;
	return ;
}
void ShowHiddenControls(HWND __hWnd)
{
	ShowWindow(GetDlgItem(__hWnd,IDOK),SW_SHOW) ;
	ShowWindow(GetDlgItem(__hWnd,IDCANCEL),SW_SHOW) ;
	ShowWindow(GetDlgItem(__hWnd,IDC_BUTTON_01),SW_SHOW) ;
	ShowWindow(GetDlgItem(__hWnd,IDC_BUTTON_02),SW_SHOW) ;
	ShowWindow(GetDlgItem(__hWnd,IDC_BUTTON_03),SW_SHOW) ;
	ShowWindow(GetDlgItem(__hWnd,IDC_BUTTON_04),SW_SHOW) ;
	ShowWindow(GetDlgItem(__hWnd,IDC_BUTTON_05),SW_SHOW) ;
	ShowWindow(GetDlgItem(__hWnd,IDC_BUTTON_06),SW_SHOW) ;
	return ;
}
void Go(HWND __hWnd)
{
	char	_szFolderPrefix[16] ;
	int		_NbFilesPerFolder, _NbCounterStart ;
	ShowWindow(GetDlgItem(__hWnd,IDOK),SW_HIDE) ;
	ShowWindow(GetDlgItem(__hWnd,IDCANCEL),SW_HIDE) ;
	ShowWindow(GetDlgItem(__hWnd,IDC_BUTTON_01),SW_HIDE) ;
	ShowWindow(GetDlgItem(__hWnd,IDC_BUTTON_02),SW_HIDE) ;
	ShowWindow(GetDlgItem(__hWnd,IDC_BUTTON_03),SW_HIDE) ;
	ShowWindow(GetDlgItem(__hWnd,IDC_BUTTON_04),SW_HIDE) ;
	ShowWindow(GetDlgItem(__hWnd,IDC_BUTTON_05),SW_HIDE) ;
	ShowWindow(GetDlgItem(__hWnd,IDC_BUTTON_06),SW_HIDE) ;
	memset(_szFolderPrefix,0,sizeof(_szFolderPrefix)) ;
	memset(szSourceFolder,0,sizeof(szSourceFolder)) ;
	memset(szDestinationFolder,0,sizeof(szDestinationFolder)) ;
	_NbFilesPerFolder = 0 ;
	_NbCounterStart = 0 ;
	if(GetDlgItemText(__hWnd,IDC_EDIT_01,szSourceFolder,MAX_PATH) == 0)
	{
		SetFocus(GetDlgItem(__hWnd,IDC_EDIT_01)) ;
		Error(__hWnd,szError_001) ;
		ShowHiddenControls(__hWnd) ;
		return ;
	}
	PathRemoveBackslash(szSourceFolder) ;
	if(!PathFileExists(szSourceFolder))
	{
		SetFocus(GetDlgItem(__hWnd,IDC_EDIT_01)) ;
		Error(__hWnd,szError_001) ;
		ShowHiddenControls(__hWnd) ;
		return ;
	}
	if(!PathIsDirectory(szSourceFolder))
	{
		SetFocus(GetDlgItem(__hWnd,IDC_EDIT_01)) ;
		Error(__hWnd,szError_001) ;
		ShowHiddenControls(__hWnd) ;
		return ;
	}
	if(GetDlgItemText(__hWnd,IDC_EDIT_02,szDestinationFolder,MAX_PATH) == 0)
	{
		SetFocus(GetDlgItem(__hWnd,IDC_EDIT_02)) ;
		Error(__hWnd,szError_002) ;
		ShowHiddenControls(__hWnd) ;
		return ;
	}
	PathRemoveBackslash(szDestinationFolder) ;
	if(!PathFileExists(szDestinationFolder))
	{
		SetFocus(GetDlgItem(__hWnd,IDC_EDIT_02)) ;
		Error(__hWnd,szError_002) ;
		ShowHiddenControls(__hWnd) ;
		return ;
	}
	if(!PathIsDirectory(szDestinationFolder))
	{
		SetFocus(GetDlgItem(__hWnd,IDC_EDIT_02)) ;
		Error(__hWnd,szError_002) ;
		ShowHiddenControls(__hWnd) ;
		return ;
	}
	_NbFilesPerFolder = GetDlgItemInt(__hWnd,IDC_EDIT_03,NULL,0) ;
	if(!_NbFilesPerFolder)
	{
		SetFocus(GetDlgItem(__hWnd,IDC_EDIT_03)) ;
		Error(__hWnd,szError_003) ;
		ShowHiddenControls(__hWnd) ;
		return ;
	}
	_NbCounterStart = GetDlgItemInt(__hWnd,IDC_EDIT_04,NULL,0) ;
	if(GetDlgItemText(__hWnd,IDC_EDIT_07,_szFolderPrefix,5) == 0)
	{
		SetFocus(GetDlgItem(__hWnd,IDC_EDIT_07)) ;
		Error(__hWnd,szError_004) ;
		ShowHiddenControls(__hWnd) ;
		return ;
	}
	StartMove(__hWnd,szDestinationFolder,szSourceFolder,_NbFilesPerFolder,_NbCounterStart,_szFolderPrefix) ;
	ShowHiddenControls(__hWnd) ;
	return ;
}
static INT_PTR CALLBACK MainDlgProc(HWND __hWnd,UINT __uMsg,WPARAM __wParam,LPARAM __lParam)
{
	HANDLE	_hThread ;
	char	_szTmp[256] ;
switch(__uMsg)
{
		case	WM_CTLCOLORSTATIC :
		case	WM_CTLCOLORBTN :
		case	WM_CTLCOLORLISTBOX :
		case	WM_CTLCOLOREDIT :
		case	WM_CTLCOLORDLG :
									return (DlgCtlColor(__hWnd,__uMsg,__wParam)) ;
case	WM_INITDIALOG :	Reset(__hWnd) ;
					return(TRUE) ;
case	WM_COMMAND :	switch(GET_WM_COMMAND_ID(__wParam,__lParam))
					{
									case	IDC_BUTTON_01 :	BrowseForFolder(__hWnd,szSourceFolder,szSourceFolder) ;
															SetDlgItemText(__hWnd,IDC_EDIT_01,szSourceFolder) ;
															break ;
									case	IDC_BUTTON_02 :	GetDlgItemText(__hWnd,IDC_EDIT_01,_szTmp,sizeof(_szTmp)) ;
															SetDlgItemText(__hWnd,IDC_EDIT_02,_szTmp) ;
															break ;
									case	IDC_BUTTON_03 :	BrowseForFolder(__hWnd,szDestinationFolder,szDestinationFolder) ;
															SetDlgItemText(__hWnd,IDC_EDIT_02,szDestinationFolder) ;
															break ;
									case	IDC_BUTTON_04 :	Reset(__hWnd) ;
															break ;
									case	IDC_BUTTON_05 :	SetDlgItemText(__hWnd,IDC_EDIT_01,"") ;
															SendDlgItemMessage(__hWnd,IDC_EDIT_01,WM_PASTE,0,0) ;
															break ;
									case	IDC_BUTTON_06 :	SetDlgItemText(__hWnd,IDC_EDIT_02,"") ;
															SendDlgItemMessage(__hWnd,IDC_EDIT_02,WM_PASTE,0,0) ;
															break ;
									case	IDOK :			_hThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE) Go,__hWnd,0,NULL) ;
															CloseHandle(_hThread) ;
															break ;
					case	IDCANCEL :		EndDialog(__hWnd,TRUE) ;
										return(TRUE) ;
					}
					break ;
case	WM_CLOSE :		EndDialog(__hWnd,0) ;
								return(TRUE) ;
}
return(FALSE) ;
}
int PASCAL WinMain(HINSTANCE __hInstance,HINSTANCE __hPrevInstance,LPSTR __lpszCmdLine,int __nCmdShow)
{
INITCOMMONCONTROLSEX	_Icc ;
WNDCLASSEX				_DlgClass ;
hInstance = __hInstance ;
_Icc.dwSize = sizeof(_Icc) ;
_Icc.dwICC = ICC_WIN95_CLASSES ;
InitCommonControlsEx(&_Icc) ;
_DlgClass.cbSize = sizeof(_DlgClass) ;
if(!GetClassInfoEx(NULL,MAKEINTRESOURCE(32770),&_DlgClass))
return(0) ;
_DlgClass.hInstance = hInstance ;
_DlgClass.hIcon = LoadIcon(hInstance,MAKEINTRESOURCE(IDR_ICO_MAIN)) ;
_DlgClass.lpszClassName = _T("MoveFileClass") ;
if(!RegisterClassEx(&_DlgClass))
return(0) ;
return(DialogBox(hInstance,MAKEINTRESOURCE(DLG_MAIN),NULL,(DLGPROC) MainDlgProc)) ;
}
-------------------------------------------


Other 5 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.