Tuesday, March 11, 2014

C++/VC++ Program to Move a File to Folder and Move a Folder to Folder

//MoveTest.CPP
#include<windows.h>
#include<iostream>
#include<conio.h>
#include<tchar.h>
using namespace std;
void MoveFiletonewLoc(void);
void MoveFoldertonewLoc(void);
int main()
{
int choice;
cout<<"\n********File and Directory mover*********\n\n";
cout<<"1.Move File to another location\n";
cout<<"2.Move Folder to another location";
cout<<"\n\n Enter your choice:";
cin>>choice;
switch(choice)
{
case 1:MoveFiletonewLoc();
break;
case 2:MoveFoldertonewLoc();
break;
   default:cout<<"\nWrong Choice";
            exit(0);
}
getch();
return 0;
}

void MoveFiletonewLoc(void)
{
TCHAR Source[256];
memset(Source,0,256);
    TCHAR Destination[256];
memset(Destination,0,256);
_tprintf("\n\nEnter the file location:");
_tscanf("%s",Source);
_tprintf("\n\nEnter the destination location:");
_tscanf("%s",Destination);
int i=MoveFile(Source,Destination);//Moves Files to Folder
if(i>0)
{
_tprintf("\n\nFile moved to location %s Sucessfully",Destination);
}
else
{
_tprintf("\n\nFile moved to location %s UnSucessful",Destination);
}
}

void MoveFoldertonewLoc(void)
{
TCHAR Source[256];
memset(Source,0,256);
    TCHAR Destination[256];
memset(Destination,0,256);
_tprintf("\n\nEnter the folder location:");
_tscanf("%s",Source);
_tprintf("\n\nEnter the destination location:");
_tscanf("%s",Destination);
SHFILEOPSTRUCT fop={};
fop.wFunc=FO_MOVE;
fop.pFrom=Source;
fop.pTo=Destination;
if(!SHFileOperation(&fop))//Moves Folder to Folder
{
_tprintf("\n\nFolder moved to location %s Sucessfully",Destination);
}
else
{
_tprintf("\n\nFolder moved to location %s UnSucessful",Destination);
}
}

OUTPUT:




No comments:

Post a Comment