作者oohay (五黑)
看板Programming
标题Re: [问题] Dos AutoBatch
时间Fri Jan 18 22:13:03 2008
※ 引述《TwoDemon (店小二)》之铭言:
: 如果将一个目录下一次移10个档至另一个目录。
: 我原来的写法是全移: move D:\test1\*.txt C:\test2\
: 但现在要改成一次最多只移10个档。
: 但,要怎麽写成一个完整可以运作的script,希望版上高手帮帮忙,谢谢!
其实看熟指令之後还蛮好写的,也不见得都只能用For.
下面这程式要四个参数:来源目录,档案筛选文字,目的目录,移档一批数量.
第四个参数可以不加.
例如,假设程式储存为test.bat,使用的命令是:
test.bat test1 *.txt test2
预设移动10个档案,如果要改成移3个档案,就把第四个参数加进去:
test.bat test1 *.txt test2 3
@echo off
rem ================ DOS shell script under Windows XP ==================
rem ----------------------- Syntax checking -----------------------------
if "%1" == "" echo Syntax: %0 source_directory file_type destination_directory [max_move] & goto end
if "%2" == "" echo Syntax: %0 source_directory file_type destination_directory [max_move] & goto end
if "%3" == "" echo Syntax: %0 source_directory file_type destination_directory [max_move] & goto end
rem --------------------- Directory checking ----------------------------
if not exist "%1" echo Argument #1 must be a directory. & goto end
if not exist %1\nul echo Argument #1 must be a directory. & goto end
if not exist "%3" echo Argument #3 must be a directory. & goto end
if not exist %3\nul echo Argument #3 must be a directory. & goto end
setlocal
set count=0
set max=10
if not "%4" == "" set max=%4
:begin
rem ------------------------- File checking -----------------------------
dir /b %1\%2 > nul 2>&1
if errorlevel 1 goto end
for /f "usebackq" %%s in (`dir /b %1\%2`) do echo Move %1\%%s to %3 & move
%1\%%s %3 & goto stop
:stop
set /a count=count+1
if not %count% == %max% goto begin
endlocal
:end
@echo on
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 59.112.225.149