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

  • Question

  • 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

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

    Wednesday, July 1, 2015 1:46 PM

Answers

  • Did you actually look at what is in that string.  I can't see your system.

    \_(ツ)_/

    • Marked as answer by prabasb Thursday, July 2, 2015 5:40 PM
    Wednesday, July 1, 2015 6:28 PM

All replies

  • What is the full error?

    \_(ツ)_/

    Wednesday, July 1, 2015 2:15 PM
  • If you get the full script path it will have the drive name.

    If the script is in the root of the drive then that is the start

    root = Split(Wscript.ScriptFullName,"\")(0)

    folderpath = root & "\folders\folder2"


    \_(ツ)_/




    • Edited by jrv Wednesday, July 1, 2015 2:21 PM
    Wednesday, July 1, 2015 2:17 PM
  • Hi JRV

    That worked in getting the drive name, here is my updated script. It is giving error saying "1.vbs(15, 1) Microsoft VBScript runtime error: Path not found" for the line "Set folder = objFSO.GetFolder(Root_Folder)".

    my intention here is , if folder structure does not exist, i want to create it. If it already exists, then i want clean the files and folders under the folder structure. but my code is going into both IF condition and else condition when i ran it first time ( i mean, when there is no folder structure).

    also, 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.  "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 objFSO,Root_Drive,Root_Folder,folder
    Root_Drive = Split(Wscript.ScriptFullName,"\")(0)
    Root_Folder = Root_Drive & "\folder1\folder2"
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    BuildPath Root_Folder

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

    else

    Set folder = objFSO.GetFolder(Root_Folder)
    ' 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

    End If
    End Sub

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

    Wednesday, July 1, 2015 5:07 PM
  • Root_Folder = Root_Drive & "\folder1\folder2"

    msgbox Root_Folder


    \_(ツ)_/

    Wednesday, July 1, 2015 5:37 PM
  • I got the root folder and the drive name using the above two lines you provided. but the error i reported above is different...

    below line is giving error from the above script saying "path not found", but the path was already created through the  script.

    Set folder = objFSO.GetFolder(Root_Folder)

    Wednesday, July 1, 2015 5:52 PM
  • Did you actually look at what is in that string.  I can't see your system.

    \_(ツ)_/

    • Marked as answer by prabasb Thursday, July 2, 2015 5:40 PM
    Wednesday, July 1, 2015 6:28 PM