Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

VB script to detect drive name from where it is running and deleting folders and files

$
0
0

Hi ,

i have written this VB script, which will createa directory structure if not exist. Else, if the directory structure exists, it will clearn all the files and folders from it's path keeping only the root directory and subdirectory. now it is giving error at the line "Set folder = fso.GetFolder("c:\folder1\folder2")", what could be the issue. few more doubts on it.

1) i want to create directory structure like (\folder1\folder2) under the drive I am running this script, I dont want to hardcode the drive name in the script. I mean, From what ever the drive i am running this script, it should create folder structure under that drive.

2) how to add logging to this script, i mean what ever i am doing in this script, it should be written into a text file. How to enable logging inside the script. also, "wscript.echo" is prompting message box if i run it without cscript, how to overcome this? I just want to double click the script to run.

------------------------------------------------------

Option Explicit
dim fso,folder,f
Const FULL_PATH = "c:\folder1\folder2"
Const DeleteReadOnly = TRUE
Set fso = CreateObject("Scripting.FileSystemObject")
BuildPath FULL_PATH

Sub BuildPath(ByVal Path)
 If Not fso.FolderExists(Path) Then
 BuildPath fso.GetParentFolderName(Path)
 fso.CreateFolder Path

else
Set folder = fso.GetFolder("c:\folder1\folder2")

' delete all files in root folder
for each f in folder.Files
   On Error Resume Next
   name = f.name
   f.Delete True
   If Err Then
     WScript.Echo "Error deleting:" & Name & " - " & Err.Description
   Else
     WScript.Echo "Deleted:" & Name
   End If
   On Error GoTo 0
Next

' delete all subfolders and files
For Each f In folder.SubFolders
   On Error Resume Next
   name = f.name
   f.Delete True
   If Err Then
     WScript.Echo "Error deleting:" & Name & " - " & Err.Description
   Else
     WScript.Echo "Deleted:" & Name
   End If
   On Error GoTo 0
 
Next
End If
End Sub

------------------------------------------------------------------------


Viewing all articles
Browse latest Browse all 15028

Latest Images

Trending Articles



Latest Images