Is it possible to delete all the subfolders (with content) and files within a folder?
For example:
Backup
November
pic1.jpg
pic2.jpg
December
January
pic3.jpg
example1.txt
example2.txt
example3.txt
There is a root folder (Backup). This root folder contains 3 subfolders (with content) and 3 text files. How can I delete the whole content (3 subfolders and 3 files) of the Backup folder without deleting the root folder (Backup) itself?
The Directory class has a Delete method that accepts a parameter that forces the deleting operation recursively on the folder passed
' Loop over the subdirectories and remove them with their contents
For Each d in Directory.GetDirectories("C:\Backup")
Directory.Delete(d, true)
Next
' Finish removing also the files in the root folder
For Each f In Directory.GetFiles("c:\backup")
File.Delete(f)
Next
FROM MSDN Directory.Delete
Deletes the specified directory and, if indicated, any subdirectories
and files in the directory.
Related
I am writing a program which iterates over "every" file on a computer. It excludes certain directories from its search such as the recycle bin and temporary and windows system files. There is a subroutine to search the root of each drive for files and then recursively search each sub-directory for files. There is a SortedSet(Of String) of excluded directories (for quick look-ups), when the search matches an excluded directory it skips it and removes it from the exclusion list to reduce the number of comparisons.
There is a subroutine which is used to add new directories to the exclusion set. The subroutine's function is to only add directories which are not sub-directories of other directories already contained in the set and to remove any directories already contained in the set if they are sub-directories of the directory to be added.
Below is my solution to the problem. The exact locations of the excluded directories cannot be known in advance, is there a more concise and/or efficient algorithm that could be used for this purpose?
Sub ExcludeDirectory(directoryPath As String)
'For simple case-insensitive comparison all paths are converted to lower-case prior to comparison.
directoryPath = directoryPath.ToLower
'Append the final '\' to the candidate if it is missing. This avoids issues with directories not being added to the exclusion list due to sharing an already listed directories path as a prefix but not actually being a subdirectory of that listed directory.
'For Example: C:\Program Files Compare To: C:\Program Files\
' C:\Program Files (x86) C:\Program Files (x86)\
If Not directoryPath.EndsWith("\") Then directoryPath &= "\"
'Do loop chosen since items in the exclusion list will be removed if they are subdirectories of the candidate directory.
Dim i As Integer = 0
Do Until i = ExcludedDirectories.Count
'Multiple listed directories may be subdirectories of candidate but the reverse is not the case so evaluating the first case is done first.
If ExcludedDirectories(i).StartsWith(directoryPath) Then
'If any listed directory is a subdirectory of the candidate then remove it. Do not increment 'i' because the next directory will be located at the same index.
ExcludedDirectories.Remove(ExcludedDirectories(i))
ElseIf directoryPath.StartsWith(ExcludedDirectories(i)) Then
'If the candidate is a subdirectory of any listed directory then it will not be added and does not need comparison to any other directories.
Exit Sub
Else
i += 1
End If
Loop
'Add candidate directory.
ExcludedDirectories.Add(directoryPath)
End Sub
I'm looking for a way/code to select a folder within a specific folder that contains part of a string from a variable
I have folderA and within folderA are numerous folders. All the folders within folderA start out with a 5 digit number followed by an "_" and then a short description. ie, 12345_Folder Test 1, 54321_Folder Test 2, 99999_Folder Test 3, etc...
tell application "Finder"
set folderA to folder "FolderA" of folder "Desktop" of folder "MacUser" of folder "Users" of startup disk
set jobNum to text returned of (display dialog "Enter 5 Digits Only:" default answer "")
open folderA
set selected_item to (folder whose name contains jobNum in folderA) -- This is what's failing for me.
-- from here on I would like to do other things such as run other parts of a script.
end tell
---Script Error Message
Can’t get folder whose name contains jobNum of folder "FolderA" of
folder "Desktop" of folder "prepress_js" of folder "Users" of startup
disk of application "Finder". ----
Any help is greatly appreciated!
Try this line:
set selection to item 1 of (get every folder in folderA whose name contains jobNum) -- This is what's failing for me.
This question is bring from another forum which not having answer yet for my situation.
I have something to do on network shared folder. But when I search on Internet, it giving me a code to do in own computer only. Step that I want to do is:
Check destination (network shared folder) path is empty or not.
Delete folder content (not the main one) eg: "\USER-PC\File\"; the folder "File" no need to deleted, but the content inside is need to deleted.
Copy folder content from source to new destination.
No. 1 and 3 is OK. But No. 2 is not yet found. How to delete a content from directory on Network Shared Folder?
Delete directory code that I use but exception "Could not complete operation since directory is a root directory":
My.Computer.FileSystem.DeleteDirectory(strDestination, FileIO.DeleteDirectoryOption.DeleteAllContents)
Please assist
EDITED:
To delete all files inside the main directory:-
Dim directory As New DirectoryInfo(strDestination)
For Each file As FileInfo In directory.GetFiles()
file.Delete()
Next file
To delete all folders inside the main directory:-
For Each folder As DirectoryInfo In directory.GetDirectories()
folder.Delete(True)
Next folder
Use this instead (it's C#, you'll need to convert it to VB.NET):
DirectoryInfo directory = new DirectoryInfo("\\USER-PC\File");
foreach(FileInfo file in directory.GetFiles()) {
file.Delete();
}
I am new to VB. Can anyone help on this, thanks. BTW, it's bloody pain to mess the formatting here :) How to do the indent?
parameters:
source file
target
backup folder
Pseudocode:
get all sub folders under target
for each folder under target
if exists source file
if exists backup folder
replace/copy source file to backup folder
else
create backup folder
replace/copy source file to backup folder
replace source file
Check this methods:
Directory.GetDirectories()
File.Exists()
Directory.Exists()
Directory.CreateDirectory()
File.Copy()
As I told you before on the other posts, just check the methods on System.IO. Its very well documented on MSDN, specially the classes File, Directory and Path.
For getting subfolders refer to this question
You then will want to put the sub folder locations into an array. From this array, loop through each object and so on.
Try using methods under 'File.' as this will include many functions for file processing.
This will require the 'Imports System.IO'.
here i am reading files from one root folder and after reading that we are moving that particular file Success folder and taking a copy of that in to back up folder normal
while reading file i will check file naming convention by targeting backup folder if suppose it was exists in backup folder then i m moving the file to duplicate folder
my problem if suppose again same file came to process this file already existed in duplicate folder how do send the file in to duplicate folder, unfornately i dint have any property as file rename any way can anybody give some suggestion to resolve from issue please.
If File.Exists(Swift_Backup + "\" + Path.GetFileName(CBFile)) Then
' File.Move(CBFile, Swift_Duplicate + "\\" + Path.GetFileName(CBFile)) 'DUPLICATE FOLDER'
' File.Copy(oldFileName, NewFileName);
File.Copy(CBFile, Swift_Duplicate + "\\" + Path.GetFileNameWithoutExtension(CBFile) + "_" + DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff tt") + Path.GetExtension(CBFile)) 'DUPLICATE FOLDER'
File.Delete(CBFile)
END IF
i have tried like this but was not worked out for am i did any mistake
Swift_Duplicate --> duplicate folder name
Swift_Backup --> backup folder name
CBFile --> string filename will be there in cbfile, it will fetch from the root folder
Renaming a file is the same as moving it to the same directory with a different name. For example:
File.Move("C:\\docs\\new.doc", "C:\\docs\\old.doc");
Also, consider using FileInfo class:
FileInfo file = new FileInfo("C:\\docs\\new.doc");
file.MoveTo("C:\\docs\\new.doc");
FileInfo copy = file.CopyTo("C:\\docs\\old.doc");