Script to find a specific USB drive and copy a file?

Page 1 of 6 123 ... LastLast

  1. Posts : 720
    Win10 x64 Pro - 2 desktops, 2 laptops
       #1

    Script to find a specific USB drive and copy a file?


    I need help writing a script to find a specific USB flash drive and copy a file. I know the path to the file except for the drive letter. The USB flash drive could be any of 3 - call them USB_Drive1, USB_Drive2, and USB_Drive3. The actual copy is no problem, but I have no idea how to find the drive letter.

    The script has to run on both Windows 10 and very old (by definition) Windows 7. I assume the the Win7 systems have PowerShell 3.0 but I don't know for sure.

    Are there any sample scripts I can look at to help me along? I'm completely PS-illiterate so I'm likely to as lots more questions if the examples are in PS.

    I'm running Win 10 20H2, build 19042.1110, but that's not very meaningful information since the script has to run on Win7.
      My Computer


  2. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
       #2

    Hello @pokeefe0001,

    Just as an alternative which would also be useful in the future rather than having to Hardcode [ Change ] the File Names, there is this . . .

     Everything - FREE

    Everything [ FREE ] is an extremely quick search engine that locates files and folders by filename instantly. Unlike Windows search, it initially displays every file and folder for which the criteria of the search matches.

    About Everything:

    • Small installation file.
    • Clean and simple user interface.
    • Quick file indexing.
    • Quick searching.
    • Minimal resource usage.
    • Share files with others easily.
    • Real-time updating.

    > Website & Download => Everything - VoidTools



    I hope this helps.
      My Computer


  3. Posts : 720
    Win10 x64 Pro - 2 desktops, 2 laptops
    Thread Starter
       #3

    I looked at it but I don't see how it helps. I need something in a script, not something with a GUI. Did I miss something? Is there a command line invocation?
      My Computer


  4. Posts : 989
    Microsoft Windows 10 Home
       #4

    Not sure if you want t.o search drives by name or all USB drives. I'm going with the latter because it's easier.


    '

    Code:
    $DestinationDir = 'c:\BaseFolder'
    $RootLessPath   = 'Music\04 Track 04 2.m4a'
    
    
    ([System.IO.DriveInfo]::GetDrives() | Where DriveType -eq 'Removable').Name |
      Where { Test-Path ( $FoundPath = Join-Path $_ $RootLessPath ) } |
    ForEach-Object{
        Copy-Item $FoundPath $DestinationDir -WhatIf
    }
    Last edited by KeithM; 06 Aug 2021 at 12:23.
      My Computer


  5. Posts : 720
    Win10 x64 Pro - 2 desktops, 2 laptops
    Thread Starter
       #5

    I think I'd better start over trying to explain what I need. The example above does me no good because "$DestinationDir = 'c:\<WhereEver>'" is exactly what I don't have. "\<WhereEver>", yes'; "c:", no.

    There is a USB flash memory drive named USB_Drive1, USB_Drive2, or USB_Drive3 probably plugged in a USB port. I have no idea what its assigned drive letter is, but I need to copy the file \WhereEver from it. If it makes things easier I'm willing to assume the drive is plugged in somewhere.
      My Computer


  6. Posts : 989
    Microsoft Windows 10 Home
       #6

    I got it the first time around. But now you're saying you don't have a destination -- where you plan on copying the file to!

    Unless your drive-letterless path exists on more than one drive, the volume labels are superfluous except narrow the search if you have 20 or so drives connected at once.

    The code I posted gets the drive letters for every attached removable drvie, combines each with the path stored in $RootLessPath and tests to see which, if any, is valid. Valid names are passed on the the ForEach Copy-Item scriptblock. But again, you have to have an idea of where you want to copy the files to -- and that is generally referred to as a destination.

    So if three uSB drives were connected, with drive letters K, L, and M, all three possible paths:
    • K:\Music\04 Track 04 2.m4a
    • L:\Music\04 Track 04 2.m4a
    • M:\Music\04 Track 04 2.m4a


    are created with Join-Path and then tested to see if htey refer to an actual file. Valid paths are passed on to Copy-Item.

    If you want to narrow the search to specific volume labels, put them in a string array:
    Code:
    $USBLabels = @'
    USB_Drive1
    USB_Drive2
    USB_Drive3
    '@ -split "`n"
    and use htat array to filter the drives returned:
    Code:
    ([System.IO.DriveInfo]::GetDrives() | Where DriveType -eq 'Removable' | Where VolumeLabel -in $USBLabels ).Name |
      Where { Test-Path ( $FoundPath = Join-Path $_ $RootLessPath ) } |
    ForEach-Object
    {
        Copy-Item $FoundPath $DestinationDir
    }
      My Computer


  7. Posts : 720
    Win10 x64 Pro - 2 desktops, 2 laptops
    Thread Starter
       #7

    Ah. I misinterpreted "destination" because I haven't been concerned with destination of the copy. I obviously didn't look at the scripts. The destination will be on the drive containing the script - yest another USB flash drive. If this were a .bat file, destination would (I think) be %~dp0%<WhatEver>%.

    I'll have to tweak the code (while trying to learn PS). There should be only one of the 3 USB drives found so I won't need an array; I'll just pick the first one whose label contains the sought string.
      My Computer


  8. Posts : 16,910
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #8

    If you are interested in a batch file solution, here it is.

    This script will search for a drive by its label and report back the drive letter so you can use the variable it sets for whatever copying operation you want.
    - Method of use - The drive label is passed by a shortcut to the batch file or by another script that calls it.
    Code:
    H:\...\FindDriveLetter-Label - TenForums.bat Fred
    - Alternative output - You can also pass a drive letter and the batch file reports the drive label.
    - Output variable - %ThisOutput% - in all cases
    - The final line, a Pause command, is normally remarked out ::Pause. I left it operating so you could watch the behaviour of the script.

    This batch file version has been superceded by that provided in post #36.


    Denis
    Last edited by Try3; 09 Aug 2021 at 01:23.
      My Computer


  9. Posts : 16
    Windows 10 - 20H2 Build 19042.1052
       #9

    What is Windows disk management showing?
      My Computers


  10. Posts : 989
    Microsoft Windows 10 Home
       #10

    pokeefe0001 said:
    Ah. I misinterpreted "destination" because I haven't been concerned with destination of the copy. I obviously didn't look at the scripts. The destination will be on the drive containing the script - yest another USB flash drive. If this were a .bat file, destination would (I think) be %~dp0%<WhatEver>%.

    I'll have to tweak the code (while trying to learn PS). There should be only one of the 3 USB drives found so I won't need an array; I'll just pick the first one whose label contains the sought string.
    Take another look -- the array isn't for collecting results, it holds the three valid VolumeLabels you want to limit your search to. So instead of testing against three separate values:

    Code:
    If ( $MyUSBlabel -eq  $Label1 ) -or ( $MyUSBlabel -eq $Label2 ) -or ( $MyUSBlabel -eq $Label3 )
    You put the reference values in an array and test against the whole collection at once:
    Code:
    $ValidLabels = ( 'Label1', 'Label2', 'Label3' )
    
    If ( $MyUSBlabel -in $ValidLabels )
    
              # # or ##
    
    If ( $ValidLabels -contains $MyUSBlabel )
    It's faster & makes your code more readable.
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 08:06.
Find Us




Windows 10 Forums