论坛首页 Web前端技术论坛

输出一个目录下文件名相同的文件

浏览 1495 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-04-24   最后修改:2009-04-30

工作需要查看某工程下文件名相同的文件,遂写下此应用...

air提供了很多个文件系统操作控件,之前也没接触过air对目录和文件的操作,查了查api就coding了....

 

 

 

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml"
						layout="vertical"  backgroundGradientColors="[0x000000,0x323232]">
	<mx:Script>
		<![CDATA[
			import mx.collections.ArrayCollection;
			import mx.events.FileEvent;


			private var fileCollection:ArrayCollection=new ArrayCollection();

			private var fileNameCollection:ArrayCollection=new ArrayCollection();

			private var currentdir:File;

			private var indexArray:Array;

			[Bindable]
			private var text:String="";

			private function changeHandler():void
			{
				currentdir=tree.selectedItem as File;


			}

			private function output(dir:File):void
			{

				var contents:Array=dir.getDirectoryListing();

				for each (var item:File in contents)
				{
					//目录
					if (item.isDirectory)
					{
						output(item);
					}
					else //文件
					{
						fileCollection.addItem(item);
						fileNameCollection.addItem(item.name);
					}

				}

			}



			//files had already stored in collection
			private function print():void
			{

				indexArray=new Array();

				for (var i=0; i < fileNameCollection.length; i++)
				{
					for (var j=i + 1; j < fileNameCollection.length; j++)
					{


						if (fileNameCollection[i] == fileNameCollection[j])
						{

							if (checkIndex(i))
							{
								indexArray.push(i);
								text+=File(fileCollection.getItemAt(i)).nativePath + "\n";
							}

							if (checkIndex(j))
							{
								indexArray.push(j);
								text+=File(fileCollection.getItemAt(j)).nativePath + "\n";
							}
						}
					}

				}



			}


			private function checkIndex(index:int):Boolean
			{

				for (var i in indexArray)
				{
					if (i == index)
					{
						return false;
					}
				}
				return true;
			}



			private function clickHandler(event:MouseEvent):void
			{
				output(currentdir);
				print();
			}
		]]>
	</mx:Script>
	<mx:FileSystemTree id="tree"
					   itemClick="changeHandler()"
					   width="300"
					   height="100"/>
	<mx:TextArea width="300"
				 height="150"
				 text="{text}"/>
	<mx:Button label="print"
			   click="clickHandler(event);"/>

</mx:WindowedApplication>

 

论坛首页 Web前端技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics