vbscript 写入文件_使用VBScript静默删除空文件夹

vbscript 写入文件

I decided to write this article after I saw an interesting question here on Experts Exchange by one of our new members, asking how to delete empty folders and subfolders in a directory without any prompts.

在我的一位新成员在Experts Exchange上看到一个有趣的问题后,我决定写这篇文章,询问如何在没有任何提示的情况下删除目录中的空文件夹和子文件夹。

The script is well commented but I am going to outline the major portion of the code here.

该脚本的注释很好,但是我将在此处概述代码的主要部分。

  1. Set the “Start Folder” - this will be the folder where you want to clean up all empty folders.  My example uses "C:\temp\ee\clean"

    设置“开始文件夹”-这将是您要清理所有空文件夹的文件夹。 我的示例使用“ C:\ temp \ ee \ clean”

  2. VBScript does not have very good array support so we have to use some tricks. I will be creating a collection of found folders and saving to a text field that will get delimited with a special delimiter that will not get confused with other possible characters in a folder name.  The field "txtFolderList" will hold the text list.

    VBScript没有很好的数组支持,因此我们必须使用一些技巧。 我将创建一个找到的文件夹的集合并将其保存到一个文本字段,该文本字段将使用特殊的定界符来定界,该定界符不会与文件夹名称中的其他可能的字符混淆。 字段“ txtFolderList”将保存文本列表。

  3. Here will run a subroutine that will recursively find all subfolders in our start folder. As folders are found, the folder names will be saved in "txtFolderList"

    这里将运行一个子例程,该子例程将递归地在我们的开始文件夹中找到所有子文件夹。 找到文件夹后,文件夹名称将保存在“ txtFolderList”中

  4. Here we are creating an array from our text field.  We will use the array to loop through all the found folders.

    在这里,我们从文本字段创建一个数组。 我们将使用该数组遍历所有找到的文件夹。

  5. Finally, we call a subroutine for each folder in the array that will check if the folder is empty. If the folder is empty, it will get deleted. Note that we are starting with the last array object and going towards the first. The reason is the first folders will be filled with the last folders and will not be found as empty. By working backward, we check if the last item is empty and if it is, delete it. This allows the parent folders to be deleted if no other files are found.

    最后,我们为数组中的每个文件夹调用一个子例程,该例程将检查文件夹是否为空。 如果文件夹为空,它将被删除。 请注意,我们从最后一个数组对象开始,向第一个数组对象前进。 原因是第一个文件夹将被最后一个文件夹填充,并且不会被发现为空。 通过向后工作,我们检查最后一项是否为空,如果为空,则将其删除。 如果找不到其他文件,这将允许删除父文件夹。

The subroutines are well commented. The script will run wherever you can run VBScript (Windows). To run the script, save the file with the .vbs extension. From there you can use the command prompt or anything that can call a command line.  

子例程的注释很好。 该脚本将在可以运行VBScript(Windows)的任何地方运行。 要运行脚本,请保存扩展名为.vbs的文件。 在这里,您可以使用命令提示符或任何可以调用命令行的命令。

Assuming the file is called "clean.vbs" and is found in c:\scripts\clean.vbs, you only need to enter c:\scripts\clean.vbs to the command prompt or call the line in your own code.

假设该文件名为“ clean.vbs”,并且位于c:\ scripts \ clean.vbs中,则只需在命令提示符下输入c:\ scripts \ clean.vbs或在您自己的代码中调用该行。

' *************************************
'  Author: Scott Fell 
'  https://www.experts-exchange.com/members/padas.html
'  Date: January 2020
'  Original Posted: Experts-Exchange.com
'  ************************************
'  WARNING! THIS ROUTINE WILL DELETE ALL SUBFOLDERS IN THE START FOLDER IF THE FOLDER IS EMPTY
'  USE THIS CODE AT YOUR OWN RISK
'  PLEASE MAKE SURE TO TEST BEFORE USING LIVE
' *************************************

' 1) THE FOLDER TO START
startFolder = "C:\temp\ee\empty1"

' 2) VARIABLE TO HOLD ALL FOUND FOLDERS
txtFolderList = ""

' CREATE FILE SYSTEM OBJECT
Set fso = CreateObject("Scripting.FileSystemObject")

' 3) CALL SUBROUTINE THAT WILL RECURSIVELY GET ALL SUB FOLDERS
getSubfolders fso.GetFolder(startFolder)

' WHEN txtFolderList IS POPULATED
' IT WILL LOOK LIKE c:\path\folder[|]c:\path\folder[|]c:\path\folder
' 4) CREATE AN ARRAY USING splut and [|] TO SEPARATE

arrSubFolders = split(txtFolderList,"[|]")


' 5) LOOP THROUGH ARRAY BACKWARDS
' ALLOWS TO DELETE THE LAST FOLDER FIRST
intArraySize = LBound(arrSubFolders) 'array size
For x = UBound(arrSubFolders) to 0 step -1 'LOOP THROUGH FOLDERS BACKWARDS
	'RUN THE CLEAN ROUTINE FOR THE CURRENT SUBFOLDER
	clean arrSubFolders(x)
Next

'Wscript.Echo txtFolderList


' SUBROUTINE TO RECURSIVELY GET ALL SUB FOLDERS
Sub getSubfolders(Folder)

	'LOOP THROUGH ALL SUB FOLDERS
	For Each Subfolder in Folder.SubFolders
	
		if txtFolderList = "" then 'IF THIS IS THE FIRST ENTRY
			
			txtFolderList = Subfolder.Path 'ADD THE FIRST SUB FOLDER TO OUR txtFolderList
			
			else
			
			' CONTINUE TO ADD TO THE FIELD txtFolderList USING A DELIMINTER OF [|]
			txtFolderList = txtFolderList &"[|]"& Subfolder.Path
			
		end if
	
		'CALL getSubfolders INSIDE THE LOOP TO GET MORE SUB FOLDERS
		getSubfolders Subfolder
		
	Next
End Sub

' SUB ROUTIE TO CHECK IF FOLDER IS EMPTY
' DELETE FOLDER IF IT IS EMPTY
sub clean(strFolder)

	If fso.FolderExists(strFolder) Then 'MAKE SURE FOLDER EXISTS
	
		Set objFolder = fso.GetFolder(strFolder) 'CREATE FOLDER OBJECT
	  
		' CHECK THAT FOLDER IS EMPTY (REASON WE ARE GOING BACKWARDS)
		If objFolder.Files.Count = 0 And objFolder.SubFolders.Count = 0 Then 
		
			'DELETE FOLDER WHEN TRUE (EMPTY)
			fso.DeleteFolder strFolder
			
			
		end if
		
	End If

end sub


set fso = nothing 

翻译自: https://www.experts-exchange.com/articles/33944/Use-VBScript-to-Silently-Remove-Empty-Folders.html

vbscript 写入文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值