none
Batch file to find removeable device and copy to drive RRS feed

  • Question

  • Trying to fix my script to find the removable device and copy. Everything is good so far except that. Any help is greatly appreciated.

    @echo off
    echo -------------------
    echo Stealin Yo Data BRAH
    echo -------------------
    xcopy "E:*.*" /e/c/h/k/r/s/g/i "c:\myfolder\"
    if errorlevel 4 goto inierror
    if errorlevel 5 goto write
    if errorlevel 0 goto exit
    :inierror
    echo One of the following occured Insufficient memory to copy files or low disk space or invalid drive specifications or invalid syntax
    goto exit
    :write
    echo Please verify the disk is not write protected
    goto exit
    :exit
    echo Its all mine
    pause.

    • Moved by Bill_Stewart Monday, December 1, 2014 8:50 PM This is not "fix script for me" forum
    Tuesday, October 28, 2014 7:08 PM

All replies

  • You could use this VBScript to find all drive letters located on USB drives:

    Set oWMIService = GetObject("winmgmts:\\.\root\cimv2")
    Set cDiskDrives = oWMIService.ExecQuery("SELECT * FROM Win32_DiskDrive")
    
    For Each oDrive In cDiskDrives
       If oDrive.InterfaceType = "USB" Then
          Set cPartitions = oWMIService.ExecQuery("ASSOCIATORS OF {Win32_DiskDrive.DeviceID=""" _
             & Replace(oDrive.DeviceID, "\", "\\") & """} WHERE AssocClass = " & "Win32_DiskDriveToDiskPartition")
     
          For Each oPartition In cPartitions
             aPartition = Split(oPartition.DeviceID)
             Set cLogicalDisks = oWMIService.ExecQuery _
                ("ASSOCIATORS OF {Win32_DiskPartition.DeviceID=""" & oPartition.DeviceID _
                & """} WHERE AssocClass = Win32_LogicalDiskToPartition")
             For Each oLogicalDisk In cLogicalDisks
                WScript.Echo oLogicalDisk.DeviceID
             Next
          Next
       End If
    Next
    

    Tuesday, October 28, 2014 8:19 PM
  • This is what I have come up with so far and it seems to work. 

    @echo off
    echo -------------------
    echo Stealin Yo Data BRAH
    echo -------------------
    @echo off
    setlocal
    for /f "tokens=1-3" %%a in ('wmic logicaldisk get caption^, description') do if "%%b %%c"=="Removable Disk" set drive=%%a&goto FOUND
    echo Couldn't detect removable drive
    pause.
    goto :EOF
    :FOUND
    echo Removable drive letter is %drive%
    mkdir c:\myfolder
    xcopy "%drive%*.*" /e/c/h/k/r/s/g/i "c:\myfolder\"
    echo %USER%
    if errorlevel 4 goto inierror
    if errorlevel 5 goto write
    if errorlevel 0 goto exit
    :inierror
    echo One of the following occured Insufficient memory to copy files or low disk space or invalid drive specifications or invalid syntax
    goto exit
    :write
    echo Please verify the disk is not write protected
    goto exit
    :exit
    echo All your info are belong to me now give me good grade!
    pause.

    Wednesday, October 29, 2014 12:57 PM
  • The advantage of your wmic.exe command is that is simple. Unfortunately it has two serious drawbacks:

    • It reports only one removable drive. If you have two then the first one does not get reported.
    • It is unreliable. It correctly identifies most removable disks, e.g. this one:
      wmic logicaldisk get caption, description
      Caption  Description
      C:       Local Fixed Disk
      D:       Local Fixed Disk
      E:       Local Fixed Disk
      F:       CD-ROM Disc
      G:       Removable Disk
      However, some USB disks are manufactured with a different geometry. They generate this output. Drive G: is still a flash disk.
      wmic logicaldisk get caption, description
      Caption  Description
      C:       Local Fixed Disk
      D:       Local Fixed Disk
      E:       Local Fixed Disk
      F:       CD-ROM Disc
      G:       Local Fixed Disk

    If you can live with these problems then you should use your command.

    Wednesday, October 29, 2014 7:11 PM
  • The advantage of your wmic.exe command is that is simple. Unfortunately it has two serious drawbacks:

    • It reports only one removable drive. If you have two then the first one does not get reported.
    • It is unreliable. It correctly identifies most removable disks, e.g. this one:
      wmic logicaldisk get caption, description
      Caption  Description
      C:       Local Fixed Disk
      D:       Local Fixed Disk
      E:       Local Fixed Disk
      F:       CD-ROM Disc
      G:       Removable Disk
      However, some USB disks are manufactured with a different geometry. They generate this output. Drive G: is still a flash disk.
      wmic logicaldisk get caption, description
      Caption  Description
      C:       Local Fixed Disk
      D:       Local Fixed Disk
      E:       Local Fixed Disk
      F:       CD-ROM Disc
      G:       Local Fixed Disk

    If you can live with these problems then you should use your command.

    Very good to know, your help is greatly appreciated. 


    Wednesday, October 29, 2014 7:26 PM