Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Problem with .vbs cutting off words  (Read 3409 times)

0 Members and 1 Guest are viewing this topic.

demosthenes705

    Topic Starter


    Rookie

    Problem with .vbs cutting off words
    « on: April 24, 2008, 03:53:02 PM »
    I have a script that I am working on for my internship. What it does is it searches the hard drive for given file names, and then if it finds the file names, it deletes the folder. Well the only problem is that if the filename is under a folder like C:\windows or C:\program files I don't want it to delete the entire folder, just the folder names and the individual files I list. Currently I am working with this code
    (I hope it is the latest version, a few WScript.Echo things might be missing, but it is mostly complete.
    Code: [Select]
    Const ForReading = 1

    strComputer = "."
    i=0
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set f = fso.OpenTextFile("H:\gameLocations\gameList.txt", ForReading)

    Do Until f.AtEndOfStream = True
    retString = f.ReadLine
    fname = Split(retstring, ".")(0)
    fext = Split(retString, ".")(1)
    Set colFiles = objWMIService.ExecQuery _
        ("Select * From CIM_DataFile Where FileName = '" & fname & "'" & " and Extension = " & "'" & fext & "'" & "")
      'WScript.Echo "Searching for " & retstring
      For Each objFile in colFiles
    If InStr(1, objFile.Caption, "c:\windows") > 0 Then
    WScript.Echo "Testing"
    WScript.Echo retstring & " Found In Windows...Not Deleting"
    'Here is the start of my delete script
    searchFileString = "H:\gameLocations\lists\" & fname & "_folders.txt"
    WScript.Echo searchFileString
    Set d = fso.OpenTextFile(searchFileString, ForReading)
    Do Until d.AtEndOfStream = True
      folderName = f.ReadLine
      folderPath = objFile.Path & folderName
      strFolder = Left(objFile.Drive & folderPath, Len(objFile.Drive & folderPath) - 1)
      WScript.Echo strFolder
      'fso.DeleteFolder strFolder, True
      'Here is the end of my test script.
    Loop
    i=i+1
    Else
    If InStr(1, objFile.Caption, "c:\program files") > 0 Then
    WScript.Echo retstring & " Found In Program Files...Not Deleting"
    WScript.Echo "Maybe I will code something to delete all the individual files?"
    'Here is the start of my delete script
    searchFileString = fname & "_folders.txt"
    Set d = fso.OpenTextFile(searchFileString, ForReading)
    Do Until d.AtEndOfStream = True
      folderName = f.ReadLine
      folderPath = objFile.Path & folderName
      strFolder = Left(objFile.Drive & folderPath, Len(objFile.Drive & folderPath) - 1)
      WScript.Echo strFolder
    'fso.DeleteFolder strFolder, True
    'Here is the end of my test script.
    Loop
    i=i+1
    Else
    WScript.Echo retstring & " Found In " & objFile.Drive & objFile.Path
    WScript.Echo "Removing Files/Folder " & objFile.Drive & objFile.Path
    strFolder = Left(objFile.Drive & objFile.Path, Len(objFile.Drive & objFile.Path) - 1)
    WScript.Echo "Maybe I will code something to delete all the individual files?"
    fso.DeleteFolder strFolder, True
    End If
    End If
    Next
    Loop
    If i > 0 Then
      msgbox "Alert!, Found something in the windows directory or the program files." & Chr(13) & Chr(13) & "You might want to reimage the computer to take care of the problem."
    Else
      msgbox "Success, Computer is clean."
    End If
    f.Close

    The only problem is that it outputs something like this.


    If you notice, I have a folders list in the .txt file but on the cmd window it shows something like C:\windows\suppor meaning it lost the t in support. It is not just that line, it is some of the other lines, but its not every line.

    Sidewinder



      Guru

      Thanked: 139
    • Experience: Familiar
    • OS: Windows 10
    Re: Problem with .vbs cutting off words
    « Reply #1 on: April 24, 2008, 04:10:04 PM »
    Quote
    a few WScript.Echo things might be missing, but it is mostly complete.

    Maybe I'm just tired but I can't for the life of me see where the literal ">folder to delete" is generated.

    On the other hand you may actually have folders with those names. Could you point out where that message originates?

     8)
    The true sign of intelligence is not knowledge but imagination.

    -- Albert Einstein

    demosthenes705

      Topic Starter


      Rookie

      Re: Problem with .vbs cutting off words
      « Reply #2 on: April 24, 2008, 04:21:33 PM »
      realized what it was, I took 1 character off of the string for the ending / that was never there because I copied and pasted from below.

      Would that make this line look something like this
      Code: [Select]
      strFolder = Left(objFile.Drive & folderPath, Len(objFile.Drive & folderPath) - 1)

      Code: [Select]
      strFolder = objFile.Drive & folderPath

      Sidewinder



        Guru

        Thanked: 139
      • Experience: Familiar
      • OS: Windows 10
      Re: Problem with .vbs cutting off words
      « Reply #3 on: April 24, 2008, 04:33:55 PM »
      Yeah, that works. Nice thing about VBS is you can use unnamed objects to slice and dice data without ever changing the underlying source data.

       8)
      The true sign of intelligence is not knowledge but imagination.

      -- Albert Einstein

      Dias de verano

      • Guest
      Re: Problem with .vbs cutting off words
      « Reply #4 on: April 26, 2008, 12:06:54 AM »
      Yeah, that works. Nice thing about VBS is you can use unnamed objects to slice and dice data without ever changing the underlying source data.

       8)

      Not unique to VBS by any means.