0

I have two scripts. Both are within the same folder on the hard disk. The "vbs" call "bat" to detect usb to run some commands.

I want to incorporate the function of selecting the usb to vbs. Thanks

usb.vbs

Option Explicit
On Error Resume next
mensaje = MSGBOX ("Start USB fix", vbOKCancel, "USB fix")
If mensaje = vbOK Then
SCRIPT = "usb.bat"
Set objShell = CreateObject("Wscript.Shell")
strPath = Wscript.ScriptFullName
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(strPath)
strFolder = objFSO.GetParentFolderName(objFile) 

NewPath = objFSO.BuildPath(strFolder, SCRIPT)
set objshell = createobject("wscript.shell")
objshell.run NewPath, 1, true
    Else
End If
On Error GoTo 0

usb.bat

@echo off
set /p drive=Choose the drive usb letter:
if exist %drive%: (goto target)
:target
attrib /d /s -r -h -s %drive%:\*.*
:: ext
call:ext "*.ini*"
call:ext "*.lnk*"
exit

:: funcion ext
@echo off
pause
goto:eof
:ext
set ext=%1
del /f/q/s %drive%:\"%ext%"
goto:eof

IMPORTANT NOTE: no answer is correct.

  • Please [edit] your question to be more specific on I could not make it work phrase as it's too broad. Quote all error messages in particular… – JosefZ Jun 26 '16 at 20:54
  • 1
    would it be simpler to block all .ini, /.lnk with a antivirus rule for all usb disk ? – yagmoth555 Jun 27 '16 at 14:27
  • why not use applocker or software restriction policies (depending on OS) to do this? – Jim B Jun 27 '16 at 14:31
  • For clarity, I have updated the question and scripts –  Jun 27 '16 at 19:18
  • 1
    @alejc changed question from that altered topic. I gave answer to your original topic, then another answer to altered topic… Never more… If any answer was helpful, please consider marking it as accepted. See this page for an explanation of why this is important. Moreover, your question is off-topic here: questions on Server Fault must be about managing information technology systems in a business environment. Questions about development, testing and development tools may be asked on Stack Overflow. Ask a new question there, pls – JosefZ Jul 07 '16 at 15:43
  • JosefZ. The answers to the original questions were not answered clearly and did not solve the problem. Therefore, i decided to rephrase the question to make it easier to answer. If you do not agree with this change, then you can correctly answer the original question and I will select your answer as correct. Thanks –  Jul 08 '16 at 16:46

2 Answers2

2
  1. VBScript: please read Rob Haupt's five VBScript 'best practices' and Rob van der Woude's Debugging Your Scripts; in short:

  2. Batch script: please read Rob van der Woude's Debugging your batch files.

  3. Why not to stay in a batch script for all given task? Let's apply wmic command: the Win32_Volume class (represents an area of storage on a hard disk) or the Win32_LogicalDisk WMI class (represents a data source that resolves to an actual local storage device on a computer system running Windows).

Try yourself *** see edit below:

wmic path Win32_Volume get BlockSize, DriveLetter, DriveType, Label
wmic path Win32_LogicalDisk get DeviceID, DriveType, Description, VolumeName, FileSystem
rem or full output:
wmic path Win32_Volume get /value
wmic path Win32_LogicalDisk get /value

Here's possible skeleton of your batch script (put it together with your usb.bat script yourself):

@ECHO OFF
SETLOCAL EnableExtensions DisableDelayedExpansion
set "_drives="
for /F "tokens=*" %%G in ('
  wmic path Win32_LogicalDisk where "DriveType=2" get DeviceID /value ^|findstr "="
  ') do for %%g in ("%%~G") do (
    set "__%%~g"
    echo(
    rem echo going to process drive __%%~g
    call :processDrive
  )

ENDLOCAL
goto :eof

:processDrive
  echo processing drive %__DeviceID%
  pushd "%__DeviceID%\"
    2>NUL dir /S /B /A *.ini
    2>NUL dir /S /B /A *.lnk
  popd
goto :eof

Here the for loops are

  • %%G to retrieve the DeviceID value;
  • %%g to remove the ending carriage return in the value returned: wmic behaviour: each output line ends with 0x0D0D0A (<CR><CR><LF>) instead of common 0x0D0A (<CR><LF>).

See Dave Benham's WMIC and FOR /F: A fix for the trailing <CR> problem

Output:

==> D:\bat\SF\786392.bat

processing drive F:
F:\Shortcut.lnk
F:\vbScriptDoc\Hey_Scripting_Guy.lnk

processing drive G:
G:\SPSS\admin\SCRIPTS.INI
G:\SPSS\admin\system32\GroupPolicy\GPT.INI
G:\VB_scripts\Net\nethood_create_a_link.vbs.lnk

==>

Further resources (required reading for a batch sripter):

*** Edit: unfortunately, querying DriveType property in both Win32_Volume and Win32_LogicalDisk wmi classes could give false results, see next output where both F: and G: are USB removable media so that DriveType property should be 2:

==> wmic path Win32_LogicalDisk get DeviceID,DriveType,Description,VolumeName, FileSystem,Size
Description       DeviceID  DriveType  FileSystem  Size           VolumeName
Local Fixed Disk  C:        3          NTFS        119664537600
Local Fixed Disk  D:        3          NTFS        1000202039296  DataDisk
CD-ROM Disc       E:        5
Removable Disk    F:        2          FAT         519274496      KINGSTON
Local Fixed Disk  G:        3          FAT32       500044136448   GOG

==> wmic path Win32_Volume get BlockSize, DriveLetter, DriveType, Label, Capacity
BlockSize  Capacity       DriveLetter  DriveType  Label
4096       1000202039296  D:           3          DataDisk
4096       366997504                   3          Rezervováno systémem
8192       519274496      F:           2          KINGSTON
65536      500044136448   G:           3          GOG
4096       119664537600   C:           3
                          E:           5

You need to combine next WMI/WMIC queries to get right drive letters of removable disks:

==> wmic path Win32_DiskDrive get DeviceID, InterfaceType, MediaType

DeviceID            InterfaceType  MediaType
\\.\PHYSICALDRIVE1  IDE            Fixed hard disk media
\\.\PHYSICALDRIVE2  USB            Removable Media
\\.\PHYSICALDRIVE0  IDE            Fixed hard disk media
\\.\PHYSICALDRIVE3  USB            External hard disk media


==> wmic path Win32_DiskDriveToDiskPartition get /value

Antecedent="\\USER-PC\root\cimv2:Win32_DiskDrive.DeviceID="\\.\PHYSICALDRIVE1""
Dependent="\\USER-PC\root\cimv2:Win32_DiskPartition.DeviceID="Disk #1, Partition #0""

Antecedent="\\USER-PC\root\cimv2:Win32_DiskDrive.DeviceID="\\.\PHYSICALDRIVE1""
Dependent="\\USER-PC\root\cimv2:Win32_DiskPartition.DeviceID="Disk #1, Partition #1""

Antecedent="\\USER-PC\root\cimv2:Win32_DiskDrive.DeviceID="\\.\PHYSICALDRIVE2""
Dependent="\\USER-PC\root\cimv2:Win32_DiskPartition.DeviceID="Disk #2, Partition #0""

Antecedent="\\USER-PC\root\cimv2:Win32_DiskDrive.DeviceID="\\.\PHYSICALDRIVE0""
Dependent="\\USER-PC\root\cimv2:Win32_DiskPartition.DeviceID="Disk #0, Partition #0""

Antecedent="\\USER-PC\root\cimv2:Win32_DiskDrive.DeviceID="\\.\PHYSICALDRIVE3""
Dependent="\\USER-PC\root\cimv2:Win32_DiskPartition.DeviceID="Disk #3, Partition #0""



==> wmic path Win32_LogicalDiskToPartition get /value

Antecedent="\\USER-PC\root\cimv2:Win32_DiskPartition.DeviceID="Disk #1, Partition #1""
Dependent="\\USER-PC\root\cimv2:Win32_LogicalDisk.DeviceID="C:""
EndingAddress=120032591871
StartingAddress=368050176

Antecedent="\\USER-PC\root\cimv2:Win32_DiskPartition.DeviceID="Disk #0, Partition #0""
Dependent="\\USER-PC\root\cimv2:Win32_LogicalDisk.DeviceID="D:""
EndingAddress=1000203091967
StartingAddress=1048576

Antecedent="\\USER-PC\root\cimv2:Win32_DiskPartition.DeviceID="Disk #2, Partition #0""
Dependent="\\USER-PC\root\cimv2:Win32_LogicalDisk.DeviceID="F:""
EndingAddress=519569407
StartingAddress=16384

Antecedent="\\USER-PC\root\cimv2:Win32_DiskPartition.DeviceID="Disk #3, Partition #0""
Dependent="\\USER-PC\root\cimv2:Win32_LogicalDisk.DeviceID="G:""
EndingAddress=500105249279
StartingAddress=32256
JosefZ
  • 1,584
  • Thank you very much for your reply and for your great effort to explain, but it's not what I want. What I want is to fix the line that detects the USB drive letter: (ObjDrive.DriveLetter & ": \ =" & objDrive.VolumeName, vbInformation, scriptBaseName) and add what is missing from "usb.vbs" to "usb.vbs" tell "usb.bat" what is the usb drive where it should run. It's all ("usb.bat" is correct)... sorry for my english –  Jun 27 '16 at 14:10
  • Your usb.vbs is not well-written. However, that problematic line is a parameter list of msgbox function. Compute strDrive = objDrive.DriveLetter & ":\" and then supply it to your usb.bat script as follows: objshell.run """" & NewPath & """ " & strDrive, 1, true or even you could specify objshell.run "cmd.exe /C """ & NewPath & """ " & strDrive, 1, true. – JosefZ Jun 27 '16 at 15:29
  • Re-edit the question –  Jul 07 '16 at 14:13
0

Here are my artless changes to your usb.vbs. Note that most MSGBOX are replaced as I hate all dialog boxes, popup boxes etc. if their output is useless in given context:

' VB Script Document
option explicit
'On Error Resume Next
On Error GoTo 0
Dim strResult: strResult = Wscript.ScriptName

Dim objFSO, objShell, objDrive, scriptBaseName
Dim mensaje, SCRIPT
Dim strPath, objFile, strFolder, NewPath, strDrive, strcmd, iResult

mensaje = MSGBOX ("Start USB fix", vbOKCancel, "USB fix")
If mensaje = vbOK Then
  Set objFSO = CreateObject("Scripting.FileSystemObject") 
  Set objShell = CreateObject("Wscript.Shell")
  scriptBaseName = objFSO.GetBaseName(Wscript.ScriptFullName)
      SCRIPT = "usb.bat"
      strPath = Wscript.ScriptFullName
      Set objFile = objFSO.GetFile(strPath)
      strFolder = objFSO.GetParentFolderName(objFile) 
      NewPath = objFSO.BuildPath(strFolder, SCRIPT)
  For Each objDrive In objFSO.Drives
    strResult = strResult & vbNewLine & objDrive.DriveLetter & vbTab & objDrive.DriveType
    If objDrive.DriveType = 1 And objDrive.IsReady Then ''

      strDrive = objDrive.DriveLetter & ":\"
      '''xmsgbox objDrive.DriveLetter & ":\ = " & objDrive.VolumeName, vbInformation, scriptBaseName
      'strcmd = "cmd.exe /C """ & NewPath & """ " & strDrive ' executable
      strcmd = """" & NewPath & """ " & strDrive                 ' batch script
      iResult = objshell.run( strcmd , 1, true)
      strResult = strResult & vbTab & strcmd & vbTab & iResult
      '''CALL xMSGBOX ("Finish USB fix", VBOKONLY, "USB fix")
    End If
  Next
Else
  CALL xMSGBOX ("Cancel USB fix", VBOKONLY, "USB fix")
End If

'strResult = strResult & vbNewLine & 

Wscript.Echo strResult
Wscript.Quit

Sub xMSGBOX(byVal sPrompt, byVal sButtons, byVal sTitle)
  strResult = strResult & vbNewLine & "=" & sPrompt & " =" & sButtons  & " =" & sTitle  
End Sub

Tested using next usb.bat:

@ECHO OFF
SETLOCAL EnableExtensions
echo 0th parameter = %0
echo     script = %~f0
echo parameters = %*
pause
ENDLOCAL
exit /B

Edit unfortunately, DriveType Property of a Drive object in FileSystemObject could could give the same false results as querying DriveType property in both Win32_Volume and Win32_LogicalDisk wmi classes. Cf output from above 786392.vbs script (run on the same data as in example in my other answer to current question). In fact, drive G here is an USB removable disk so that should be of DriveType 1.

==> cscript D:\VB_scripts\SF\786392.vbs
786392.vbs
C       2       drive type=2
D       2       drive type=2
E       4       drive type=4
F       1       "D:\VB_scripts\SF\usb.bat" F:\  0
G       2       drive type=2

==>

JosefZ
  • 1,584
  • Thanks JosefZ for your answers, but doesn't work. For more clarity, I have updated the question and scripts. Thanks again –  Jun 27 '16 at 19:38
  • @alejc the doesn't work phrase is indeed very exhaustive :)) For more clarity, this is my last post on current thread and question. – JosefZ Jun 27 '16 at 20:00
  • It's a shame I was not able to find the solution to what I want. Anyway, thank you very much for your help and time spent. –  Jun 27 '16 at 22:35
  • Note that desired drive specification is supplied to testing usb.bat as the first parameter so that you could use set "drive=%1" instead of set /p drive=Choose the drive usb letter: – JosefZ Jun 27 '16 at 22:57
  • Update the question –  Jul 07 '16 at 14:12