diff --git a/Delphi10/开发档案/replacepas.py b/Delphi10/开发档案/replacepas.py new file mode 100644 index 0000000..860fea3 --- /dev/null +++ b/Delphi10/开发档案/replacepas.py @@ -0,0 +1,81 @@ +import os +import logging + +# 设置日志记录 +logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') + +directory_path = r'D:\Dp10RepoV1\项目代码\D10rcDingchangsheng\B03基础物料仓库' +# 定义替换列表 +replacements = [('EndDate.DateTime', 'frmFrameDateSel1.EndDate.Date') + ,('enddate.DateTime', 'frmFrameDateSel1.EndDate.Date') + ,('Enddate.DateTime', 'frmFrameDateSel1.EndDate.Date') + ,('BegDate.DateTime', 'frmFrameDateSel1.BegDate.Date') + ,('begdate.DateTime', 'frmFrameDateSel1.BegDate.Date') + ,('begDate.SetFocus', 'toolbar1.SetFocus') + ,('BegDate.SetFocus', 'toolbar1.SetFocus') + ,(""" if ADOQueryMain.Active then + begin + SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2)); + SCreateCDS(ADOQueryMain, CDS_Main); + SInitCDSData(ADOQueryMain, CDS_Main); + end;""", """CDSDataFilter(ADOQueryMain, CDS_Main, Tv1, '{"FilterStr":"' + CommonFiltersByContainer(Panel1, ' {"EquTag": 1,"LikeTag": 2,"Fields": ""}') + '" }');""") + + + ]#,('TComboBox', 'TcxComboBox'),('TMemo', 'TcxMemo') + +def replace_in_file(file_path, replacements, encodings=['utf-8', 'gbk']): + temp_file_path = file_path + '.tmp' + try: + # 尝试使用提供的编码列表打开文件 + for encoding in encodings: + try: + with open(file_path, 'r', encoding=encoding) as infile, \ + open(temp_file_path, 'w', encoding=encoding) as outfile: + # 读取整个文件内容到一个字符串中 + file_content = infile.read() + # 对文件内容进行所有替换操作 + for old, new in replacements: + + file_content = file_content.replace(old, new) + # 将替换后的内容写回临时文件 + outfile.write(file_content) + # 使用os.replace原子性地替换原文件 + os.replace(temp_file_path, file_path) + logging.info(f"Successfully processed file: {file_path} using encoding {encoding}") + break # 成功处理后跳出循环 + except UnicodeDecodeError: + # 如果当前编码失败,则尝试下一个编码 + logging.warning(f"Failed to decode file {file_path} using encoding {encoding}") + else: + # 如果所有编码都失败,则记录错误并删除临时文件(如果存在) + logging.error(f"Failed to process file {file_path} with all provided encodings") + if os.path.exists(temp_file_path): + os.remove(temp_file_path) + except Exception as e: + logging.error(f"Error processing file {file_path}: {e}") + if os.path.exists(temp_file_path): + os.remove(temp_file_path) + +def convert_to_unicode(replacements): + unicode_replacements = [] + for original, replacement in replacements: + # 将每个字符转换为Unicode编码 + original_unicode = ''.join(f'#{ord(char)}' for char in original) + replacement_unicode = ''.join(f'#{ord(char)}' for char in replacement) + unicode_replacements.append((original_unicode, replacement_unicode)) + return unicode_replacements + +def replace_in_folder(folder_path, replacements, encodings=['utf-8', 'gbk']): + for root, dirs, files in os.walk(folder_path): + for file_name in files: + if file_name.endswith('.pas'): + file_path = os.path.join(root, file_name) + replace_in_file(file_path, replacements, encodings) + + +# 转换编码执行替换操作 +# replace_in_folder(directory_path, convert_to_unicode(replacements) ) + + +# 执行替换操作 +replace_in_folder(directory_path, replacements ) \ No newline at end of file