Announcement

Collapse
No announcement yet.

need help to improve little script

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • need help to improve little script

    Hello,
    I need to find particular file format on networked comps, and have a report for each pc.
    I found a nice script on the blog. It does the job, but lists all the files on C:
    There is a line in additional post that should basically list files only for asked extentions.
    For some reason this line doesn't work.
    Could somebody take a look and correct it. Thx. Michael.

    Here is the script. REM line is original. The line above is additional and should list only .doc *. xls *.ppt
    As I mentioned with original line only, it lists all the files. With the line above it says file not found. Sure I have files with extentions asked on target machines.
    I guess that the line: dir "\%~1\c$" /s /a /b *.doc *.xls *.ppt > "%~1.txt"
    missing a slash dir "\%...
    If I add a slash it lists all files exactly like with the REM line.
    Machines.TXT is placed in the same folder as FIND.BAT (the script)

    What is missing?


    @echo off
    if "%1"=="" goto all
    echo Scanning %~1
    dir "\%~1\c$" /s /a /b *.doc *.xls *.ppt > "%~1.txt"
    rem dir "\\%~1\c$" /s /a /b > "%~1.txt"
    goto end
    :all
    for /f "delims=" %%i in (Machines.TXT) do call %0 "%%i"
    :end
    Last edited by mla; 21st July 2009, 18:36. Reason: correction
    "When you hit a wrong note it's the next note that makes it good or bad". Miles Davis

  • #2
    Re: need help to improve little script

    Yes, the network path should always start with double backslashes. And, also the network path should be added before every wildcard that is before the filetypes.

    Try this sample,
    Code:
    @echo off & color 6A & setlocal
    
    if "%~1"=="" goto all
    
    Set "f=\\%~1\c$\*"
    title ^> Scanning: %f%
    echo\&echo please wait...
    
    dir /s /a /b %f%.doc %f%.xls %f%.ppt > "%~1.txt"
    rem dir "\\%~1\c$" /s /a /b > "%~1.txt"
    goto end
    
    :all
    for /f "delims=" %%i in (Machines.TXT) do call %0 "%%i"
    :end
    endlocal
    \Rems

    This posting is provided "AS IS" with no warranties, and confers no rights.

    __________________

    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts

    Comment


    • #3
      Re: need help to improve little script

      Mr. Rems,
      as usually the efficiency of your work 110%
      I liked the color

      Could you please enable output of search on screen and sort files in output file by type.
      Will be more impressive ...

      Thanks,
      Michael.
      "When you hit a wrong note it's the next note that makes it good or bad". Miles Davis

      Comment


      • #4
        Re: need help to improve little script

        Rems,
        I tryied my enormous scripting skills and added /o:e to the line:
        dir /s /a /b /o:e %f%.doc %f%.xls %f%.ppt > "%~1.txt"

        It sorts files by type in each directory.
        Is there a way to sort all doc, then all ppt and xls

        Anyway, it's absolutely fine with youra TAKE 1.
        And my additional "requests" are kind of "give me more", "give me more"...

        Thanks again.
        Michael.
        "When you hit a wrong note it's the next note that makes it good or bad". Miles Davis

        Comment


        • #5
          Re: need help to improve little script

          Originally posted by mla View Post

          dir /s /a /b /o:e %f%.doc %f%.xls %f%.ppt > "%~1.txt"

          It sorts files by type in each directory.
          Is there a way to sort all doc, then all ppt and xls
          Hi Michael,

          If you like all output to be sorted by filetype per computer and not per folder, the batch solution would be: search each computer three times (a search seperately for every file type). I think that is not very efficient. Alternatively you could search in one run and write the output to a csv file and add an extra column for just the extension, then use Excel to sort.

          However the best option (and the fastest!) would be to use vbs for this.
          With vbs it is possible to
          - search for the three file types on every target computer in one run
          - show the search results realtime in an iexplorer window.
          - sort the results in any order youi prefere, before writing it to a file.

          I can help to put the vbscript together.


          \Rems

          This posting is provided "AS IS" with no warranties, and confers no rights.

          __________________

          ** Remember to give credit where credit's due **
          and leave Reputation Points for meaningful posts

          Comment


          • #6
            Re: need help to improve little script

            Rems,
            the script that you fixed is absolutely enough for the required task.

            Thanks a lot.
            "When you hit a wrong note it's the next note that makes it good or bad". Miles Davis

            Comment


            • #7
              Re: need help to improve little script---TO: REMS

              Rems,
              could you please to create VBS script (as you offered), ffrom the batch one...

              Thanks.
              Michael.
              "When you hit a wrong note it's the next note that makes it good or bad". Miles Davis

              Comment


              • #8
                Re: need help to improve little script---TO: REMS

                Originally posted by mla View Post
                Rems,
                could you please to create VBS script (as you offered), ffrom the batch one...

                Thanks.
                Michael.
                Hi Michael,

                After you PM'd me earlier to say that you might be intrested to see the vbs solution as wel if it wouldn't be to much time-consuming for me, I started to create the script.

                It was not so difficult since all script snippets I could use for this script were already available for me.


                I have added two extra options to the sample below;
                * add-1 = Perform a 1x-ping test before trying to connect to a remote computer to check if the host is connectable.
                * add-2 = Activated the time-out option for the remote WMI-service (fixed 2 minutes) to avoid hanging connections.



                sample:
                Code:
                Const ForReading = 1
                Const ForWriting = 2
                Const dictKey    = 1
                Const dictItem   = 2
                Const WBEM_FLAG_CONNECT_USE_MAX_WAIT = &H80  '=> ConnectServer timeout 2 min
                
                Dim objShell  ' globaly
                
                Set objDictionary1 = CreateObject("Scripting.Dictionary")
                Set objFSO = CreateObject("Scripting.FileSystemObject")
                Set oIELog = SetupMSIE("Scanning remote computers", _
                   200, 150, 1000, 400, oLogEnd)
                
                If Wscript.Arguments.count = 1 Then
                   ConnectTo Wscript.Arguments(0)
                Else
                   inputFile = "c:\computers.txt"
                   If objFSO.FileExists(inputFile) = True Then
                     With objFSO.GetFile(inputFile)
                       Set ts = .OpenAsTextStream(ForReading)
                     End With
                     Do While ts.AtEndOfStream <> True
                       newItem = trim(ts.ReadLine)
                       If len(newItem) >0 Then ConnectTo newItem
                     Loop
                     ts.Close: Set fileObj = Nothing
                   End If
                End If
                
                Set oIELog = Nothing
                msgBox "",,"Done!": wscript.quit
                
                
                Sub ConnectTo (strComputer) ' ---------------
                   IELogMsg String(25,"=") & "/ " & now & " \" & String(25,"=")
                   If (IsConnectible(strComputer) = TRUE) Then
                     Set objSWbemLocator = CreateObject _
                       ("WbemScripting.SWbemLocator") 
                     Set objWMIService = objSWbemLocator.ConnectServer _
                       (strComputer, "root\CIMV2","","",,,WBEM_FLAG_CONNECT_USE_MAX_WAIT)
                     objWMIService.Security_.ImpersonationLevel = 3
                
                     Set colFiles = objWMIService. _
                       ExecQuery("Select * from CIM_DataFile Where " _
                       & "(extension='doc' OR extension='ppt' OR extension='xls')")
                
                     For Each objFile in colFiles
                       IELogMsg "(" & strComputer & ") " & objfile.name
                       objDictionary1.Add objfile.name, objfile.extension _
                         ' & "." & objfile.FileName
                     Next
                
                     SortDictionary objDictionary1, dictItem, dictKey
                     Set objTextFile = objFSO.OpenTextFile(strComputer _
                       & ".txt", ForWriting, True)  '=> outputFile, if connectable
                
                     Z = objDictionary1.Count: If Z > 0 Then
                       For Each objKey In objDictionary1
                         objTextFile.WriteLine CStr(objKey)
                       Next
                     End If
                   Else
                     strMSG = "(" & strComputer & ") *** UNABLE TO CONNECT ***"
                     IELogMsg strMSG
                     Set objTextFile = objFSO.OpenTextFile(strComputer _
                       & " (!!!).txt", ForWriting, True)  '=> 'else'-outputFile
                     objTextFile.WriteLine strMSG & vbTab & now()
                   End If
                   objTextFile.close: Set objTextFile = Nothing
                   objDictionary1.RemoveAll
                End Sub
                
                Function SortDictionary(objDict, intSortA, intSortB)
                   ' http://support.microsoft.com/kb/246067
                   ' declare our variables
                   Dim strDict()
                   Dim objKey
                   Dim strKey,strItem
                   Dim X,Y,Z
                
                   ' get the dictionary count
                   Z = objDict.Count
                
                   ' we need more than one item to warrant sorting
                   If Z > 1 Then
                     ' create an array to store dictionary information
                     ReDim strDict(Z,2)
                     X = 0
                     ' populate the string array
                     For Each objKey In objDict
                       strDict(X,dictKey)  = CStr(objKey)
                       strDict(X,dictItem) = CStr(objDict(objKey))
                       X = X + 1
                     Next
                
                     ' perform a a shell sort of the string array
                     For X = 0 to (Z - 2)
                       For Y = X to (Z - 1)
                         If StrComp((strDict(X,intSortA) & strDict(X,intSortB)), _
                           (strDict(Y,intSortA) & strDict(Y,intSortB)), _
                           vbTextCompare) > 0 Then
                             strKey  = strDict(X,dictKey)
                             strItem = strDict(X,dictItem)
                             strDict(X,dictKey)  = strDict(Y,dictKey)
                             strDict(X,dictItem) = strDict(Y,dictItem)
                             strDict(Y,dictKey)  = strKey
                             strDict(Y,dictItem) = strItem
                         End If
                       Next
                     Next
                
                     ' erase the contents of the dictionary object
                     objDict.RemoveAll
                
                     ' repopulate the dictionary with the sorted information
                     For X = 0 to (Z - 1)
                       objDict.Add strDict(X,dictKey), strDict(X,dictItem)
                     Next
                   End If
                End Function
                
                Function SetupMSIE(sTitle, iLeft, iTop, iWidth, iHeight, oLogEnd) 
                   Set SetupMSIE = CreateObject("InternetExplorer.Application") 
                   With SetupMSIE 
                     .AddressBar = False : .ToolBar = False 
                     .StatusBar = True : .Resizable = True 
                     .Left = iLeft : .Top = iTop 
                     .Width = iWidth : .Height = iHeight 
                     .Navigate("about:blank") 
                     Do Until .readyState = 4: wscript.sleep 100: Loop 
                     .Visible = True
                     With .document 
                       .Body.InnerHtml = "<span id='theEnd'>" 
                       Set oLogEnd = .all.theEnd
                     End With ' document 
                     .Document.Title = sTitle & String(555, chr(160)) 
                   End With 'SetupMSIE obj
                End Function
                
                Sub IELogMsg(sMsg)
                   oLogEnd.insertAdjacentHtml "beforeBegin", sMsg & "<BR>"
                   oLogEnd.scrollIntoView
                End Sub 
                
                Function IsConnectible(strHost)
                   Dim lngResult
                   ' objShell has been globally declared
                   If Not IsObject(objShell) _
                     Then Set objShell = CreateObject("Wscript.Shell")
                   lngResult = objShell.Run("%comspec% /c ping -n 1 " _
                     & " -w 750 " & strHost _
                     & " | find /i ""TTL="" > nul 2>&1", 0, True)
                   Select Case lngResult
                     Case 0
                       IsConnectible = True
                     Case Else
                       IsConnectible = False
                   End Select
                End Function
                \Rems

                This posting is provided "AS IS" with no warranties, and confers no rights.

                __________________

                ** Remember to give credit where credit's due **
                and leave Reputation Points for meaningful posts

                Comment


                • #9
                  Re: need help to improve little script

                  Rems,
                  I am getting an error...
                  Cannot attach print screen (currently attachment button not working in IE and FFox)

                  Script:...
                  Line: 159
                  Char: 13
                  Code: 800A0401

                  Thanks for your time.
                  Michael.
                  "When you hit a wrong note it's the next note that makes it good or bad". Miles Davis

                  Comment


                  • #10
                    Re: need help to improve little script

                    Originally posted by mla View Post
                    Rems,
                    I am getting an error...

                    Line: 159
                    Char: 13
                    Code: 800A0401
                    "Expected end of statement"
                    This is a compilation error, therefore check your punctuation.

                    This posting is provided "AS IS" with no warranties, and confers no rights.

                    __________________

                    ** Remember to give credit where credit's due **
                    and leave Reputation Points for meaningful posts

                    Comment

                    Working...
                    X