LINUX.ORG.RU

Some manipulations with files and folders. (loop, find, create and rm)


0

2

Hello!

I need to realize such task.

1. In my user's home dir I have folder1;
2. In folder1 I have some (various count) subfolders with random names;
3. In these subfolders I have one file anyname.pdf (various name in each subfolder) and file content.txt (constant name in each subfolder)
## If in subfolder more than one .pdf or more than one .txt file, I must miss this subfolder (move it to Folder3 - it's one of the folders of my user's home dir) ;

So,
4. I must to scan every subfolders in folder1,
enter in each one;
5. If this subfolder has more than one .pdf file or more than one .txt file, I must go to next subfolder (with moving this subfolder to Folder3);
6. In «good» subfolders I must take file content.txt;
7. It has such structure:

dfdf{some trash}wqwq
begin_of_useful_info
info info ... info
end_of_useful_info
dfdf{some trash}wqwq

8. In this file content.txt I must find and cut only «useful_info» (between begin_of_useful_info and end_of_useful_info,including it):

begin_of_useful_info
info info ... info
end_of_useful_info

( begin_of_useful_info and end_of_useful_info is key-words on each content.txt, it must be cutted too)

9. this info I must put in new .txt file with name as .pdf file in this subfolder!

Example:
In folder1/4323353/ I have files GHTY34.pdf and content.txt. So, after operation I must get a file GHTY34.txt with useful_info)

10. delete parsed file content.txt
(so, now I have again two files in subfolder somename.pdf and somename.txt)

11. then I must copy files .pdf and new .txt with similar names somewhere, for example in folder2 (my user's home dir) and delete analized subfolder.
12. Go to next subfolder.

That's all
Thanks!

This will be cost one hundred US dollars

Jetty ★★★★★
()

В топике готовый текст программы, осталось только интерпретатор написать

unC0Rr ★★★★★
()

You can use Java, and google for walking through directory structure.

For example: http://download.oracle.com/javase/tutorial/essential/io/walk.html

Also you can use existing solutions such as DirectoryWalker class of the Apache Commons IO project (http://commons.apache.org/io/api-release/org/apache/commons/io/DirectoryWalke...)

Next, you can modify your files using java built-in methods (google about «how to read and write text files in java».

Next, you can remove and copy files by hands or by using Apache Commons api.

Regards

stevejobs ★★★★☆
()

Don't listen to fake steve, optik77. He is brain damaged. java, oh god *puke*

#!/bin/sh

find ~/folder1 -maxdepth 0 -type d | while read folder; do
  if [ 1 -ne $(ls "$folder/*.pdf" |wc -l) ] ||
     [ 1 -ne $(ls "$folder/*.txt" |wc -l) ] ||
     [ ! -f "$folder/content.txt" ] ; then
     mv "$folder" ~/folder3/
     continue
  fi
  BNAME=$(basename "$folder/*.pdf" .pdf)
  FNAME="$folder/$BNAME.txt"
  awk 'BEGIN { found=0 }
/^begin_of_useful_info$/ {found=1}
/^end_of_useful_info$/ {found=0;print $0}
{if (found)print $0}
' "$folder/content.txt" > $FNAME
  mv "$folder/$BNAME.txt" "$folder/$BNAME.pdf" ~/folder2/
  rm -rf "$folder/"
done

Didn't tested this code because of windows. I'll check myself when I return home to my computer with a real operating system.

PS wtf this shit I cannot reply in Development anonymously anymore

PPS obviously you aren't a real English-speaker and question is purely synthetic but I don't care lol

PPPS thanks to opennet for man test and man find.

legolegs ★★★★★
()
Ответ на: комментарий от stevejobs

Java is bad choice. It is not native and need additional installed software. Use Python with additional frameworks is better idea.

Also you can write your own Perl module for same tasks.

In that way your problem will be resolved forever.

--
wbr,
Chief of galaxy NT problems department
Syava Izgoldovish

Jetty ★★★★★
()

«Inglesh is spocken hier» (ц)

Xenesz ★★★★
()
Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.