Schemaphic Systems Blog

Sharing experiences in Flash, Flex and AIR

Writing & executing CMD scripts through AIR

leave a comment »

The following script writes a CMD file, executes it and disposes itself.

Here a cmd file named “hide.cmd” is getting created which sets the file attribute of test.txt to hidden and then deletes itseld (hide.cmd).

private var applicationDirectory:File;
public function createCMDFile():void
{
    applicationDirectory = File.desktopDirectory;
    var cmdFile:File = applicationDirectory.resolvePath( 'hide.cmd' );
    var stream:FileStream = new FileStream()
    stream.open( cmdFile, FileMode.WRITE );

    var dataString:String = "ATTRIB +H \" C:\My Documents\test.txt \"";

    stream.writeUTFBytes( dataString );
    stream.close();

    stream = null;

    var nativeProcessStartupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
    nativeProcessStartupInfo.executable = cmdFile;

    var process:NativeProcess = new NativeProcess();
    process.start(nativeProcessStartupInfo);
    process.addEventListener( NativeProcessExitEvent.EXIT, onExitHandler );
}

private function onExitHandler( event:NativeProcessExitEvent ):void
{
    var cmdFile:File = applicationDirectory.resolvePath( 'hide.cmd' );
    cmdFile.deleteFile();
}

Please Remember to set <supportedProfiles>extendedDesktop</supportedProfiles> in air descriptor file. Check AIR NativeProcess compatible SDK before compiling.

Advertisements

Written by Deepanjan Das

September 10, 2010 at 3:52 PM

Posted in AIR, Code Snippets

Tagged with

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: