Python储存.bat文件中的数据到"collection"并更新数据库

2024-05-16 13:00:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个带有.bat文件的目录结构。这些.bat文件调用一个小应用程序,并根据参数值向它传递一些参数,从而生成一个通过电子邮件发送或保存的报告。你知道吗

我需要把这些报告和参数选项一起编目。有25种不同的可能参数。给定的.bat文件不会引用所有参数。你知道吗

我希望将有关.bat及其参数的数据存储在某种列表中,但丢失参数的可能性让我大吃一惊。我应该为每个可能的参数创建一个具有占位符的列表,并将其存储在列表列表中吗?你知道吗

还是有更好的办法?你知道吗

我已经计算出了我的脚本所需的大部分其他部分,它将如下流动。你知道吗

Loop through directory:
    if .bat then
        loop through lines
            store data in structure
insert results to sql server table

.bat文件如下所示:

@echo off
start C:\ProgramPath\SomeProgram.exe ^
/o=excel ^
/g=C:\SavePath  ^
/s=spStoredProcToCall ^
/a=InitialCatalogName^
/c=ServerName ^
/f=outputFile.xls ^
/p=@param1~999=@param2~XYZ

我可以批量或一次插入一行结果。你知道吗


Tags: 文件数据目录应用程序列表参数选项报告
1条回答
网友
1楼 · 发布于 2024-05-16 13:00:48

我最终使用字典解决问题,但一次完全处理一个对象/字典,以避免需要一组字典。你知道吗

我的主要职能是:

def main():
    for root,dirs,files in os.walk("Y:\\"):
        for filename in files:
            if filename.endswith(".bat"):
                d={}
                initDict(d,root,filename)
                f = open(os.path.join(root,filename))
                for line in f.read().splitlines():
                    if line[:1]=='/':
                        fieldType = line[1:2]
                        fieldValue = line.rstrip(' ^')[3:]
                        buildDict(d,fieldType,fieldValue)
                strSQL = buildQuery(d)
                print (strSQL)
                conn = pyodbc.connect('DRIVER={SQL Server Native Client 11.0};SERVER=someServer;DATABASE=myDatabase;Trusted_Connection=yes')
                cur = conn.cursor()
                cur.execute(strSQL)
                cur.commit()

相关问题 更多 >