How can I run all executable files that are located in series of subfolders?

24 ビュー (過去 30 日間)
GeoMSI
GeoMSI 2015 年 10 月 4 日
コメント済み: Wesser 2022 年 7 月 8 日
I have a hundreds of sub-folders (named in sequence like folder_1, folder_2 ...) that include executable file and all necessary input files in them. I just need to get in every sub-folders and run exe files, they don't even require typing in command window. So far, i got this code:
currentFolder = pwd;
for n=1:100
cd(sprintf('D:\analysis\folder_%i',n))
system('runner.exe &');
cd(currentFolder);
end
But i think this code only runs the exe in main folder(in my case D:\analysis) repeatedly, not the ones in sub-folders. How can i do it?

採用された回答

Walter Roberson
Walter Roberson 2015 年 10 月 4 日
projectdir = pwd; %or hard-code the name
folderinfo = dir(projectdir);
folderinfo(~[folderinfo.isdir]) = []; %discard the non-directories
folderinfo(ismember({folderinfo.name}, {'.', '..'})) = []; %get rid of . and ..
for K = 1 : length(folderinfo)
thisdir = fullfile( projectdir, folderinfo(K).name );
cmd = sprintf('cd "%s" & runner.exe &');
system(cmd);
end
  12 件のコメント
Stephen23
Stephen23 2015 年 10 月 6 日
I recommend using natsortfiles for sorting filenames with file extensions, rather than natsort. The file extension and separator character (period) can affect the sort order when using natsort. I created natsortfiles to remove this effect, so shorter filenames always sort first (like a dictionary, and like the most popular desktop OS).
Wesser
Wesser 2022 年 7 月 8 日
Hi Walter,
Same question many moons later...
I don't quite understand the code in your original reply to this question. Right now, I believe I'm only runing the .exe in the original directory file, but I'd like to run the copied .exe in each of the created path{i}. The .exe doesn't cooporate when it's not in the same directory as all the required files. Thank you for any help you may be able to provide with this!!
clear all;
clc;
%Solute transport parameters in row 47 of SELECTOR.IN file
SolTrans=[0.035 0 1 0.0479 0 0 0 0 273895 1 0 0 0 0]; %[Kd Nu Beta Henry SnkL1 SnkS1 SnkG1 SnkL1p SnkS1p SnkG1p SnkL10 SnkS10 SnkG10 Alfa] !! UPDATE NUMBERS IN BRACKETS !!
%~~~~~~
%ADD NOISE TO SOLUTE TRANSPORT PARAMETERS
num_sim=100; %100 Monte Carlo Simulations
Kd=SolTrans(1)+.02*rand(1,num_sim); %a vector with 100 values of Kd parameters. We assume that the error is normally distributed with a standard deviation of ##.
Nu=SolTrans(2)+.02*rand(1,num_sim);
Beta=SolTrans(3)+.02*rand(1,num_sim); %!!! CHANGE .02 FOR ALL THESE TO APPROPRIATE STANDARD DEVIATIONSN !!!
Henry=SolTrans(4)+.02*rand(1,num_sim);
SnkL1=SolTrans(5);
SnkS1=SolTrans(6);
SnkG1=SolTrans(7);
SnkL1p=SolTrans(8);
SnkS1p=SolTrans(9)+.02*rand(1,num_sim); %!!! FOR AWI MODIFIED MODEL - KL - LANGMUIR EXPONENT FOR AWI FOR LANGMUIR SORPTION or 0 FOR FREUNDLICH SORPTION
SnkG1p=SolTrans(10);
SnkL10=SolTrans(11);
SnkS10=SolTrans(12);
SnkG10=SolTrans(13);
Alfa=SolTrans(14);
%~~~~~~
%INITIALIZE FILES AND DIRECTORIES
hydrus_exec='C:\Users\jessi\Desktop\Hydrus\Projects\ATSDR\AFFF1PFOS\H1D_CALC.exe'; %'C:\Program Files (x86)\PC-Progress\Hydrus-1D 4\H1D_calc.exe';
hydrus_ref='C:\Users\jessi\Desktop\Hydrus\Projects\ATSDR\AFFF1PFOS';
profileDAT='C:\Users\jessi\Desktop\Hydrus\Projects\ATSDR\AFFF1PFOS\PROFILE.DAT';
options='C:\Users\jessi\Desktop\Hydrus\Projects\ATSDR\AFFF1PFOS\OPTIONS.IN';
atmosph='C:\Users\jessi\Desktop\Hydrus\Projects\ATSDR\AFFF1PFOS\ATMOSPH.IN';
selectorIN='C:\Users\jessi\Desktop\Hydrus\Projects\ATSDR\AFFF1PFOS\SELECTOR.IN';
level='C:\Users\jessi\Desktop\Hydrus\Projects\ATSDR\AFFF1PFOS\LEVEL_01.dir';
work_dir='C:\Users\jessi\Desktop\Simulations\';
mkdir(work_dir);
%~~~~~~
%CREATE DIRECTORIES AND INPUT FILES - This block is doing the work. Creates the input files for all realizations. Copies the mandatory file Profile.dat to
% the corresponding folder and creates the corresponding Selector
% .in. The Selector.in file is different for each realization since the solute transport parameters is
% variable. The code reads the reference Selector.in, copies the first 46 lines to the new file, write the van Genuchten parameters to the 47th line,
% and finally copies the last 10 lines from the reference Selector.in to the new file.
path=cell(1,num_sim);
for i=1:num_sim
path{i}=strcat(work_dir,'run_',num2str(i)); %create folder for each run
mkdir(path{i});
copyfile(profileDAT,path{i}); %copy profileDAt from reference directory to the simulation directory
copyfile(options,path{i}); %copy options from reference directory to the simulation directory
copyfile(atmosph,path{i}); %copy atmosph from reference directory to the simulation director
copyfile(hydrus_exec,path{i}); %copy options from reference directory to the simulation directory
%create a new level_01.dir in each subdirectory with associated file
%directory path
fid_level = fopen(fullfile(path{i},'LEVEL_01.dir'),'w');
fprintf(fid_level,'%s',path{i});
fclose(fid_level);
fileID_out=fopen(strcat(path{i},'\selector.in'),'wt'); %manipulate Selector.in for each run
% "wt"= permision for file axis type
% w=Open or create new file for writing. Discard existing contents, if any.
% To open files in text mode, attach the letter 't' to the permission argument,
fileID_in=fopen(selectorIN);
skip_lines=46; %!!! THIS IS THE LINE THAT CHANGES IN SELECTOR.IN FILE , i.e. solute transport parameters!!!
for k=1:(skip_lines)
x=fgetl(fileID_in); %x = fgetl(fileID)= returns the next line of the specified file, removing the newline characters.
fprintf(fileID_out,'%s\n',x); % %s in the formatSpec input indicates that the values of the variables url and sitename, should be printed as text.
% '\n' as a newline indicator.
% "x" = prints the values from variable x
end
out_Sol=SolTrans; % Renaming...
%Whatever the solute transport parameter is for that monte carlo run (i)
out_Sol(1)=Kd(i); % Solid phase sorption Kd
out_Sol(2)=Nu(i); % Van Genuchten parameter
out_Sol(3)=Beta(i); % Van Genuchten parameter
out_Sol(4)=Henry(i); % Kh = KL*Rmax
out_Sol(9)=SnkS1p(i); % KL - LANGMUIR EXPONENT FOR AWI FOR LANGMUIR SORPTION or 0 FOR FREUNDLICH SORPTION
fprintf(fileID_out,'%f %f %f %f %f %f %f %f %f %f %f %f %f %f\n',out_Sol');
% %f = Fixed-point notation (Use a precision operator to specify the number of digits after the decimal point.)
% '\n' as a newline indicator.
fgetl(fileID_in); % returns the next line of the specified file, removing the newline characters.
skip_lines_end=5; % CORRECT NUMBER??? DOUBLE CHECK
for k=1:(skip_lines_end)
x=fgetl(fileID_in);
fprintf(fileID_out,'%s\n',x);
end
fclose('all');
end
% THE SECTOIN THATIS WONKY AND NEEDS HELP!
path=cell(1,num_sim);
for i=1:num_sim
exec_path=['"' hydrus_exec '" "' path{i} '"'];
[x, y]= dos(exec_path);
if x % unsuccessful
error('exe failed'); % or take some other action besides throwing an error
end
end

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2015 年 10 月 4 日
If your folders are more than one level deep, you can use genpath(), like in my attached example.

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by