class ÊÇÒª´´½¨µÄÓ¦ÓóÌÐòÃû³ÆºÍÀà¡£
servername ÒªÔÚÆäÉÏ´´½¨¶ÔÏóµÄÍøÂç·þÎñÆ÷Ãû³Æ¡££¨Èç¹ûÒªÔÚÔ¶³Ì¼ÆËã»úÉÏ´´½¨¶ÔÏó²ÅÓã© class ²ÎÊýʹÓà appname.objecttype ÕâÖÖÓï·¨£¬°üÀ¨ÒÔϲ¿·Ö£º appname ±ØÐèµÄ£»Ìṩ¸Ã¶ÔÏóµÄÓ¦ÓóÌÐòÃû¡£ objecttype ±ØÐèµÄ£»´ý´´½¨¶ÔÏóµÄÀàÐÍ»òÀà¡£
Òò´Ë£¬ÎÒÃÇÓÃÏÂÃæµÄ´úÂë´´½¨FileSystemObject¶ÔÏó: Dim fso As Object
Set fso = CreateObject(\
ScriptingÊÇÀàÐÍ¿âµÄÃû³Æ£¬FileSystemObject¾ÍÊÇÒª´´½¨µÄ¶ÔÏóµÄÃû×Ö¡£ ͬÑùÎÒÃÇ¿ÉÒÔ´´½¨Dictionary ¶ÔÏóÈçÏ£º Dim d
Set d = CreateObject(\
£¨¶þ£©FileSystemObject¶ÔÏóµÄ·½·¨
FileSystemObject¶ÔÏóÄ£ÐÍÖÐÓÐЩ¹¦ÄÜÊÇÖظ´µÄ£¬Èç¿ÉÓÃFileSystemObject¶ÔÏóµÄCpoyFile·½·¨£¬Ò²¿ÉÓÃFile¶ÔÏóµÄCopy·½·¨À´¸´ÖÆÎļþ¡£ÏÂÃæÏȽéÉÜFileSystemObject¶ÔÏóµÄ·½·¨¡£ 1¡¢GetDrive ·½·¨
Óï·¨£ºobject.GetDrive drivespec
drivespec²ÎÊý¿ÉÒÔÊÇÒ»¸öÇý¶¯Æ÷×Ö·û£¨c£©¡¢Ò»¸öÇý¶¯Æ÷×Ö·û¼ÓÒ»¸öðºÅ£¨c:£©¡¢Ò»¸öÇý¶¯Æ÷×Ö·û¼ÓðºÅºÍ·¾¶·Ö¸ô·û£¨c:\\£©»òÈκÎÍøÂç¹²ÏíµÄ˵Ã÷£¨\\\\computer2\\share1£©¡£ ×÷Ó㺷µ»ØÒ»¸öÓëÖ¸¶¨Â·¾¶ÖеÄÇý¶¯Æ÷Ïà¶ÔÓ¦µÄ Drive ¶ÔÏó¡£ Ê¾Àý£º Dim d
Set d = fso.GetDrive(\±äÁ¿d¾Í´ú±íÁËÇý¶¯Æ÷D¶ÔÏó
Èç¹û drivespec ²»·ûºÏÈκÎÒ»ÖÖ¿ÉÒÔ½ÓÊܵÄÐÎʽ»òÕß²»´æÔÚ£¬Ôò·¢ÉúÒ»¸ö´íÎó¡£ ×¢Ò⣺Ϊ¼ò½à£¬Ê¾ÀýÖж¼¼Ù¶¨fsoÊÇÒѾ´´½¨µÄFileSystemObject¶ÔÏó 2¡¢GetDriveName ·½·¨
Óï·¨£ºobject.GetDriveName(path)
×÷Ó㺷µ»ØÒ»¸ö°üº¬Ö¸¶¨Â·¾¶µÄÇý¶¯Æ÷Ãû×ÖµÄ×Ö·û´®¡£ ʾÀý£º
Debug.Print fso.GetDriveName(\Á¢¼´´°¿ÚÏÔʾ\3¡¢GetExtensionName ·½·¨
Óï·¨£ºobject.GetExtensionName(path)
×÷Ó㺷µ»ØÒ»¸ö°üº¬Â·¾¶ÖÐ×îºó²¿¼þÀ©Õ¹ÃûµÄ×Ö·û´®¡£ ʾÀý£º
Debug.Print fso.GetExtensionName(\Á¢¼´´°¿ÚÏÔʾ\4¡¢GetBaseName ·½·¨
Óï·¨£ºobject.GetBaseName(path)
×÷Ó㺷µ»ØÒ»¸ö°üº¬Â·¾¶ÖÐ×îºó²¿¼þµÄ»ù±¾Ãû×Ö£¨È¥µôÈκÎÎļþÀ©Õ¹Ãû£©µÄ×Ö·û´®¡£ ʾÀý£º
Debug.Print fso.GetBaseName(\Á¢¼´´°¿ÚÏÔʾ\5¡¢GetAbsolutePathName ·½·¨
Óï·¨£ºobject.GetAbsolutePathName(pathspec)
×÷Ó㺴ÓÌṩµÄ·¾¶ËµÃ÷Öзµ»ØÒ»¸öÍêÕû¡¢Ã÷È·µÄ·¾¶¡£
ʾÀý£º
Èç¹ûpathspecΪ¿Õ×Ö·û´®\£¬Ôò·µ»Øµ±Ç°Â·¾¶¡£¼ÙÉ赱ǰ·¾¶ÎªC:\\Documents and Settings\\yc\\My Documents
Debug.Print fs.GetAbsolutePathName(\ÏÔʾC:\\Documents and Settings\\yc\\My Documents Debug.Print fs.GetAbsolutePathName(\ÏÔʾC:\\Documents and Settings\\yc£¬¼´ÉϲãĿ¼ Debug.Print fs.GetAbsolutePathName(\ÏÔʾC:\\Documents and Settings\\yc\\My Documents\\abc
Debug.Print fs.GetAbsolutePathName(\ÏÔʾC:\\test.txt 6¡¢GetFile ·½·¨
Óï·¨£ºobject.GetFile(filespec)
×÷Ó㺷µ»ØÒ»¸öºÍÖ¸¶¨Â·¾¶ÖÐÎļþÏà¶ÔÓ¦µÄ File ¶ÔÏó¡£ Ê¾Àý£º Dim f
Set f = fso.GetFile(\±äÁ¿f¾Í´ú±íÁËÎļþtest.txt¶ÔÏó ×¢Ò⣺Èç¹ûÖ¸¶¨µÄÎļþ²»´æÔÚ£¬Ôò·¢ÉúÒ»¸ö´íÎó¡£ 7¡¢GetFileName ·½·¨
Óï·¨£ºobject.GetFileName(pathspec)
×÷Ó㺷µ»ØÖ¸¶¨Â·¾¶ÖеÄ×îºó²¿¼þ£¬¸Ã·¾¶²»ÊÇÇý¶¯Æ÷˵Ã÷µÄÒ»²¿·Ö¡£ ʾÀý£º
Debug.Print fso.GetFileName(\Á¢¼´´°¿ÚÏÔʾ\8¡¢GetFolder ·½·¨
Óï·¨£ºobject.GetFolder(folderspec)
×÷Ó㺷µ»ØÒ»¸öºÍÖ¸¶¨Â·¾¶ÖÐÎļþ¼ÐÏà¶ÔÓ¦µÄ Folder ¶ÔÏó¡£ Ê¾Àý£º Dim fd
Set fd = fso.GetFolder(\±äÁ¿f¾Í´ú±íÁËÎļþ¼Ðwindows¶ÔÏó ×¢Ò⣺Èç¹ûÖ¸¶¨µÄÎļþ¼Ð²»´æÔÚ£¬Ôò·¢ÉúÒ»¸ö´íÎó¡£ 9¡¢GetSpecialFolder ·½·¨
Óï·¨£ºobject.GetSpecialFolder(folderspec) ×÷Ó㺷µ»ØÖ¸¶¨µÄÌØÊâÎļþ¼Ð¡£ ˵Ã÷£º
folderspec ²ÎÊý¿ÉΪÈκεÄÏÂÁÐÖµ£º
WindowsFolder 0 Windows Îļþ¼Ð£¬°üº¬ÓÉ Windows ²Ù×÷ϵͳ°²×°µÄÎļþ¡£ SystemFolder 1 ϵͳÎļþ¼Ð£¬°üº¬¿â¡¢×ÖÌå¡¢É豸Çý¶¯³ÌÐò¡£
TemporaryFolder 2 Temp Îļþ¼Ð£¬ÓÃÓÚ´æ´¢ÁÙʱÎļþ¡£ËüµÄ·¾¶ÔÚ TMP »·¾³±äÁ¿ÖС£ 10¡¢GetParentFolderName ·½·¨
Óï·¨£ºobject.GetParentFolderName(path)
×÷Ó㺷µ»ØÒ»¸ö°üº¬Ö¸¶¨Â·¾¶×îºó²¿¼þ¸¸Îļþ¼ÐÃû×ÖµÄ×Ö·û´®¡£ ʾÀý£º
Debug.Print fso.GetParentFolderName(\ÏÔʾ\11¡¢GetTempName ·½·¨ Óï·¨£ºobject.GetTempName ×÷Ó㺷µ»ØÒ»¸öËæ»ú²úÉúµÄÁÙʱÎļþ»òÎļþ¼ÐµÄÃû×Ö£¬¸ÃÃû×ÖÔÚÖ´ÐÐÐèÒªÁÙʱÎļþ»òÎļþ¼ÐµÄ²Ù×÷ʱÓÐÓá£
˵Ã÷£ºGetTempName ·½·¨²»²úÉúÒ»¸öÎļþ£¬Ëü½öÌṩһ¸öÁÙʱÎļþÃû×Ö£¬¸ÃÃû×ֿɱ» CreateTextFile ÓÃÓÚ´´½¨Ò»¸öÎļþ¡£ ʾÀý£º
Debug.Print fso.GetTempName 'ÏÔʾ\ÿ´Î¶¼»á±ä¡£ 12¡¢BuildPath ·½·¨
Óï·¨£ºobject.BuildPath(path, name)
×÷Óãº×·¼ÓÒ»¸öÃû×Öµ½Ò»¸öÒѾ´æÔڵķ¾¶¡£ ʾÀý£º
Debug.Print fso.BuildPath(\ÏÔʾ\13¡¢CreateFolder ·½·¨
Óï·¨£ºobject.CreateFolder(foldername) ×÷Ó㺴´½¨Ò»¸öÎļþ¼Ð¡£
×¢Ò⣺Èç¹ûÖ¸¶¨µÄÎļþ¼ÐÒѾ´æÔÚ£¬Ôò·¢ÉúÒ»¸ö´íÎó¡£ Ê¾Àý£º
fso.CreateFolder(\ÔÚCÅÌ´´½¨Ò»¸ömyfolderÎļþ¼Ð 14¡¢CopyFolder ·½·¨
Óï·¨£ºobject.CopyFolder source, destination[, overwrite]
source ±ØÐèµÄ¡£Ö¸Ã÷Ò»¸ö»ò¶à¸ö±»¸´ÖÆÎļþ¼ÐµÄ×Ö·û´®Îļþ¼Ð˵Ã÷£¬¿ÉÒÔ°üÀ¨Í¨Åä·û¡£ destination ±ØÐèµÄ¡£Ö¸Ã÷ source Öб»¸´ÖÆÎļþ¼ÐºÍ×ÓÎļþ¼ÐµÄ½ÓÊܶ˵Ä×Ö·û´®£¬²»ÔÊÐíÓÐͨÅä·û¡£
overwrite ¿ÉÑ¡µÄ¡£Boolean Öµ£¬Ëü±íʾÒÑ´æÔÚµÄÎļþ¼ÐÊÇ·ñ±»¸²¸Ç¡£Èç¹ûΪ True£¬Îļþ±»¸²¸Ç¡£Èç¹ûΪ False£¬Îļþ²»±»¸²¸Ç¡£È±Ê¡ÖµÎª True¡£ ×÷Ó㺸´ÖÆÒ»¸öÎļþ¼Ðµ½ÁíÒ»¸öµØ·½¡£ ˵Ã÷£º
¢Ù ͨÅä·û½ö¿ÉÓÃÓÚ source ²ÎÊýµÄ×îºóÒ»¸ö·¾¶²¿¼þ¡£
ÀýÈ磺fso.CopyFolder \ÕâÊÇ¿ÉÒԵġ£ µ«²»ÄÜÕâÑù£ºfso.CopyFolder \¢Ú Èç¹û source °üº¬Í¨Åä·û»ò destination ÒÔ·¾¶·Ö¸ô·û£¨\\£©Îª½á⣬ÔòÈÏΪ destination ÊÇÒ»¸öÒÑ´æÔÚµÄÎļþ¼Ð£¬ÔÚÆäÖи´ÖÆÏàÆ¥ÅäµÄÎļþ¼ÐºÍ×ÓÎļþ¼Ð¡£·ñÔòÈÏΪ destination ÊÇÒ»¸öÒª´´½¨µÄÎļþ¼ÐµÄÃû×Ö¡£
ÀýÈ磺fso.copyfolder \
Èç¹ûFÅÌûÓÐabcÎļþ¼Ð£¬½«·¢Éú´íÎó¡£Èç¹û´æÔÚ£¬¿É¿´µ½abcÎļþ¼ÐÀïÓÐtmpÎļþ¼Ð¡£ ¼ÙÈçд³ÉÕâÑù£ºfso.copyfolder \
´ËʱÈôabc²»´æÔÚ£¬½«´´½¨abcÎļþ¼Ð£¬ÇÒ½«tmpÎļþ¼ÐÀïµÄÄÚÈݸ´ÖƵ½abcÎļþ¼ÐÀ¶ø²»ÊÇtmpÎļþ¼Ð£¬Ö»ÓÐabcÊÇÒ»¸öÒѾ´æÔÚµÄÎļþ¼Ðʱ£¬²Å¸´ÖÆÕû¸ötmpÎļþ¼Ðµ½abcÎļþ¼ÐÀï¡£
¢ÛÈç¹û destination ÊÇÒ»¸öÒÑ´æÔÚµÄÎļþ£¬Ôò·¢ÉúÒ»¸ö´íÎó¡£
¢ÜÈç¹û destination ÊÇÒ»¸öĿ¼£¬Ëü½«³¢ÊÔ¸´ÖÆÎļþ¼ÐºÍËüËùÓеÄÄÚÈÝ¡£Èç¹ûÒ»¸ö°üº¬ÔÚ source µÄÎļþÒÑÔÚ destination ÖдæÔÚ£¬µ± overwrite Ϊ False ʱ·¢ÉúÒ»¸ö´íÎ󣬷ñÔòËü½«³¢ÊÔ¸²¸ÇÕâ¸öÎļþ¡£
¢ÝÈç¹û destination ÊÇÒ»¸öÖ»¶ÁĿ¼£¬µ±³¢ÊÔÈ¥¸´ÖÆÒ»¸öÒÑ´æÔÚµÄÖ»¶ÁÎļþµ½´ËĿ¼²¢ÇÒ overwriteΪ False ʱ£¬Ôò·¢ÉúÒ»¸ö´íÎó¡£
¢ÞÈç¹û source ²»´æÔÚ»òʹÓõÄͨÅä·û²»ÄܺÍÈκÎÎļþ¼ÐÆ¥Å䣬Ҳ·¢ÉúÒ»¸ö´íÎó¡£
¢ßCopyFolder ·½·¨Í£Ö¹ÔÚËüÓöµ½µÄµÚÒ»¸ö´íÎóÉÏ£¬Ö®Ç°Ëù×öµÄ²Ù×÷ÊDz»»áÏûʧµÄ£¬ËùÒÔÒª
×¢Òâ¡£
15¡¢MoveFolder ·½·¨
Óï·¨£ºobject.MoveFolder source, destination ²ÎÊýÓëCopyFolderµÄÇ°Á½¸öÒ»Ñù¡£
×÷Ó㺽«Ò»¸ö»ò¶à¸öÎļþ¼Ð´ÓÒ»¸öµØ·½Òƶ¯µ½ÁíÒ»¸öµØ·½¡£ ˵Ã÷£º
¢ÙÖ»ÓÐÔÚ²Ù×÷ϵͳ֧³ÖµÄÇé¿öÏ£¬Õâ¸ö·½·¨²ÅÔÊÐíÎļþ¼ÐÔÚ¾íÖ®¼äÒƶ¯¡£WindowsÊDz»ÔÊÐíµÄ£¬½«CÅ̵ÄÎļþ¼ÐÒƵ½DÅÌÊDz»Ðеġ£
¢ÚÈç¹û source °üº¬Í¨Åä·û»ò destination ÒÔ·¾¶·Ö¸ô·û £¨\\£© Ϊ½á⣬ÔòÈÏΪ destination Ö¸¶¨ÁËÒ»¸öÒÑ´æÔÚµÄÎļþ¼Ð£¬ÔÚ´ËÎļþ¼ÐÖÐÒƶ¯ÏàÆ¥ÅäµÄÎļþ¡£·ñÔò£¬ÈÏΪ destination ÊÇÒ»¸öÒª´´½¨µÄÄ¿±êÎļþ¼ÐÃû×Ö¡£ÕâµãÓëCopyFolderÊÇÒ»ÑùµÄ¡£ ¢ÛÈç¹û destination ÊÇÒ»¸öÒÑ´æÔÚµÄÎļþ£¬Ôò·¢ÉúÒ»¸ö´íÎó¡£ ¢ÜÈç¹û destination ÊÇÒ»¸öĿ¼£¬Ôò·¢ÉúÒ»¸ö´íÎó¡£ ÀýÈ磺
fso.movefolder \·¢Éú´íÎó¡£
¢ÝÈç¹û source ²»´æÔÚ»òʹÓõÄͨÅä·û²»ÄܺÍÈκÎÎļþ¼ÐÆ¥Å䣬Ҳ·¢ÉúÒ»¸ö´íÎó¡£
¢ÞMoveFolder ·½·¨Í£Ö¹ÔÚËüÓöµ½µÄµÚÒ»¸ö´íÎóÉÏ¡£²»Òª³¢ÊԻؾíÔÚ´íÎó·¢ÉúÇ°Ëù×öµÄÈκθı䡣
16¡¢DeleteFolder ·½·¨
Óï·¨£ºobject.DeleteFolder folderspec[, force] folderspec ±ØÐèµÄ¡£ÒªÉ¾³ýµÄÎļþ¼ÐµÄÃû×Ö¡£ Folderspec ¿ÉÒÔÔÚ×îºóµÄ·¾¶²¿¼þÖаüº¬Í¨Åä·û¡£
force ¿ÉÑ¡µÄ¡£Boolean Öµ£¬Èç¹ûҪɾ³ý¾ßÓÐÖ»¶ÁÊôÐÔÉèÖõÄÎļþ¼Ð£¬ÆäֵΪ True£¬Èç¹ûֵΪ False £¨È±Ê¡£©£¬Ôò²»ÄÜɾ³ý¾ßÓÐÖ»¶ÁÊôÐÔÉèÖõÄÎļþ¼Ð¡£ ×÷ÓãºÉ¾³ýÒ»¸öÖ¸¶¨µÄÎļþ¼ÐºÍËüµÄÄÚÈÝ¡£
˵Ã÷£ºÈç¹ûûÓз¢ÏÖÏàÆ¥ÅäµÄÎļþ¼Ð£¬Ôò·¢ÉúÒ»¸ö´íÎó¡£DeleteFolder ·½·¨Í£Ö¹ÔÚËüÓöµ½µÄµÚÒ»¸ö´íÎóÉÏ£¬²»Òª³¢ÊԻؾí»ò³·Ïû´íÎó·¢ÉúÇ°Ëù×öµÄÈκθı䡣 ʾÀý£º
fso.DeleteFolder(\17¡¢FolderExists ·½·¨
Óï·¨£ºobject.FolderExists(folderspec)
×÷ÓãºÈç¹ûÖ¸¶¨µÄÎļþ¼Ð´æÔÚ·µ»Ø True£¬²»´æÔÚ·µ»Ø False¡£ 18¡¢DriveExists ·½·¨
Óï·¨£ºobject.DriveExists(drivespec)
×÷ÓãºÈç¹ûÖ¸¶¨µÄÇý¶¯Æ÷´æÔÚ£¬·µ»Ø True£¬Èç¹û²»´æÔÚ·µ»Ø False¡£ 19¡¢FileExists ·½·¨
Óï·¨£ºobject.FileExists(filespec)
×÷ÓãºÈç¹ûÖ¸¶¨µÄÎļþ´æÔÚ£¬·µ»Ø True£¬Èô²»´æÔÚ£¬Ôò·µ»Ø False¡£ 20¡¢CreateTextFile ·½·¨
Óï·¨£ºobject.CreateTextFile(filename[, overwrite[, unicode]])
overwrite ¿ÉÑ¡µÄ¡£Boolean Öµ£¬±íʾһ¸öÒÑ´æÔÚÎļþÊÇ·ñ¿É±»¸²¸Ç¡£Èç¹û¿É±»¸²¸ÇÆäֵΪ True£¬ÆäֵΪ False ʱ²»Äܸ²¸Ç¡£Èç¹ûËü±»Ê¡ÂÔ£¬ÔòÒÑ´æÔÚÎļþ²»Äܸ²¸Ç¡£
unicode ¿ÉÑ¡µÄ¡£Boolean Öµ£¬±íʾÎļþÊÇ×÷Ϊһ¸ö Unicode Îļþ´´½¨µÄ»¹ÊÇ×÷Ϊһ¸öASCII Îļþ´´½¨µÄ¡£Èç¹û×÷Ϊһ¸ö Unicode Îļþ´´½¨£¬ÆäֵΪ True£¬×÷Ϊһ¸ö ASCII ÎÄ