Naming a file based on the folder name

조회 수: 9 (최근 30일)
John
John 2014년 8월 7일
댓글: John 2014년 8월 14일
Hi,
So I have a script that can take all the images in a folder and stitch them together into an AVI movie file. It works great. Here's where I need some help.
The script tells ML to start in a root directory, it will look at all folders and subfolders, start in the first one, enter the subfolder, and take the images and create the AVI. Then it goes back out and enters any other subfolders and does the same, then goes back out to the root directory and goes to the next folder, and so on down the line until it runs out of folders to use.
The only thing I would like to do is to have MATLAB name the new AVI file this way: FolderName_SubFolderName_AVI
Any tips are appreciated.
  댓글 수: 2
Azzi Abdelmalek
Azzi Abdelmalek 2014년 8월 7일
I don't understand what you've already done and what your are asking for.
John
John 2014년 8월 7일
What I've done:
The ML code will go to a root directory. Ex: C:\Stuff
Within this directory, there are many folders. Ex: C:\Stuff\Folder1 through C:\Stuff\Folder 500
Within each folder there are several subfolders. Ex: C:\Stuff\Folder1\Sub1 and Sub2 and Sub3
The code takes all image files in each folder (the images are still frames of a video) and stitches them back into a video file (AVI).
What I'd like to do:
The code works great, but all the video files are named something generic. I'd like to name each new video file based on the folder name. For example, I would like the files to be named this way:
For the file in C:\Stuff\Folder1\Sub1, I would like the AVI to be named "Folder1_Sub1_AVI"

댓글을 달려면 로그인하십시오.

답변 (1개)

Image Analyst
Image Analyst 2014년 8월 7일
Try this:
folder = 'd:\all_tests\test1'; % Sample folder name.
[parentFolder, deepestFolder, ~] = fileparts(folder)
[parentFolder, folder1, ~] = fileparts(parentFolder)
baseFileName = sprintf('%s_%s_AVI.avi', folder1, deepestFolder)
fullFileName = fullfile(folder, baseFileName)
In command window:
parentFolder =
d:\all_tests
deepestFolder =
test1
parentFolder =
d:\
folder1 =
all_tests
baseFileName =
all_tests_test1_AVI.avi
fullFileName =
d:\all_tests\test1\all_tests_test1_AVI.avi
Also, see my demo, attached, where I recurse into all subfolders.
  댓글 수: 4
John
John 2014년 8월 8일
I will try sometime soon and will let you know then. As always with your answers, I'm positive it will work just fine. I sent you a PM.
John
John 2014년 8월 14일
Image Analyst, I replied to an older post you made with a question regarding that thread.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by