Java
Java

Reputation: 1292

Convert multiple files using command line with Python

I am trying to convert all SQB files into BAK files format in the same folder (called "Today").

I was able to come up with Python script to move necessary (same day's) SQB files from ("Test1" folder) into a separate folder ("Today" folder).

Now, I need to run a command line to go thru each SQB files and convert all to BAK file format.

There is a EXE file called "SQBConverter" that will be used, and the command line goes like this (for example, for an individual file):

SQBConverter "C:\Users\Justin Dolt\Today\LOG_us_bcan_multi_replica_2021042603.SQB" 
"C:\Users\Justin Dolt\Today\test.bak" "PasswordHere"

Here is Python script I have so far.

import os
from os import path

import datetime
import glob
import shutil

# Today's date recognition and concat
today = datetime.date.today()
today = str(today)
nohyphen_today = today.replace('-','')
revised_today = "LOG_us_bcan_multi_replica_" + nohyphen_today 

src = "C:\\Users\\Justin Dolt\\Test1"
dst = "C:\\Users\\Justin Dolt\\Today" 

# Move SQB files from "Test1" folder to "Today" folder
src_files = os.listdir(src)
for file_name in src_files:
full_file_name = os.path.join(src, file_name)
    if os.path.isfile(full_file_name):
        shutil.copy(full_file_name, dst)

# Not sure from here
for f in glob.glob("*.sqb"):
        SQBConverter....?   

enter image description here

What would be the code like from here (to go thru all SQB files in the same folder and convert all to BAK files)?

for f in glob.glob("*.sqb"):
        SQBConverter...? 

Added image later: enter image description here

Upvotes: 0

Views: 369

Answers (1)

Tim Roberts
Tim Roberts

Reputation: 54698

The easy way is:

for f in glob.glob('*.sqb'):
    os.system( f'SQBConverter "{f}" "{dst}/test.bak" {password}' )

If you need more sophisticated control, you can use subprocess.Popen, but this may be enough to get you started.

Update

You should have been able to figure out how to vary the backup file name. That's just string manipulation.

for f in glob.glob('*.sqb'):
    os.system( f'SQBConverter "{f}" "{dst}/{f[:-4]}.bak" {password}' )

Upvotes: 2

Related Questions