Search code examples
windowsbatch-filecopy-pasteusb-drive

How to copy a file from a USB drive to C drive in Windows 10 using a batch file?


I have some files in a USB drive which need to be copied to multiple computers. These files contain an executable which will use the other config files. My issue is, for Windows 10 PCs, while the temp_folder gets created, none of the files get copied.

For windows 7 I was able to create a batch file which copied the files to the local drive and ran the executable using the config files.

The batch file contents were as below :

mkdir C:\temp_installer
copy ".\file_name" "C:\temp_installer"
<rest of the code>

I have tried using xcopy and robocopy, but still see the batch file run and just stop at creating the folder. The same issue isn't observed in Windows 7.

Has someone tried this or can someone tell me what I might be doing wrong?


Solution

  • This would be a better option, we do not need to be concerened about permission issues on the root of C:

    @echo off
    cd /d "%~dp0"
    set "inst_dir=%temp%\temp_installer"
    mkdir "%inst_dir%">nul 2>&1
    for %%i in (*) do if not "%%i"=="%~nx0" copy /Y "%%i "%inst_dir%"
    :# When completed, we can call execute the files from "%inst_dir%"
    

    The for loop is not needed to be honest, I am only doing it to not copy the .bat/.cmd file itself to the folder as there would be no need for it there.

    Or even simpler, without having to do all the above, you could just use robocopy

    @echo off
    cd /d "%~dp0"
    robocopy /MIR .\ "%temp%\temp_installer"