Tuesday 19 January 2016

Move File From One Folder To Another Folder Using X++

Hi Guys . I hope you are doing Good.
Today i’m going to share you that , how to move a File from One Folder to another folder using X++.
Here we can achieve this with the Help of WINAPI(Windows Application Programming Interface). Even though we can do Copy a file, delete a file, Create Directory, getting the file Size, etc. Refer more Details WINAPI
static void MoveFilesFromFoldertoAnotherFolder(Args _args)
{
 FilenameOpen fileNameOpen;
 DialogField dialogFileName;
 Dialog dialog;
 Filename filePath;
 Filename fileName;
 Filename fileType;
 FileName DestinationPath;
 #File
 
 ;
DestinationPath = @'C:\Users\saadullah\Desktop\Dest\'; // Define your Destination Path
 dialog = new Dialog("Move Files");
// AX 2009
 dialogFilename = dialog.addField(typeId(FileNameOpen));
// AX 2012
 // dialogFileName = dialog.addField(extendedTypeStr(FileNameOpen));
dialog.filenameLookupFilter([#AllFilesType]);
 dialog.filenameLookupTitle("Select File");
 dialog.caption("Move File");
 dialogFilename.value(fileName);
if(!dialog.run())
 return;
filenameOpen = dialogFilename.value();
 [filePath, fileName, fileType] = fileNameSplit(fileNameOpen);
// MoveFile - The Original File won't be available once it's moved into destination path
WinAPI::moveFile(fileNameOpen, DestinationPath+FileName+FileType);
// CopyFile - The Original File will be available even if it's moved into destination path
 //WinAPI::copyFile(fileNameOpen, DestinationPath+FileName+FileType);
// DeleteFile - Delete the Selected File
 //WinAPI::deleteFile(fileNameOpen);
 
 // If u need to Use this in Form Take look in AOT > Forms > Tutorial_Form_File
}