home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 Special / chip-cd_2001_spec_05.zip / spec_05 / ras.cab / fileman.vbs < prev    next >
Text File  |  1999-11-04  |  37KB  |  1,085 lines

  1. '********************************************************************
  2. '*
  3. '* File:           FileMan.vbs
  4. '* Created:        March 1999
  5. '* Version:        1.0
  6. '*
  7. '*  Main Function:  Perform various simple operations on a file.
  8. '*
  9. '*   1.  Fileman.vbs /D <targetfile> | /T <targetfile>
  10. '*                  [/S <server>][/U <username>][/W <password>]
  11. '*                  [/O <outputfile>]
  12. '*
  13. '*   2.  Fileman.vbs /R <targetfile> | /C <targetfile>
  14. '*                   /N <newfile>
  15. '*                  [/S <server>][/U <username>][/W <password>]
  16. '*                  [/O <outputfile>]
  17. '*
  18. '*    Copyright (C) 1999 Microsoft Corporation
  19. '*
  20. '********************************************************************
  21.  
  22.  
  23. OPTION EXPLICIT
  24.  
  25.     'Define constants
  26.  
  27.     CONST CONST_ERROR                   = 0
  28.     CONST CONST_WSCRIPT                 = 1
  29.     CONST CONST_CSCRIPT                 = 2
  30.     CONST CONST_SHOW_USAGE              = 3
  31.     CONST CONST_PROCEED                 = 4
  32.     CONST CONST_DELETE                  = 5
  33.     CONST CONST_RENAME                  = 6
  34.     CONST CONST_COPY                    = 7
  35.     CONST CONST_TAKEOWNERSHIP           = 8
  36.  
  37.  
  38.     'Declare variables
  39.     Dim intOpMode,   i
  40.     Dim strServer, strUserName, strPassword, strOutputFile
  41.     Dim strTaskCommand, strFileName, strNewFileName
  42.     Dim blnForce
  43.  
  44.     'Make sure the host is csript, if not then abort
  45.     VerifyHostIsCscript()
  46.  
  47.     'Parse the command line
  48.     intOpMode = intParseCmdLine(strServer      ,  _
  49.                                 strUserName    ,  _
  50.                                 strPassword    ,  _
  51.                                 strOutputFile  ,  _
  52.                                 strTaskCommand ,  _
  53.                                 strFileName    ,  _
  54.                                 strNewFileName ,  _
  55.                                 blnForce          )
  56.  
  57.     Select Case intOpMode
  58.  
  59.         Case CONST_SHOW_USAGE
  60.             Call ShowUsage()
  61.  
  62.         Case CONST_PROCEED
  63.             Call FileMan(strServer      ,  _
  64.                          strUserName    ,  _
  65.                          strPassword    ,  _
  66.                          strOutputFile  ,  _
  67.                          strTaskCommand ,  _ 
  68.                          strFileName    ,  _
  69.                          strNewFileName ,  _
  70.                          blnForce          )
  71.  
  72.         Case CONST_ERROR
  73.             Call Wscript.Echo("Invalid or missing parameters " _
  74.                & "Please check the input and try again," & vbCRLF _
  75.                & "or invoke with '/?' for help with the syntax.")
  76.             Wscript.Quit
  77.  
  78.         Case Else                    'Default -- should never happen
  79.             Call Wscript.Echo("Error occurred in passing parameters.")
  80.  
  81.     End Select
  82.  
  83. '********************************************************************
  84. '* End of Script
  85. '********************************************************************
  86.  
  87. '********************************************************************
  88. '*
  89. '* Sub FileMan
  90. '* Purpose: Perform various simple operations on a file.
  91. '* Input:   
  92. '*          strServer          a machine name
  93. '*          strOutputFile      an output file name
  94. '*          strUserName        the current user's name
  95. '*          strPassword        the current user's password
  96. '*          strTaskCommand     the file operation to perform 
  97. '*          strFileName        the target file
  98. '*          strNewFileName     the new file
  99. '*          blnForce           overwrite read only files.
  100. '*
  101. '********************************************************************
  102. Private Sub FileMan(strServer      ,  _
  103.                     strUserName    ,  _
  104.                     strPassword    ,  _
  105.                     strOutputFile  ,  _
  106.                     strTaskCommand ,  _ 
  107.                     strFileName    ,  _
  108.                     strNewFileName ,  _
  109.                     blnForce          )
  110.  
  111.  
  112.     ON ERROR RESUME NEXT
  113.  
  114.     Dim objFileSystem, objOutputFile, objService
  115.     Dim strQuery
  116.  
  117.  
  118.     'Open a text file for output if the file is requested
  119.     If Not IsEmpty(strOutputFile) Then
  120.         If (NOT blnOpenFile(strOutputFile, objOutputFile)) Then
  121.             Call Wscript.Echo ("Could not open an output file.")
  122.             Exit Sub
  123.         End If
  124.     End If
  125.  
  126.     'Establish a connection with the server.
  127.     If blnConnect("root\cimv2" , _
  128.                    strUserName , _
  129.                    strPassword , _
  130.                    strServer   , _
  131.                    objService  ) Then
  132.         Call Wscript.Echo("")
  133.         Call Wscript.Echo("Please check the server name, " _
  134.                         & "credentials and WBEM Core.")
  135.         Exit Sub
  136.     End If
  137.  
  138.     'Now execute the method.
  139.     Select Case strTaskCommand
  140.  
  141.         Case CONST_DELETE
  142.  
  143.             Call DeleteFile(strServer     ,  _
  144.                             strUserName   ,  _
  145.                             strPassword   ,  _
  146.                             objService    ,  _
  147.                             objOutputFile ,  _
  148.                             strFileName   ,  _
  149.                             blnForce         )
  150.  
  151.         Case CONST_RENAME
  152.  
  153.             Call RenameFile(strServer      ,  _
  154.                             strUserName    ,  _
  155.                             strPassword    ,  _
  156.                             objService     ,  _
  157.                             objOutputFile  ,  _
  158.                             strFileName    ,  _
  159.                             strNewFileName    )
  160.  
  161.         Case CONST_COPY
  162.  
  163.             Call CopyFile(strServer      ,  _
  164.                           strUserName    ,  _
  165.                           strPassword    ,  _
  166.                           objService     ,  _
  167.                           objOutputFile  ,  _
  168.                           strFileName    ,  _
  169.                           strNewFileName    )
  170.  
  171.         Case CONST_TAKEOWNERSHIP
  172.  
  173.             Call TakeOwnOfFile(strServer      ,  _
  174.                                strUserName    ,  _
  175.                                strPassword    ,  _
  176.                                objService     ,  _
  177.                                objOutputFile  ,  _
  178.                                strFileName       )
  179.  
  180.         Case CONST_ERROR
  181.             'Do nothing.
  182.  
  183.         Case Else                    'Default -- should never happen
  184.             Call Wscript.Echo("Error occurred in passing parameters.")
  185.  
  186.     End Select
  187.  
  188.     If NOT IsEmpty(objOutputFile) Then
  189.         objOutputFile.Close
  190.         Wscript.Echo "Results are saved in file " & strOutputFile & "."
  191.     End If
  192.  
  193. End Sub
  194. '********************************************************************
  195. '*
  196. '* Sub DeleteFile()
  197. '*
  198. '* Purpose: Delete a file.
  199. '*
  200. '* Input:   strServer           a machine name
  201. '*          strUserName         the current user's name
  202. '*          strPassword         the current user's password
  203. '*          objService          The Wbem service
  204. '*          objOutputFile       The Output File Object
  205. '*          strFileName         The file to delete.
  206. '*          blnForce            Overwrite Read-Only files
  207. '*
  208. '* Output:  Results are either printed on screen or saved in strOutputFile.
  209. '*
  210. '********************************************************************
  211. Private Sub DeleteFile(ByVal strServer     ,  _
  212.                        ByVal strUserName   ,  _
  213.                        ByVal strPassword   ,  _
  214.                        ByVal objService    ,  _
  215.                        ByVal objOutputFile ,  _
  216.                        ByVal strFileName   ,  _
  217.                        ByVal blnForce         )
  218.  
  219.     ON ERROR RESUME NEXT
  220.  
  221.     Dim strWBEMClass
  222.     Dim objFileSet, objInst
  223.     Dim intFileCount
  224.  
  225.     strWBEMClass = "CIM_DataFile"
  226.  
  227.     strFileName = strDoubleBackSlashes(strFileName)
  228.  
  229.     Set objFileSet = objService.ExecQuery("SELECT * FROM " & strWBEMClass & _
  230.                      " WHERE Name = """ & strFileName & """",,0)
  231.     if objFileSet.Count = 0 then
  232.          Call WriteLine("File not found.", objOutputFile)
  233.          Exit Sub
  234.     End If
  235.  
  236.     If blnErrorOccurred("Could not obtain " & strWBEMClass & " instance.") Then
  237.         Exit Sub
  238.     End If
  239.  
  240.     intFileCount = 0
  241.     For Each objInst In objFileSet
  242.         If blnCheckFile(objInst.Name,objService,blnForce) Then
  243.             Call objInst.Delete()
  244.             If NOT blnErrorOccurred("Could not delete " & objInst.Name) Then
  245.                 intFileCount = intFileCount + 1
  246.             End If
  247.         Else
  248.             Call WriteLine("File is either marked as a system file, " _
  249.                         & "or is read only.", objOutputFile)
  250.             Call WriteLine("Use the /F switch to override.", objOutputFile)
  251.         End If
  252.     Next
  253.  
  254.     If blnErrorOccurred("Could not delete " & strFileName & ".") Then
  255.         Exit Sub
  256.     Else
  257.         Call WriteLine("File deleted.", objOutputFile)
  258.     End If
  259.  
  260. End Sub
  261.  
  262.  
  263. '********************************************************************
  264. '*
  265. '* Sub RenameFile()
  266. '*
  267. '* Purpose: Rename a file.
  268. '*
  269. '* Input:   strServer           A machine name
  270. '*          strUserName         The current user's name
  271. '*          strPassword         The current user's password
  272. '*          objService          The Wbem service
  273. '*          objOutputFile       The Output File Object
  274. '*          strFileName         The file to delete
  275. '*          strNewFileName      The new file name
  276. '*
  277. '* Output:  Results are either printed on screen or saved in strOutputFile.
  278. '*
  279. '********************************************************************
  280. Private Sub RenameFile(ByVal strServer      ,  _
  281.                        ByVal strUserName    ,  _
  282.                        ByVal strPassword    ,  _
  283.                        ByVal objService     ,  _
  284.                        ByVal objOutputFile  ,  _
  285.                        ByVal strFileName    ,  _
  286.                        ByVal strNewFileName    )
  287.  
  288.  
  289.     ON ERROR RESUME NEXT
  290.  
  291.     Dim strWBEMClass
  292.     Dim objInst, objFileSet
  293.  
  294.     strWBEMClass = "CIM_DataFile"
  295.  
  296.     strFileName = strDoubleBackSlashes(strFileName)
  297.  
  298.     Set objFileSet = objService.ExecQuery("SELECT * FROM " & strWBEMClass _
  299.                      & " WHERE Name = """ & strFileName & """",,0)
  300.  
  301.     if objFileSet.Count = 0 then
  302.         Call Writeline ("File not found.", objOutputFile)
  303.         Exit Sub
  304.     End If
  305.  
  306.     If blnErrorOccurred("Could not obtain " & strWBEMClass & " instance.") Then
  307.         Exit Sub
  308.     End If
  309.  
  310.     For Each objInst in objFileSet
  311.         Call objInst.Rename(strNewFileName)
  312.             If blnErrorOccurred("Could not copy " & strFileName & ".") Then
  313.                 Exit Sub
  314.             Else
  315.                 Call WriteLine("File renamed.", objOutputFile)
  316.            End If
  317.     Next
  318.  
  319. End Sub
  320.  
  321.  
  322. '********************************************************************
  323. '*
  324. '* Sub CopyFile()
  325. '*
  326. '* Purpose: Copy a file.
  327. '*
  328. '* Input:   strServer           a machine name
  329. '*          strUserName         the current user's name
  330. '*          strPassword         the current user's password
  331. '*          objService          The Wbem service
  332. '*          objOutputFile       The Output File Object
  333. '*          strFileName         The file to copy.
  334. '*          strNewFileName      The new file name.
  335. '*
  336. '* Output:  Results are either printed on screen or saved in strOutputFile.
  337. '*
  338. '********************************************************************
  339. Private Sub CopyFile(ByVal strServer      ,  _
  340.                      ByVal strUserName    ,  _
  341.                      ByVal strPassword    ,  _
  342.                      ByVal objService     ,  _
  343.                      ByVal objOutputFile  ,  _
  344.                      ByVal strFileName    ,  _
  345.                      ByVal strNewFileName    )
  346.  
  347.     ON ERROR RESUME NEXT
  348.  
  349.     Dim strWBEMClass
  350.     Dim objInst, objFileSet
  351.  
  352.     strWBEMClass = "CIM_DataFile"
  353.  
  354.     strFileName = strDoubleBackSlashes(strFileName)
  355.  
  356.     Set objFileSet = objService.ExecQuery("SELECT * FROM " & _
  357.                 strWBEMClass & " WHERE Name = """ & strFileName & """",,0)
  358.  
  359.     if objFileSet.Count = 0 then
  360.         Call Writeline ("File not found.", objOutputFile)
  361.         Exit Sub
  362.     End If
  363.  
  364.     If blnErrorOccurred("Could not obtain " & strWBEMClass & " instance.") Then
  365.         Exit Sub
  366.     End If
  367.  
  368.     For Each objInst in objFileSet
  369.         Call objInst.Copy(strNewFileName)
  370.             If blnErrorOccurred("Could not copy " & strFileName & ".") Then
  371.                 Exit Sub
  372.             Else
  373.                 Call WriteLine("File copied.", objOutputFile)
  374.            End If
  375.     Next
  376.  
  377. End Sub
  378.  
  379. '********************************************************************
  380. '* Sub TakeOwnOfFile()
  381. '*
  382. '* Purpose: Take ownership of a file.
  383. '*
  384. '* Input:   strServer           a machine name
  385. '*          strUserName         the current user's name
  386. '*          strPassword         the current user's password
  387. '*          objService          The Wbem service
  388. '*          objOutputFile       The Output File Object
  389. '*          strFileName         the file to sieze
  390. '*
  391. '* Output:  Results are either printed on screen or saved in strOutputFile.
  392. '*
  393. '********************************************************************
  394. Private Sub TakeOwnOfFile(ByVal strServer      ,  _
  395.                           ByVal strUserName    ,  _
  396.                           ByVal strPassword    ,  _
  397.                           ByVal objService     ,  _
  398.                           ByVal objOutputFile  ,  _
  399.                           ByVal strFileName       )
  400.  
  401.     ON ERROR RESUME NEXT
  402.  
  403.     Dim objFileSystem, objInst
  404.     Dim strWBEMClass
  405.  
  406.     strWBEMClass = "CIM_DataFile"
  407.  
  408.     strFileName = strDoubleBackSlashes(strFileName)
  409.  
  410.     Set objInst = objService.Get(strWBEMClass & "=" & """" & strFileName & """")
  411.     If blnErrorOccurred("Could not obtain " & strWBEMClass & " instance.") Then
  412.         Exit Sub
  413.     End If
  414.  
  415.     Call objInst.TakeOwnerShip()
  416.     If blnErrorOccurred("Could not take ownership of " & strFileName & ".") Then
  417.         Exit Sub
  418.     Else
  419.         Call WriteLine("Ownership taken.", objOutputFile)
  420.     End If
  421.  
  422.  
  423. End Sub
  424.  
  425. '********************************************************************
  426. '*
  427. '* Function intParseCmdLine()
  428. '*
  429. '* Purpose: Parses the command line.
  430. '* Input:   
  431. '*
  432. '* Output:  strServer          a remote server ("" = local server")
  433. '*          strUserName        the current user's name
  434. '*          strPassword        the current user's password
  435. '*          strOutputFile      an output file name
  436. '*          strTaskCommand     one of /list, /start, /stop /install /remove
  437. '*                                    /dependents
  438. '*          strDriverName      name of the DEVICE
  439. '*          strStartMode       start mode of the DEVICE
  440. '*          strDisplayName     Display name for the DEVICE.
  441. '*          blnDetails         Extra information to be displayed on the output
  442.  
  443. '*
  444. '********************************************************************
  445. Private Function intParseCmdLine( ByRef strServer      ,  _
  446.                                   ByRef strUserName    ,  _
  447.                                   ByRef strPassword    ,  _
  448.                                   ByRef strOutputFile  ,  _
  449.                                   ByRef strTaskCommand ,  _
  450.                                   ByRef strFileName    ,  _
  451.                                   ByRef strNewFileName ,  _
  452.                                   ByRef blnForce          )
  453.  
  454.  
  455.     ON ERROR RESUME NEXT
  456.  
  457.     Dim strFlag
  458.     Dim intState, intArgIter
  459.     Dim objFileSystem
  460.  
  461.     If Wscript.Arguments.Count > 0 Then
  462.         strFlag = Wscript.arguments.Item(0)
  463.     End If
  464.  
  465.     If IsEmpty(strFlag) Then                'No arguments have been received
  466.         intParseCmdLine = CONST_ERROR
  467.         Wscript.Echo("Arguments are required")
  468.         Exit Function
  469.     End If
  470.  
  471.     'Check if the user is asking for help or is just confused
  472.     If (strFlag="help") OR (strFlag="/h") OR (strFlag="\h") OR (strFlag="-h") _
  473.         OR (strFlag = "\?") OR (strFlag = "/?") OR (strFlag = "?") _ 
  474.         OR (strFlag="h") Then
  475.         intParseCmdLine = CONST_SHOW_USAGE
  476.         Exit Function
  477.     End If
  478.  
  479.     'Retrieve the command line and set appropriate variables
  480.      intArgIter = 0
  481.     Do While intArgIter <= Wscript.arguments.Count - 1
  482.         Select Case LCase(Wscript.arguments.Item(intArgIter))
  483.   
  484.             Case "/s"
  485.                 If Not blnGetArg("Server", strServer, intArgIter) Then
  486.                     intParseCmdLine = CONST_ERROR
  487.                     Exit Function
  488.                 End If
  489.                 intArgIter = intArgIter + 1
  490.  
  491.             Case "/o"
  492.                 If Not blnGetArg("Output File", strOutputFile, intArgIter) Then
  493.                     intParseCmdLine = CONST_ERROR
  494.                     Exit Function
  495.                 End If
  496.                 intArgIter = intArgIter + 1
  497.  
  498.             Case "/u"
  499.                 If Not blnGetArg("User Name", strUserName, intArgIter) Then
  500.                     intParseCmdLine = CONST_ERROR
  501.                     Exit Function
  502.                 End If
  503.                 intArgIter = intArgIter + 1
  504.  
  505.             Case "/w"
  506.                 If Not blnGetArg("User Password", strPassword, intArgIter) Then
  507.                     intParseCmdLine = CONST_ERROR
  508.                     Exit Function
  509.                 End If
  510.                 intArgIter = intArgIter + 1
  511.  
  512.             Case "/d"
  513.                 If Not blnGetArg ("Target File", strFileName, intArgIter) Then
  514.                     intParseCmdLine = CONST_ERROR
  515.                     Exit Function
  516.                 End If
  517.                 strTaskCommand = CONST_DELETE
  518.                 intParseCmdLine = CONST_PROCEED
  519.                 intArgIter = intArgIter + 1
  520.                
  521.             Case "/t"
  522.                 If Not blnGetArg ("Target File", strFileName, intArgIter) Then
  523.                     intParseCmdLine = CONST_ERROR
  524.                     Exit Function
  525.                 End If
  526.                 strTaskCommand = CONST_TAKEOWNERSHIP
  527.                 intParseCmdLine = CONST_PROCEED
  528.                 intArgIter = intArgIter + 1
  529.  
  530.             Case "/r"
  531.                 If Not blnGetArg ("Target File", strFileName, intArgIter) Then
  532.                     intParseCmdLine = CONST_ERROR
  533.                     Exit Function
  534.                 End If
  535.                 strTaskCommand = CONST_RENAME
  536.                 intParseCmdLine = CONST_PROCEED
  537.                 intArgIter = intArgIter + 1
  538.               
  539.             Case "/c"
  540.                 If Not blnGetArg ("Target File", strFileName, intArgIter) Then
  541.                     intParseCmdLine = CONST_ERROR
  542.                     Exit Function
  543.                 End If
  544.                 strTaskCommand = CONST_COPY
  545.                 intParseCmdLine = CONST_PROCEED
  546.                 intArgIter = intArgIter + 1
  547.  
  548.             Case "/n"
  549.                 If Not blnGetArg ("New File", strNewFileName, intArgIter) Then
  550.                     intParseCmdLine = CONST_ERROR
  551.                     Exit Function
  552.                 End If
  553.                 intArgIter = intArgIter + 1 
  554.  
  555.             Case Else 'We shouldn't get here
  556.                 Call Wscript.Echo("Invalid or misplaced parameter: " _
  557.                    & Wscript.arguments.Item(intArgIter) & vbCRLF _
  558.                    & "Please check the input and try again," & vbCRLF _
  559.                    & "or invoke with '/?' for help with the syntax.")
  560.                 Wscript.Quit
  561.  
  562.         End Select
  563.  
  564.     Loop '** intArgIter <= Wscript.arguments.Count - 1
  565.  
  566.     If IsEmpty(intParseCmdLine) Then 
  567.         intParseCmdLine = CONST_ERROR
  568.         Wscript.Echo ("Required arguments [/D | /T | /C | /R] are missing.")
  569.     End If
  570.     If strTaskCommand = CONST_COPY then
  571.         if isEmpty(strNewFileName) then
  572.             intParseCmdLine = CONST_ERROR
  573.             Wscript.Echo ("The /N parameter is required when copying.")
  574.         End If
  575.     End If
  576.     If strTaskCommand = CONST_RENAME then
  577.         if isEmpty(strNewFileName) then
  578.             intParseCmdLine = CONST_ERROR
  579.             Wscript.Echo ("The /N parameter is required when renaming a file.")
  580.         End If
  581.     End If
  582.     
  583.     
  584.  
  585. End Function
  586.  
  587. '********************************************************************
  588. '*
  589. '* Sub ShowUsage()
  590. '*
  591. '* Purpose: Shows the correct usage to the user.
  592. '*
  593. '* Input:   None
  594. '*
  595. '* Output:  Help messages are displayed on screen.
  596. '*
  597. '********************************************************************
  598. Private Sub ShowUsage()
  599.  
  600.     Wscript.Echo ""
  601.     Wscript.Echo "Perform various simple operations on a file."
  602.     Wscript.Echo ""
  603.     Wscript.Echo "SYNTAX:"
  604.     Wscript.Echo "1.  Fileman.vbs /D <targetfile> | /T <targetfile>" 
  605.     Wscript.Echo "               [/S <server>][/U <username>][/W <password>]"
  606.     Wscript.Echo "               [/O <outputfile>]"
  607.     Wscript.Echo ""
  608.     Wscript.Echo "2.  Fileman.vbs /R <targetfile> | /C <targetfile>"
  609.     Wscript.Echo "                /N <newfile>"
  610.     Wscript.Echo "               [/S <server>][/U <username>][/W <password>]"
  611.     Wscript.Echo "               [/O <outputfile>]"
  612.     Wscript.Echo ""
  613.     Wscript.Echo "PARAMETER SPECIFIERS:"
  614.     Wscript.Echo "   /D            Delete File"
  615.     Wscript.Echo "   /T            Take Ownership of file"
  616.     Wscript.Echo "   /R            Rename File"
  617.     Wscript.Echo "   /C            Copy File"   
  618.     Wscript.Echo "   targetfile    The target file."
  619.     Wscript.Echo "   newfile       The destination filename."
  620.     Wscript.Echo "   server        A machine name."
  621.     Wscript.Echo "   username      The current user's name."
  622.     Wscript.Echo "   password      Password of the current user."
  623.     Wscript.Echo "   outputfile    The output file name."
  624.     Wscript.Echo ""
  625.     Wscript.Echo "EXAMPLE:"
  626.     Wscript.Echo "1. cscript Fileman.vbs /d c:\test.txt /s MyMachine2"
  627.     Wscript.Echo "   Deletes the file test.txt on the machine MyMachine2."
  628.     Wscript.Echo "2. cscript Fileman.vbs /r c:\test.txt /n c:\new.txt"
  629.     Wscript.Echo "   Renames the file test.txt to new.txt on the " _
  630.                & "local machine."
  631.     Wscript.Echo ""
  632.     Wscript.Echo "NOTE:"
  633.     Wscript.Echo "1.   You must include the full path when specifying " _
  634.                & "the files."
  635.     Wscript.Echo ""
  636.     Wscript.Echo "2.   Wildcards (*, ?) will not work."
  637. End Sub
  638. '********************************************************************
  639. '* General Routines
  640. '********************************************************************
  641.  
  642.  
  643. '********************************************************************
  644. '*
  645. '* Function blnCheckFile()
  646. '*
  647. '* Purpose: Checks to see if file is read only or marked as a system file.
  648. '*
  649. '* Input:   strFileName           The file to check
  650. '*          objService            The WBEM object
  651. '*          blnForce              Whether to force an action on a read only file.
  652. '*
  653. '* Output:  Ture or False
  654. '*
  655. '********************************************************************
  656.  
  657. Function blnCheckFile(strFileName,objService,blnForce)
  658.  
  659.  
  660.     ON ERROR RESUME NEXT
  661.  
  662.     Dim objFileSet
  663.     Dim strWBEMClass
  664.     Dim Inst
  665.  
  666.     If blnForce=True Then
  667.         blnCheckFile=True
  668.         Exit Function
  669.     Else
  670.         blnCheckFile=False
  671.     End If
  672.     strFileName = strDoubleBackSlashes(strFileName)
  673.  
  674.   
  675.     strWBEMClass = "CIM_LogicalFile"
  676.  
  677.     Set objFileSet = objService.ExecQuery("SELECT * FROM " & _
  678.                      strWBEMClass & " WHERE Name = """ & strFileName & """",,0)
  679.  
  680.     If blnErrorOccurred("Could not obtain " & strWBEMClass & " instance.") Then
  681.         Exit Function
  682.     End If
  683.  
  684.     For Each Inst In objFileSet
  685.         If Inst.System = True then
  686.             Exit Function
  687.         ElseIf inst.Writeable = False then
  688.             Exit Function
  689.         Else
  690.             blnCheckFile = True
  691.         End If
  692.     Next
  693.  
  694. End Function
  695.  
  696. '********************************************************************
  697. '*  Function: strDoubleBackSlashes (strIn)
  698. '*
  699. '*  Purpose:  expand path string to use double node-delimiters;
  700. '*            doubles ALL backslashes.
  701. '*
  702. '*  Input:    strIn    path to file or directory
  703. '*  Output:            WMI query-food
  704. '*
  705. '*  eg:      c:\pagefile.sys     becomes   c:\\pagefile.sys
  706. '*  but:     \\server\share\     becomes   \\\\server\\share\\
  707. '*
  708. '********************************************************************
  709. Private Function strDoubleBackSlashes (strIn)
  710.     Dim i, str, strC
  711.     str = ""
  712.     for i = 1 to len (strIn)
  713.         strC = Mid (strIn, i, 1)
  714.         str = str & strC
  715.         if strC = "\" then str = str & strC
  716.     next
  717.     strDoubleBackSlashes = str
  718. End Function
  719.  
  720.  
  721. '********************************************************************
  722. '*
  723. '* Function strPackString()
  724. '*
  725. '* Purpose: Attaches spaces to a string to increase the length to intWidth.
  726. '*
  727. '* Input:   strString   a string
  728. '*          intWidth    the intended length of the string
  729. '*          blnAfter    Should spaces be added after the string?
  730. '*          blnTruncate specifies whether to truncate the string or not if
  731. '*                      the string length is longer than intWidth
  732. '*
  733. '* Output:  strPackString is returned as the packed string.
  734. '*
  735. '********************************************************************
  736. Private Function strPackString( ByVal strString, _
  737.                                 ByVal intWidth,  _
  738.                                 ByVal blnAfter,  _
  739.                                 ByVal blnTruncate)
  740.  
  741.     ON ERROR RESUME NEXT
  742.  
  743.     intWidth      = CInt(intWidth)
  744.     blnAfter      = CBool(blnAfter)
  745.     blnTruncate   = CBool(blnTruncate)
  746.  
  747.     If Err.Number Then
  748.         Call Wscript.Echo ("Argument type is incorrect!")
  749.         Err.Clear
  750.         Wscript.Quit
  751.     End If
  752.  
  753.     If IsNull(strString) Then
  754.         strPackString = "null" & Space(intWidth-4)
  755.         Exit Function
  756.     End If
  757.  
  758.     strString = CStr(strString)
  759.     If Err.Number Then
  760.         Call Wscript.Echo ("Argument type is incorrect!")
  761.         Err.Clear
  762.         Wscript.Quit
  763.     End If
  764.  
  765.     If intWidth > Len(strString) Then
  766.         If blnAfter Then
  767.             strPackString = strString & Space(intWidth-Len(strString))
  768.         Else
  769.             strPackString = Space(intWidth-Len(strString)) & strString & " "
  770.         End If
  771.     Else
  772.         If blnTruncate Then
  773.             strPackString = Left(strString, intWidth-1) & " "
  774.         Else
  775.             strPackString = strString & " "
  776.         End If
  777.     End If
  778.  
  779. End Function
  780.  
  781. '********************************************************************
  782. '* 
  783. '*  Function blnGetArg()
  784. '*
  785. '*  Purpose: Helper to intParseCmdLine()
  786. '* 
  787. '*  Usage:
  788. '*
  789. '*     Case "/s" 
  790. '*       blnGetArg ("server name", strServer, intArgIter)
  791. '*
  792. '********************************************************************
  793. Private Function blnGetArg ( ByVal StrVarName,   _
  794.                              ByRef strVar,       _
  795.                              ByRef intArgIter) 
  796.  
  797.     blnGetArg = False 'failure, changed to True upon successful completion
  798.  
  799.     If Len(Wscript.Arguments(intArgIter)) > 2 then
  800.         If Mid(Wscript.Arguments(intArgIter),3,1) = ":" then
  801.             If Len(Wscript.Arguments(intArgIter)) > 3 then
  802.                 strVar = Right(Wscript.Arguments(intArgIter), _
  803.                          Len(Wscript.Arguments(intArgIter)) - 3)
  804.                 blnGetArg = True
  805.                 Exit Function
  806.             Else
  807.                 intArgIter = intArgIter + 1
  808.                 If intArgIter > (Wscript.Arguments.Count - 1) Then
  809.                     Call Wscript.Echo( "Invalid " & StrVarName & ".")
  810.                     Call Wscript.Echo( "Please check the input and try again.")
  811.                     Exit Function
  812.                 End If
  813.  
  814.                 strVar = Wscript.Arguments.Item(intArgIter)
  815.                 If Err.Number Then
  816.                     Call Wscript.Echo( "Invalid " & StrVarName & ".")
  817.                     Call Wscript.Echo( "Please check the input and try again.")
  818.                     Exit Function
  819.                 End If
  820.  
  821.                 If InStr(strVar, "/") Then
  822.                     Call Wscript.Echo( "Invalid " & StrVarName)
  823.                     Call Wscript.Echo( "Please check the input and try again.")
  824.                     Exit Function
  825.                 End If
  826.  
  827.                 blnGetArg = True 'success
  828.             End If
  829.         Else
  830.             strVar = Right(Wscript.Arguments(intArgIter), _
  831.                      Len(Wscript.Arguments(intArgIter)) - 2)
  832.             blnGetArg = True 'success
  833.             Exit Function
  834.         End If
  835.     Else
  836.         intArgIter = intArgIter + 1
  837.         If intArgIter > (Wscript.Arguments.Count - 1) Then
  838.             Call Wscript.Echo( "Invalid " & StrVarName & ".")
  839.             Call Wscript.Echo( "Please check the input and try again.")
  840.             Exit Function
  841.         End If
  842.  
  843.         strVar = Wscript.Arguments.Item(intArgIter)
  844.         If Err.Number Then
  845.             Call Wscript.Echo( "Invalid " & StrVarName & ".")
  846.             Call Wscript.Echo( "Please check the input and try again.")
  847.             Exit Function
  848.         End If
  849.  
  850.         If InStr(strVar, "/") Then
  851.             Call Wscript.Echo( "Invalid " & StrVarName)
  852.             Call Wscript.Echo( "Please check the input and try again.")
  853.             Exit Function
  854.         End If
  855.         blnGetArg = True 'success
  856.     End If
  857. End Function
  858.  
  859. '********************************************************************
  860. '*
  861. '* Function blnConnect()
  862. '*
  863. '* Purpose: Connects to machine strServer.
  864. '*
  865. '* Input:   strServer       a machine name
  866. '*          strNameSpace    a namespace
  867. '*          strUserName     name of the current user
  868. '*          strPassword     password of the current user
  869. '*
  870. '* Output:  objService is returned  as a service object.
  871. '*          strServer is set to local host if left unspecified
  872. '*
  873. '********************************************************************
  874. Private Function blnConnect(ByVal strNameSpace, _
  875.                             ByVal strUserName,  _
  876.                             ByVal strPassword,  _
  877.                             ByRef strServer,    _
  878.                             ByRef objService)
  879.  
  880.     ON ERROR RESUME NEXT
  881.  
  882.     Dim objLocator, objWshNet
  883.  
  884.     blnConnect = False     'There is no error.
  885.  
  886.     'Create Locator object to connect to remote CIM object manager
  887.     Set objLocator = CreateObject("WbemScripting.SWbemLocator")
  888.     If Err.Number then
  889.         Call Wscript.Echo( "Error 0x" & CStr(Hex(Err.Number)) & _
  890.                            " occurred in creating a locator object." )
  891.         If Err.Description <> "" Then
  892.             Call Wscript.Echo( "Error description: " & Err.Description & "." )
  893.         End If
  894.         Err.Clear
  895.         blnConnect = True     'An error occurred
  896.         Exit Function
  897.     End If
  898.  
  899.     'Connect to the namespace which is either local or remote
  900.     Set objService = objLocator.ConnectServer (strServer, strNameSpace, _
  901.        strUserName, strPassword)
  902.     ObjService.Security_.impersonationlevel = 3
  903.     If Err.Number then
  904.         Call Wscript.Echo( "Error 0x" & CStr(Hex(Err.Number)) & _
  905.                            " occurred in connecting to server " _
  906.            & strServer & ".")
  907.         If Err.Description <> "" Then
  908.             Call Wscript.Echo( "Error description: " & Err.Description & "." )
  909.         End If
  910.         Err.Clear
  911.         blnConnect = True     'An error occurred
  912.     End If
  913.  
  914.     'Get the current server's name if left unspecified
  915.     If IsEmpty(strServer) Then
  916.         Set objWshNet = CreateObject("Wscript.Network")
  917.     strServer     = objWshNet.ComputerName
  918.     End If
  919.  
  920. End Function
  921.  
  922. '********************************************************************
  923. '*
  924. '* Sub      VerifyHostIsCscript()
  925. '*
  926. '* Purpose: Determines which program is used to run this script.
  927. '*
  928. '* Input:   None
  929. '*
  930. '* Output:  If host is not cscript, then an error message is printed 
  931. '*          and the script is aborted.
  932. '*
  933. '********************************************************************
  934. Sub VerifyHostIsCscript()
  935.  
  936.     ON ERROR RESUME NEXT
  937.  
  938.     Dim strFullName, strCommand, i, j, intStatus
  939.  
  940.     strFullName = WScript.FullName
  941.  
  942.     If Err.Number then
  943.         Call Wscript.Echo( "Error 0x" & CStr(Hex(Err.Number)) & " occurred." )
  944.         If Err.Description <> "" Then
  945.             Call Wscript.Echo( "Error description: " & Err.Description & "." )
  946.         End If
  947.         intStatus =  CONST_ERROR
  948.     End If
  949.  
  950.     i = InStr(1, strFullName, ".exe", 1)
  951.     If i = 0 Then
  952.         intStatus =  CONST_ERROR
  953.     Else
  954.         j = InStrRev(strFullName, "\", i, 1)
  955.         If j = 0 Then
  956.             intStatus =  CONST_ERROR
  957.         Else
  958.             strCommand = Mid(strFullName, j+1, i-j-1)
  959.             Select Case LCase(strCommand)
  960.                 Case "cscript"
  961.                     intStatus = CONST_CSCRIPT
  962.                 Case "wscript"
  963.                     intStatus = CONST_WSCRIPT
  964.                 Case Else       'should never happen
  965.                     Call Wscript.Echo( "An unexpected program was used to " _
  966.                                        & "run this script." )
  967.                     Call Wscript.Echo( "Only CScript.Exe or WScript.Exe can " _
  968.                                        & "be used to run this script." )
  969.                     intStatus = CONST_ERROR
  970.                 End Select
  971.         End If
  972.     End If
  973.  
  974.     If intStatus <> CONST_CSCRIPT Then
  975.         Call WScript.Echo( "Please run this script using CScript." & vbCRLF & _
  976.              "This can be achieved by" & vbCRLF & _
  977.              "1. Using ""CScript FileMan.vbs arguments"" for Windows 95/98 or" _
  978.              & vbCRLF & "2. Changing the default Windows Scripting Host " _
  979.              & "setting to CScript" & vbCRLF & "    using ""CScript " _
  980.              & "//H:CScript //S"" and running the script using" & vbCRLF & _
  981.              "    ""FileMan.vbs arguments"" for Windows NT/2000." )
  982.         WScript.Quit
  983.     End If
  984.  
  985. End Sub
  986.  
  987. '********************************************************************
  988. '*
  989. '* Sub WriteLine()
  990. '* Purpose: Writes a text line either to a file or on screen.
  991. '* Input:   strMessage  the string to print
  992. '*          objFile     an output file object
  993. '* Output:  strMessage is either displayed on screen or written to a file.
  994. '*
  995. '********************************************************************
  996. Sub WriteLine(ByVal strMessage, ByVal objFile)
  997.  
  998.     On Error Resume Next
  999.     If IsObject(objFile) then        'objFile should be a file object
  1000.         objFile.WriteLine strMessage
  1001.     Else
  1002.         Call Wscript.Echo( strMessage )
  1003.     End If
  1004.  
  1005. End Sub
  1006.  
  1007. '********************************************************************
  1008. '* 
  1009. '* Function blnErrorOccurred()
  1010. '*
  1011. '* Purpose: Reports error with a string saying what the error occurred in.
  1012. '*
  1013. '* Input:   strIn        string saying what the error occurred in.
  1014. '*
  1015. '* Output:  displayed on screen 
  1016. '* 
  1017. '********************************************************************
  1018. Private Function blnErrorOccurred (ByVal strIn)
  1019.  
  1020.     If Err.Number Then
  1021.         Call Wscript.Echo( "Error 0x" & CStr(Hex(Err.Number)) & ": " & strIn)
  1022.         If Err.Description <> "" Then
  1023.             Call Wscript.Echo( "Error description: " & Err.Description)
  1024.         End If
  1025.         Err.Clear
  1026.         blnErrorOccurred = True
  1027.     Else
  1028.         blnErrorOccurred = False
  1029.     End If
  1030.  
  1031. End Function
  1032.  
  1033. '********************************************************************
  1034. '* 
  1035. '* Function blnOpenFile
  1036. '*
  1037. '* Purpose: Opens a file.
  1038. '*
  1039. '* Input:   strFileName        A string with the name of the file.
  1040. '*
  1041. '* Output:  Sets objOpenFile to a FileSystemObject and setis it to 
  1042. '*            Nothing upon Failure.
  1043. '* 
  1044. '********************************************************************
  1045. Private Function blnOpenFile(ByVal strFileName, ByRef objOpenFile)
  1046.  
  1047.     ON ERROR RESUME NEXT
  1048.  
  1049.     Dim objFileSystem
  1050.  
  1051.     Set objFileSystem = Nothing
  1052.  
  1053.     If IsEmpty(strFileName) OR strFileName = "" Then
  1054.         blnOpenFile = False
  1055.         Set objOpenFile = Nothing
  1056.         Exit Function
  1057.     End If
  1058.  
  1059.     'Create a file object
  1060.     Set objFileSystem = CreateObject("Scripting.FileSystemObject")
  1061.     If blnErrorOccurred("Could not create filesystem object.") Then
  1062.         blnOpenFile = False
  1063.         Set objOpenFile = Nothing
  1064.         Exit Function
  1065.     End If
  1066.  
  1067.     'Open the file for output
  1068.     Set objOpenFile = objFileSystem.OpenTextFile(strFileName, 8, True)
  1069.     If blnErrorOccurred("Could not open") Then
  1070.         blnOpenFile = False
  1071.         Set objOpenFile = Nothing
  1072.         Exit Function
  1073.     End If
  1074.     blnOpenFile = True
  1075.  
  1076. End Function
  1077.  
  1078. '********************************************************************
  1079. '*                                                                  *
  1080. '*                           End of File                            *
  1081. '*                                                                  *
  1082. '********************************************************************
  1083.  
  1084.  
  1085.