Axapta’s WinAPI class has a bunch of static methods to handle files. It is a very useful class when handling files that need to be accessed outside of the AX environment
The examples below show how to utilize some of these methods.
1) Create a dialog which takes source and destination file name and file path respectively and on click of OK button COPY the file from source folder to destination folder.
class example_WINAPI extends RunBase
{
FilenameOpen fileNameOpen;
Filename filename;
Filename filePath;
Filename fileType;
Filename destPath;
DialogField dialogSource;
DialogField dialogDestination;
str sourceFileName;
str destFileName;
}
{
FilenameOpen fileNameOpen;
Filename filename;
Filename filePath;
Filename fileType;
Filename destPath;
DialogField dialogSource;
DialogField dialogDestination;
str sourceFileName;
str destFileName;
}
public static void main(Args args)
{
example_WINAPI fileDialog = new example_WINAPI();
fileDialog.createDialog();
}
public void createDialog()
{
Dialog dialog;
dialog = new Dialog("Move files");
dialog.caption('Move file');
dialogSource = dialog.addField(extendedTypeStr(FilenameOpen), "@SYS1425");
dialogDestination = dialog.addField(extendedTypeStr(FilePath), "@SYS37735");
dialogSource.value(filename);
if(dialog.run())
{
this.copyFile();
}
}
public void copyFile()
{
sourcefileName = dialogSource.value();
[filePath, fileName, fileType] = fileNameSplit(sourcefileName);
destPath = dialogDestination.value();
destFileName = destPath + "\\" + conPeek(fileNameSplit(sourceFileName),2) + conPeek(fileNameSplit(sourceFileName),3);
info(queryValue(destFileName));
WinAPI::copyFile(sourceFileName, destFileName);
}
2) Create a dialog which takes source and destination file name and file path respectively and on click of OK button MOVE the file from source folder to destination folder.
Add one more method moveFile to the above class and call it from createDialog method
public void moveFile()
{
sourcefileName = dialogSource.value();
if(WinAPI::fileExists(sourceFileName))
{
[filePath, fileName, fileType] = fileNameSplit(sourcefileName);
destPath = dialogDestination.value();
destFileName = destPath + "\\" + conPeek(fileNameSplit(sourceFileName),2) + conPeek(fileNameSplit(sourceFileName),3);
WinAPI::moveFile(sourceFileName, destFileName);
}
else
{
warning("This file doesnot exist in the specified path");
}
}
NOTE :
In the above method one more method fileExists is used from WINAPI class to check whether the specified file is present in the path or not. If present then executes the code else throws the warning message.
3) Delete a file in a specified path :
WINAPI::deleteFile(fileName);
No comments:
Post a Comment