On Mon, May 5, 2014 at 5:44 PM, Steinmetz, Paul <PSteinmetz@xxxxxxxxxx> wrote:
I'm looking for a solution to move all objects from lib A
to lib B in batch mode with a replace option if the object
already exists.
Well, I'm sure there are several ways to do this, but as I'm the
resident Python fanboy, I will present a simple three-line Python
script:
# begin script
import os
for name in os.listdir('/qsys.lib/libA.lib'):
os.rename('/qsys.lib/libA.lib/' + name, '/qsys.lib/libB.lib/' + name)
# end script
In this case, it's so short because the behavior of os.rename is to
automatically replace (assuming you have the necessary authority).
If you had wanted to only move objects that DON'T exist in lib B:
# begin script
import os
objectsB = os.listdir('/qsys.lib/libB.lib')
for name in os.listdir('/qsys.lib/libA.lib'):
if name not in objectsB:
os.rename('/qsys.lib/libA.lib/' + name, '/qsys.lib/libB.lib/' + name)
# end script
(The objectsB variable isn't strictly necessary, but getting the list
of objects once up front is more efficient than recalculating the list
every time within the loop, especially if there are lots of objects in
lib B.)
Finally, if you had wanted only newer objects to replace older ones:
# begin script
import os
libA = '/qsys.lib/libA.lib'
libB = '/qsys.lib/libB.lib'
objectsB = os.listdir(libB)
for name in os.listdir(libA):
mtimeA = os.path.getmtime(libA + '/' + name)
if name in objectsB:
mtimeB = os.path.getmtime(libB + '/' + name)
else:
mtimeB = 0
if mtimeA > mtimeB:
os.rename(libA + '/' + name, libB + '/' + name)
# end script
All these can be done on the i with iSeriesPython
(www.iseriespython.com), which is freely downloadable and open source.
And simple enough to install on the i that even I was able do it in
just a few minutes.
John Y.
As an Amazon Associate we earn from qualifying purchases.