通过循环运行LibreOffice命令行脚本

时间:2017-07-09 01:37:46

标签: loops command-line libreoffice

我已经能够在单个文件上成功运行以下代码,但我想找到一种方法让它运行,以便循环遍历目录中显示的所有文件。我相信在CMD.exe中可以循环,但我没有任何成功。任何帮助将不胜感激。

"C:\Program Files (x86)\LibreOffice 5\program\scalc.exe" --convert-to DBF --infilter="csv:44,34,76,1,1/2/2/2/3/2/4/2/5/2/6/2/7/2/8/2/9/2/10/2/11/2/12/2/13/2/14/2/15/2/16/2/17/2/18/2/19/2/20/2/21/2/22/2/23/2/24/2/25/2/26/2/27/2/28/2/29/2/30/2/31/2/32/2/33/2/34/2/35/2/36/2/37/2/38/2/39/2/40/2/41/2/42/2/43/2/44/2/45/2/46/2/47/2/48/2/49/2/50/2/51/2/52/2/53/2/54/2/55/2/56/2/57/2/58/2/59/2/60/2/61/2/62/2/63/2/64/2/65/2/66/2/67/2/68/2/69/2/70/2/71/2/72/2/73/2/74/2/75/2/76/2/77/2/78/2/79/2/80/2/81/2/82/2/83/2/84/2/85/2/86/2/87/2/88/2/89/2/90/2/91/2/92/2/93/2/94/2/95/2/96/2/97/2/98/2/99/2/100/2/101/2/102/2/103/2/104/2/105/2/106/2/107/2/108/2/109/2/110/2/111/2/112/2/113/2/114/2/115/2/116/2/117/2/118/2/119/2/120/2/121/2/122/2/123/2/124/2/125/2/126/2/127/2/128/2/129/2/130/2/131/2/132/2/133/2/134/2/135/2/136/2,0,false,false" C:\Users\jdavidson\Desktop\DBFCONVERT\57826001_JQSAMPLE.csv --outdir C:\Users\jdavidson\desktop\complete

如果您对如何实现这一点有任何想法,那将是一个巨大的帮助。我正在努力将其构建为更大工作流程的一部分,并使其可重复。

更新

通过创建批处理文件并将其放在我将用于转换前进的目录中,我能够遍历(循环)遍历目录中的所有文件

for /r %%i in (*.csv) do "C:\Program Files (x86)\LibreOffice 5\program\scalc.exe" --convert-to DBF --infilter="csv:44,34,76,1,1/2/2/2/3/2/4/2/5/2/6/2/7/2/8/2/9/2/10/2/11/2/12/2/13/2/14/2/15/2/16/2/17/2/18/2/19/2/20/2/21/2/22/2/23/2/24/2/25/2/26/2/27/2/28/2/29/2/30/2/31/2/32/2/33/2/34/2/35/2/36/2/37/2/38/2/39/2/40/2/41/2/42/2/43/2/44/2/45/2/46/2/47/2/48/2/49/2/50/2/51/2/52/2/53/2/54/2/55/2/56/2/57/2/58/2/59/2/60/2/61/2/62/2/63/2/64/2/65/2/66/2/67/2/68/2/69/2/70/2/71/2/72/2/73/2/74/2/75/2/76/2/77/2/78/2/79/2/80/2/81/2/82/2/83/2/84/2/85/2/86/2/87/2/88/2/89/2/90/2/91/2/92/2/93/2/94/2/95/2/96/2/97/2/98/2/99/2/100/2/101/2/102/2/103/2/104/2/105/2/106/2/107/2/108/2/109/2/110/2/111/2/112/2/113/2/114/2/115/2/116/2/117/2/118/2/119/2/120/2/121/2/122/2/123/2/124/2/125/2/126/2/127/2/128/2/129/2/130/2/131/2/132/2/133/2/134/2/135/2/136/2,0,false,false" C:\Users\jdavidson\Desktop\DBFCONVERT\57826001_JQSAMPLE.csv --outdir C:\Users\jdavidson\desktop\complete %%i

请注意,我使用for循环来实现我想要的效果。

for /r %%i in ('list directory if command not being run in specific directory already; you can also but * for all files in directory or *.ext for the extension of only certain files) do 'add in command %%i

1 个答案:

答案 0 :(得分:0)

选项1:您只需执行一次Open / Libre Office就可以实际转换多个文档..没有循环。只需使用通配符,例如:

string name = "Jacob's Ladder";
string commandText = "SELECT Name, LicenseType, till FROM myTable WHERE Name = @Name";


using (SqlConnection connection = new SqlConnection(connectionString))
{
    SqlCommand findItForMe = new SqlCommand(commandText, connection);



    // Use AddWithValue to assign name
    // The parameterized query will escape your strings and keep you safe from hackers.
    command.Parameters.AddWithValue("@name", name);

    try
    {
        connection.Open();
        SqlDataReader dr = command.ExecuteReader();
        while (dr.Read())
        {
            // do something here
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}

选项2:我认为Windows对你可以通过这种方式传递的文件数量有限制..不确定是什么限制..但如果你超过了这个限制,你可以使用我写的这个python程序几天前这样做..作为我仍在努力的项目的一部分。在我的情况下,我正在将 "C:\Program Files (x86)\LibreOffice 5\program\scalc.exe" --convert-to DBF mydir\*.csv 转换为.doc,显然我的目录与您的目录不同。但是你可以很容易地根据自己的需要进行调整......我想做的一件事就是检查转换文件是否存在,如果存在则跳过它...这样如果有一些新文件添加它可以再次运行转换它们而不重做它们...

.odt应该设置为文件的位置,docpath应该设置为Calc exe的路径名。如果你按照我的方式保留它们,那就是程序放置的位置转换后的文件也是..并检查它们的存在。此外,我走在寻找输入文件的路径..所以,如果由于某种原因你将文件放在不同的子目录中,它会找到它们并将转换放在同一目录中。

soffice

只需安装Python27 ..并设置import os, sys, re, subprocess docpath = 'doc' soffice = 'C:\Program Files\LibreOffice 5\program\soffice.exe' convert = '"%s" --headless --convert-to odt --outdir "%s" "%s"' def plog(fmt = '', *args): sys.stdout.write(fmt % args) sys.stdout.flush() def log(fmt = '', *args): plog((fmt + '\n'), *args) def convert(): odtfiles = [] for subdir, dirs, files in os.walk(docpath): for file in files: if not file.endswith('.doc'): continue docfile = os.path.join(subdir, file) odtfile = re.sub(r"\.doc$", ".odt", docfile) plog("%s -> %s: " % (docfile, odtfile)) if not os.path.isfile(odtfile): plog('Converting.. ') subprocess.check_output(convert % (soffice, docpath, docfile), shell=True) log('OK.') odtfiles.append(odtfile) return odtfiles odtfiles = convert() 字符串即可获得正确的转换设置。除了一些其他变化应该非常简单..但如果您需要帮助,请在评论中提问。