编码中经常需要搜索文件夹中特定文件,文件可能在子文件夹中,这时候需要用到递归搜索,下面的函数可以实现这个功能,其中:
pic表包含身份证号、学号、班级列。照片用身份证号或学号命名。照片按班级分类存储。
n_dir_index用于区别递归中产生的数组名,防止冲突。
每进入1层,将ltLevel减1,直至小于lsLelel,调用时,lsLevel一般设置为1。
代码中有调整图片大小的函数resizeimage和转换格式的函数saveimage。
其中也包括一些全局变量参数,用于区别操作。
Function FindFile
Parameters lsLevel,ltLevel,lcPath
If ltLevel<lsLevel
Return
Endif
Local m_name
m_name="m"+Alltrim(Str(n_dir_index))
n_dir_index = n_dir_index+1
Local i,c_ext_name,c_file_name,s_image_file,c_image_bj,c_sfz,c_xh,c_tmp_file,p_tmp_file,c_image_file
Adir(&m_name,lcPath+"\*.*","D")
Select pic
If Type("&m_name")<>"U"
For i=1 To Alen(&m_name)/5
If &m_name(i,1)="." Or &m_name(i,1)=".."
Loop
Endif
If &m_name(i,5)="....D"
*子文件夹
FindFile(lsLevel,ltLevel-1,lcPath+"\"+&m_name(i,1))
Else
c_ext_name=Lower(Justext(&m_name(i,1)))
If Empty(c_ext_name)
Loop
Endif
c_file_name=Lower(Left(&m_name(i,1),Len(&m_name(i,1))-Len(c_ext_name)-1)) && 身份证号或学号
If c_ext_name<>"jpg" And c_ext_name<>"gif" And c_ext_name<>"png"
Loop
Endif
If n_image_type=1 And c_ext_name<>"jpg"
Loop
Endif
If n_image_type=2 And c_ext_name<>"gif"
Loop
Endif
If n_image_type=3 And c_ext_name<>"png"
Loop
Endif
s_image_file=lcPath+"\"+&m_name(i,1)
If File(s_image_file)
Do Case
Case n_source=1
Locate For Lower(Alltrim(身份证号))=c_file_name
Case n_source=2
Locate For Lower(Alltrim(学号))=c_file_name
Case n_source=3
Locate For Lower(Alltrim(身份证号))=c_file_name Or Lower(Alltrim(学号))=c_file_name
Endcase
If !Eof()
c_image_bj =Alltrim(班级)
Try
Md Testdir+"\Photo\"+c_image_bj
Catch
Endtry
c_sfz =Lower(Alltrim(身份证号))
c_image_file =Testdir+"\Photo\"+c_image_bj+"\"+c_sfz+".jpg" &&
c_xh =Lower(Alltrim(学号))
If n_replace =1 && 覆盖方式
Try
Delete File Testdir+"\Photo\"+c_image_bj+"\"+c_sfz+".jpg"
Catch
Endtry
Endif
If !File(c_image_file)
c_tmp_file=Getenv("TEMP")+"\"+c_file_name+".jpg"
p_tmp_file=Getenv("TEMP")+"\p_"+c_file_name+".jpg"
Try
Delete File "&p_tmp_file"
Catch
Endtry
If c_ext_name<>"jpg"
*转换格式
Try
Delete File "&c_tmp_file"
Catch
Endtry
=saveimage(s_image_file,c_tmp_file)
=resizeimage(c_tmp_file,p_tmp_file,system_photo_width,system_photo_height)
Try
Delete File "&c_tmp_file"
Catch
Endtry
Else
=resizeimage(s_image_file,p_tmp_file,system_photo_width,system_photo_height)
Endif
If File(p_tmp_file)
Copy File "&p_tmp_file" To "&c_image_file"
Endif
Try
Delete File "&p_tmp_file"
Catch
Endtry
n_count = n_count +1
Endif
Endif
Endif
Endif
Endfor
Endif
Endfunc