search sepecific file and copy to target file

2 ビュー (過去 30 日間)
wenchao zhang
wenchao zhang 2023 年 11 月 4 日
コメント済み: Walter Roberson 2023 年 11 月 5 日
i want search the file (which are source file .c and head file .h) under file test20231104,and copy them to new file, how to realize.
i have try, but following issue happen

採用された回答

Walter Roberson
Walter Roberson 2023 年 11 月 4 日
file1 = char({dirOutput1.name}');
That creates a char array -- with shorter individual entries padded with spaces out to the length of the longest entry
movefile(file1(i),'collect')
Remember that file1 is a char array. file1(i) is a single character from that array, with the indexing being in linear order. So for i = 1, you would get file1(1,1), for i = 2 you would get file1(2,1) and so on.
You are unlikely to have files whos name happen to be single characters.
And if you did... the place you asked to look for the files was anywhere underneath the directory that the source code for the script is in, and with the way you do that, it is highly unlikely that you would end up with the directory being referred to being the current directory . When you asked to movefile(file1(i),'collect') you are asking to look for the file in the current directory.
There are a bunch of things wrong with your code, but I do not feel like typing it all in; you should post your code as text not just as picture.
  13 件のコメント
Walter Roberson
Walter Roberson 2023 年 11 月 5 日
Suppose that you already have a folder named collect . Then your **\*.c is going to look for .c files in all subfolders of the main folder, including inside collect . The logic about dirAlreadyCollected is that the code looks inside collect and removes from the list all files that match completely.
For example, before you might have
beach\frog.c
beach\jump.h
collect\frog.c
collect\shell.h
tide\frog.c
and because collect is a subfolder, your **\*.c and **\*.h is going to include all of those entries in fullfiles. Then the dirAlreadyCollected would look specifically in collect and would find there collect\frog.c and collect\shell.h . Taking the setdiff() would leave beach\frog.c and beach\jump.h and both of those would be moved into collect. The effect of the code is to prevent the code from trying to movefile collect\frog.c to the same destination collect\frog.c . No movefile('collect\frog.c', 'collect\frog.c') will be done -- but anything outside of collect would be moved. For example beach\frog.c would be moved even though there is a matching filename frog.c inside collect -- the code is not preventing the move to try to avoid overwriting the existing collect\frog.c . The code is just preventing moving collect\frog.c to collect\frog.c -- besides it being inefficient to try to move a file to itself, sometimes movefile can give an error if the source and destination are the same location.
In particular, the code is going to move beach\frog.c to collect\frog.c and then will later move tide\frog.c over-top of the collect\frog.c that was already created. So the beach\frog.c will have gotten moved and then that moved file will get overwritten.
The code is always moving the file to directly under collect . The code is not going to transfer the subfolder. For example, the movefile of beach\frog.c to collect is not going to create create\beach\frog.c
You are likely going to lose files because of this. Your algorithm made no effort to prevent files from being overwritten, and your algorithm moves files instead of copying them, so if anything goes wrong during the operation, the file has been moved already and no longer exists to try again.
Walter Roberson
Walter Roberson 2023 年 11 月 5 日
If this were MacOS or Linux, I would probably suggest quite different code, probably using the operating system find utility with output piped to a cpio command https://www.geeksforgeeks.org/cpio-command-in-linux-with-examples/
If the files were to be copied instead of moved, then on Windows I would probably consider system() of a windows dos xcopy command https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/xcopy

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSearch Path についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by