简体   繁体   中英

TCL script to go through all folders in a directory and perform a function

File structure

File1
 test.pdb
 xyz.txt

File2
 test.pdb
 xyz.txt

File3
 test.pdb
 xyz.txt

I want to loop in through all folders in the directory and run the following code which is in textfile autopsf.tcl on Tk console:

package require autopsf
mol new test.pdb
autopsf -mol 0

package require solvate  
solvate test_autopsf.psf test_autopsf.pdb -t 5 -o solvate

package require autoionize
autoionize -psf solvate.psf -pdb solvate.pdb -neutralize

I am running the following code at the moment:

for d in ./*/ ; do
source autopsf.tcl
done

If you don't care about the order, you can do:

foreach dir [glob -type d *] {
    # safety check
    if {![file exists $dir/test.pdb]} continue 
    # code to do the work here; note that we have directories
}

You can probably factor out the package require calls. Well-designed packages can live together right, and putting them at the top is a useful way to make dependencies evident.

If you want the directories sorted, apply lsort to the output of glob . The default order from glob is whatever the OS gives us the directory entries in, and can depend on all sorts of things (including file ages and so on) so it should not be relied upon in code where a definite of processing matters.

This code worked for me:

foreach file [glob -nocomplain "./*/"] {
cd $file
source autopsf.tcl
cd ..
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM