@echo off
setlocal enabledelayedexpansion
color f9
if not exist backup mkdir backup
warn.vbs 2 0
dir /s /b /o:n *.doc 1>nul 2>nul
if !errorlevel!==0 goto start
warn.vbs 1 0
exit
:start
if not exist DATA cd. >DATA
set num=0 ::记录写入文件的数目
::x值为0表示bak文件存在,y初值为0表示doc与bak无相同文件,y值为1表示有文件相同
dir /b /o:n "%cd%\backup\*.bak" 1>nul 2>nul
set x=!errorlevel!
for /f "delims=" %%a in ('dir /s /b /o:n *.doc') do (
set y=0
if %x%==0 (
call :fileCompare
if !y!==0 (
cscript //nologo main.vbs "%%a"
copy "%%a" "%cd%\backup\!date!-!time:~0,2!-!time:~3,2!-!time:~6,5!.bak" 1>nul 2>nul
set /a num+=1
echo 已写入 !num! 个文件
)
) else (
cscript //nologo main.vbs "%%a"
copy "%%a" "%cd%\backup\!date!-!time:~0,2!-!time:~3,2!-!time:~6,5!.bak" 1>nul 2>nul
set /a num+=1
echo 已写入 !num! 个文件
)
)
quit.vbs
warn.vbs 3 !num!
exit
::直接在内循环用goto跳转会跳出所有循环,所以通过call间接调用内循环
:fileCompare
for /f "delims=" %%b in ('dir /s /b /o:n "%cd%"\backup\*.bak') do (
fc /u "%%a" "%%b" 1>nul 2>nul
if !errorlevel!==0 (
set y=1
echo %%a :文件库已存在该文件
goto end
)
)
:end
set args=wscript.arguments
arg=args(0)
On Error Resume Next
'寻找已有word进程,避免反复开关进程造成运行缓慢
set objWord = getObject(,"word.application")
'无word进程则创建新进程
if objWord="" then
set objWord=createobject("word.application")
end if
objWord.Visible=False
set docfile=objWord.Documents.Open(arg)
str=docfile.Content
docfile.Close
set args=wscript.arguments
arg=args(0)
num=args(1)
if arg=0 then
msgbox "word文件库未建立无法检索!",16
else
if arg=1 then
msgbox "当前目录未找到word文件!",16
else
if arg=2 then
msgbox "关闭已打开的word文件,点确定开始",0
else
if arg=3 then
msgbox "文件库写入了 " & num & " 个文件",0
else
set objFSO = CreateObject("Scripting.FileSystemObject")
set txtFile = objFSO.OpenTextFile("temp", 1)
if txtFile.atendofstream then
msgbox "未检索到关键词!",16
else
str=txtFile.readAll
msgbox str
msgbox "全部结果保存在“检索结果.txt”"
end if
txtfile.close
end if
end if
end if
end if