MATLAB Answers

auto run and delete files from multiple subfolders

25 views (last 30 days)
My question consist over three parts
Part 1
My current directory is
E:\meta data\new\data _thesis\NCEP\winds_speed
In winds_speed , there are 30 folders (for year) and inside of each of these 30 folders there are further 12 folders are present.
Inside each of 12 folders, there is sorted_lat_split_NCAR_NCEP.m which i want to run automatically?
Part 2:
In this directory
E:\meta data\new\data _thesis\NCEP\winds_speed
there are 30 folders and inside of each of these 30 folders there are 12 folders and inside of each 12 folder, there is a runmyfile.m file which i want to delete from each folder?
Part 3
In this directory
E:\meta data\new\data _thesis\NCEP\winds_speed
there are 30 folders and inside of each of these 30 folders there are 12 folders and inside of each 12 folder, there is a abc.txt file inside of each of 12 folder. I want to paste each abc.txt file from this folder to my current directory
I am on window 7 and using matlab 2013, how can this do??

  7 Comments

Show 4 older comments
per isakson
per isakson on 15 Apr 2016
  • "24 text files name with Output_00.txt to Output_23.txt" &nbsp The script, sorted_lat_split_NCAR_NCEP.m, which you uploaded creates 17 files named. output_0.txt, ..., output_16.txt. Note the single 0 in output_0.txt.
for x = 0 : 16
....
fId = fopen( sprintf( 'Output_%d.txt', x), 'w' ) ;
fprintf( fId, ' \t \t \r\n' ) ; % first line
fprintf( fId, '%.3f\t%.3f\t%.3f\r\n', data(:) ) ;
fclose( fId ) ;
end
  • "gather output_00.txt of apr folder in each year (1985 to 2015)" &nbsp What do you exactly mean by gather? For instance, shall the the first line of each file be included? (Or is gather the name of the script, to be constructed?)
  • What about the other output-files and the other months?
  • The output-files don't contain any information on date and time
Muhammad Usman  Saleem
Muhammad Usman Saleem on 15 Apr 2016
  • I appologise about miss confusion. yes there are 17 text files with name Output_00.txt to Output_16.txt and my code just extract these files from orginal one.
  • Not it i mean name of the script, to be constructed and first line of each file be included here are the meaning of Gather(what i want to explain
I am at this folder
E:\meta data\new\data _thesis\NCEP\winds_speed
In this folder there are 30 folder with name 1985 to 2015.
Alright..
Now in 1985 folder , address of the directory will be
E:\meta data\new\data _thesis\NCEP\winds_speed\1985
In this this folder of 1985, there are 12 more folder with name of apr, aug,...sep (For year 1985 , monthly temperature data in folder)
Lets move ahead or not?
Now after click on apr folder, address is
E:\meta data\new\data _thesis\NCEP\winds_speed\1985\apr
(Here we have auto run our mat file. Alright!)
Now here will be 17 text files name i mentioned above.
Below lines, will understandable if you understand my problem well.
With your previous code, we have run all month .m code from 1985 to 2015 in each month.
I want to copy(move) output_00.txt file of each month of each year into a folder with name Output_00.txt. So that when i open this folder, instead of moving in batch, i find all output_00.txt file in this folder?
I hope , i explain my best. If no, let me know please.
Thanks
per isakson
per isakson on 15 Apr 2016
  • "with name Output_00.txt" &nbsp Did you change the script, , sorted_lat_split_NCAR_NCEP.m, to output double zero, 00?
  • "my code just extracts these files from original one" &nbsp the word, "original one", is new to me. Does "extracts" refer to the operation of the script, sorted_lat_split_NCAR_NCEP.m?
  • I don't know what "auto run mat file" means and I failed to find it in the Matlab documentation. (I very recently updated from R2013a.)
  • I wasn't aware that you use mat-files to solve this task
I believe, I understand the folder tree you use for this task.
E:\meta data\new\data _thesis\NCEP\winds_speed
1985
jan
...
dec
...
2015
jan
...
dec
"I want to copy(move) output_00.txt file of each month of each year into a folder with name Output_00.txt."
  • New sub-folders will be added to the root.
  • However, I don't understand the copy part, since one folder can only contain one file named, output_00.txt.
E:\meta data\new\data _thesis\NCEP\winds_speed
Output_00.txt
...
Output_16.txt
"So that when i open this folder, instead of moving in batch" &nbsp What is "moving in batch"?

Sign in to comment.

Accepted Answer

per isakson
per isakson on 12 Apr 2016
Edited: per isakson on 14 Apr 2016
I see two approaches.
Create cell arrays of strings containing the names of the sub-folders on the different levels. Use these cell arrays to loop over all folders. Use cd to make the folders the current folder and run the script. Pro: you have exact control over which folders you visit. Con: a lot of work.
Second approach: Use a function, which finds all files under one folder and which matches a given pattern. Search for rdir in the File Exchange or use cmd_where, which I attach. ( rdir is smarter, but I don't think you need that now.) cmd_where uses the dos-command where and thus it ignores the search path of Matlab.
sad_list = cmd_where( 'E:\meta data\new\data _thesis\NCEP\winds_speed' ...
, 'sorted_lat_split_NCAR_NCEP.m' );
%
for sad = sad_list
run( fullfile( sad.dir, sad.name ) )
end
Pro: little work. Con: cmd_where has problems with exotic characters in the filespec string; you might run scripts that you didn't intend to run; I haven't tested your case.
It might be a good idea to write a verbose report to a log-file. And add some assert.
Part 2.
sad_list = cmd_where( 'E:\meta data\new\data _thesis\NCEP\winds_speed' ...
, 'runmyfile.m' );
%
for sad = sad_list
delete( fullfile( sad.dir, sad.name ) )
end
Part 3. "paste each abc.txt file from this folder to my current directory" &nbsp I don't understand what you mean

  28 Comments

per isakson
per isakson on 15 Apr 2016
"*3rd part* of my question remains?" &nbsp Yes, but here your problems come second.
per isakson
per isakson on 15 Apr 2016
"which i [sic] have shared above" &nbsp Be more specific! Do you mean the forth comment under your question. If so, does that description overtake all older descriptions?
Muhammad Usman  Saleem
Muhammad Usman Saleem on 16 Apr 2016
I have done this task , thanks for your kind assistance. Please vote up my question, if you can
Usman

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 12 Apr 2016
Use genpath() to get a list of all files in all subfolders. See attached demo.

  4 Comments

Show 1 older comment
Image Analyst
Image Analyst on 12 Apr 2016
I don't know - it's not my code and you didn't give the complete error message so we don't know what line it's throwing the error on. Read this - it will help.
Muhammad Usman  Saleem
Muhammad Usman Saleem on 13 Apr 2016
Keeping in view , about question and continuity of my sub folders.
The code, i shared above only running one mat file. Not running remaining folder .m files,
Export for pressure ID 0 -> p=1000.0hpa.
Export for pressure ID 1 -> p=925.0hpa.
Export for pressure ID 2 -> p=850.0hpa.
Export for pressure ID 3 -> p=700.0hpa.
Export for pressure ID 4 -> p=600.0hpa.
Export for pressure ID 5 -> p=500.0hpa.
Export for pressure ID 6 -> p=400.0hpa.
Export for pressure ID 7 -> p=300.0hpa.
Export for pressure ID 8 -> p=250.0hpa.
Export for pressure ID 9 -> p=200.0hpa.
Export for pressure ID 10 -> p=150.0hpa.
Export for pressure ID 11 -> p=100.0hpa.
Export for pressure ID 12 -> p=70.0hpa.
Export for pressure ID 13 -> p=50.0hpa.
Export for pressure ID 14 -> p=30.0hpa.
Export for pressure ID 15 -> p=20.0hpa.
Export for pressure ID 16 -> p=10.0hpa.
These lines are indication of running of sorted_lat_split_NCAR_NCEP.m in only one folder.
Then this error appear
Cell contents reference from a non-cell array object.
You can edit this code also can help me with your kind code?
Muhammad Usman  Saleem
Muhammad Usman Saleem on 16 Apr 2016
I have done this task , thanks for your kind assistance. Please vote up my question, if you can
Usman

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!