Find and Delete Empty Files

This process prompts the user to select a folder and then deletes all the empty files within that folder.

Display.SelectFolder Description:'Please select a folder to delete the empty files from. Press Cancel to exit.' \
                    InitialDirectory:'' IsTopMost:False \
                    SelectedFolder=> SelectedFolder \
                    ButtonPressed=> ButtonPressed

if ButtonPressed='OK' then
	Folder.GetFiles Folder:SelectedFolder \
                    FileFilter:'*' \
                    IncludeSubfolders:False \
                    FailOnAccessDenied:True \
                    SortBy1:Folder.SortBy.NoSort \
                    SortDescending1:False \
                    SortBy2:Folder.SortBy.NoSort \
                    SortDescending2:False \
                    SortBy3:Folder.SortBy.NoSort \
                    SortDescending3:False Files=> AllFilesInFolder

	loop foreach item in AllFilesInFolder
		if item.size=0 then
			File.Delete Files: item
		end
	end
end

Display.ShowMessage Title:'Process completed!' \
                    Message:'Process "Find and Delete Empty Files" completed.' \
                    Icon:Display.Icon.None \
                    Buttons:Display.Buttons.OK \
                    DefaultButton:Display.DefaultButton.Button1 \
                    IsTopMost:False \
                    ButtonPressed=> ButtonPressed

In more detail:


The process prompts the user to select a folder and checks if the “OK” button is pressed. If so, it retrieves all the files of the folder in a list. Finally, it examines the size of each file and, if it is equal to zero, deletes it.

Step 1: Prompt the User to Select a Folder

Prompt the user to select a folder to delete all the empty files within it.

Display.SelectFolder Description:'Please select a folder to delete the empty files from. Press Cancel to exit.' \
                    InitialDirectory:'' IsTopMost:False \
                    SelectedFolder=> SelectedFolder \
                    ButtonPressed=> ButtonPressed

Use the “Display.SelectFolder”action and set the Dialog description.

Step 2: Delete the Empty Files

Check whether the user has selected a folder (i.e. if the user pressed the “OK” button). If so, check the size of each file and, if it is equal to 0, delete it.

if ButtonPressed='OK' then
	Folder.GetFiles Folder:SelectedFolder \
                    FileFilter:'*' \
                    IncludeSubfolders:False \
                    FailOnAccessDenied:True \
                    SortBy1:Folder.SortBy.NoSort \
                    SortDescending1:False \
                    SortBy2:Folder.SortBy.NoSort \
                    SortDescending2:False \
                    SortBy3:Folder.SortBy.NoSort \
                    SortDescending3:False Files=> AllFilesInFolder

	loop foreach item in AllFilesInFolder
		if item.size=0 then
			File.Delete Files: item
		end
	end
end

Display.ShowMessage Title:'Process completed!' \
                    Message:'Process "Find and Delete Empty Files" completed.' \
                    Icon:Display.Icon.None \
                    Buttons:Display.Buttons.OK \
                    DefaultButton:Display.DefaultButton.Button1 \
                    IsTopMost:False \
                    ButtonPressed=> ButtonPressed
  1. Use the “if” statement to check whether the user has pressed the “OK” button.
  2. Use the “Folder.GetFiles” action to retrieve the list of files in the selected folder.
    1. Set the “Folder” argument to “SelectedFolder”, which is an output variable of the previously used “Display.SelectFolder” action.
    2. Store the retrieved files in the “AllFilesInFolder” output variable.
  3. Use the “for each” loop to iterate through the items of the “AllFilesInFolder” list. In each iteration, the current file of the list will be stored into the “item” loop variable and its size will be examined.
  4. Use a nested “if” statement to check whether the current file is empty, i.e. if the “.size” property of the “item” variable is equal to 0.
  5. Close the nested conditional block, using the “end” statement.
  6. Close the loop block, using another “end” statement.
  7. Close the outer condition block, using a third “end” statement.
  8. Finally, inform the user when the process is complete.