利用IDL,完成给定情况的文件操作。本例中,主要使用 File_delete 命令
;程序背景:基于文件名,对不同文件夹下的同名文件进行识别,删除多余的文件
;程序编写于2017.9.29
;----------------------------------
save_name = 'F:\Temporary\test\Result.txt'
openw, lun_1,save_name, /get_lun
;获取A文件夹中文件名信息
A_path = 'F:\Temporary\test\A\'
cd, A_path
A_Files = FILE_Search(A_path + '*.*', count = A_Filecount)
If A_Filecount eq 0 Then Begin
Print, 'No Files Found in the A directory!'
RETURN
Endif
; print, file_basename(A_Files)
; printf, lun_1, A_path + ' total file number: '
; printf, lun_1, A_Filecount
;去掉后缀名
A = file_basename(A_Files)
For num = 0, A_Filecount - 1 Do begin
pos_num = strpos(A[num],'.') ; strpos 实现查找给定字符位置的功能
A[num] = strmid(A[num],0, pos_num)
Endfor
print, A
printf, lun_1, A_path + ' total file number: '
printf, lun_1, size(A, /N_ELEMENTS)
;----------------------------------
;获取B文件夹中文件名信息
B_path = 'F:\Temporary\test\B\'
cd, B_path
B_Files = FILE_Search(B_path + '*.*', count = B_Filecount)
If B_Filecount eq 0 Then Begin
Print, 'No Files Found in the B directory!'
RETURN
Endif
; print, file_basename(B_Files)
; printf, lun_1, B_path + ' total file number: '
; printf, lun_1, B_Filecount
;去掉后缀名
B = file_basename(B_Files)
For num = 0, B_Filecount - 1 Do begin
pos_num = strpos(B[num],'.') ; strpos 实现查找给定字符位置的功能
B[num] = strmid(B[num],0, pos_num)
Endfor
print, B
printf, lun_1, B_path + ' total file number: '
printf, lun_1, size(B, /N_ELEMENTS)
;;;-----------------------------
;对A B文件夹中文件名比对识别
Delete_Count = 0
For i = 0, ((A_Filecount GE B_Filecount)? A_Filecount : B_Filecount) - 1 Do Begin
if A_Filecount GE B_Filecount then begin
Flag = 0 ;删除标识
for j = 0, B_Filecount - 1 do begin
if A[i] eq B[j] then begin
print, 'Filename matched'
Flag = 1
break
endif
endfor
;删除
if Flag ne 1 then begin
Print, 'Filename not match'
print, A[i] + ' will be deleted'
printf, lun_1, A[i] + ' has been deleted!'
;;---------------
;File_delete, A[i]
;;---------------
Delete_Count++
endif
endif
Endfor
printf, lun_1, 'Total deleted file number:'
printf, lun_1, Delete_Count
free_lun, lun_1
print, 'Done'