컴퓨터 엑셀 워드 포토샵 구글어스 WINDOWS JAVASCRIPT JAVA C++

 
Monday, October 30, 2006

펄,Perl] 파일 삭제, 디렉토리(폴더) 지우기 함수; Delete File Directory


펄에서는 unlink 함수로 파일을 지웁니다. 성공적으로 지운 파일의 개수를 반환합니다.

디렉토리는 rmdir 함수로 지웁니다. 실패했을 때에는 false 논리값을 반환합니다. 아래 예제의 경우, 디렉토리 삭제에 실패했을 때 메시지를 출력하기 위해 !rmdir 이렇게 앞에 느낌표를 붙여 논리값을 반전시켜 주었습니다.


파일/디렉토리 지우기 예제


#!/usr/bin/perl
use strict; use warnings;

  # 0.obj 라는 파일 지우기
  unlink "0.obj";


  my $delCount = unlink <*.bak>;     # .bak 확장자를 가진 백업파일 모두 지우기
  print $delCount, "개 파일 삭제\n"; # 파일을 몇 개 지웠는지 출력


  # fooo 란 이름의 디렉토리 삭제 (실패하면 메시지 출력)
  print "삭제 실패\n" if !rmdir "fooo";


빈 디렉토리만 삭제할 수 있습니다. 안에 파일이 있는 디렉토리는 우선 그 파일들을
unlink <fooo/*>;
이런 식으로 지워야 합니다.




다음 명령어로, perldoc 에서 함수에 대한 도움말을 볼 수 있습니다:

perldoc -f unlink

perldoc -f rmdir

D:\Z>perldoc -f unlink
  unlink LIST
  unlink  Deletes a list of files. Returns the number of files
      successfully deleted.

          $cnt = unlink 'a', 'b', 'c';
          unlink @goners;
          unlink <*.bak>;

      Note: "unlink" will not attempt to delete directories unless you
      are superuser and the -U flag is supplied to Perl. Even if these
      conditions are met, be warned that unlinking a directory can
      inflict damage on your filesystem. Finally, using "unlink" on
      directories is not supported on many operating systems. Use
      "rmdir" instead.

      If LIST is omitted, uses $_.


D:\Z>perldoc -f rmdir
  rmdir FILENAME
  rmdir   Deletes the directory specified by FILENAME if that directory is
      empty. If it succeeds it returns true, otherwise it returns
      false and sets $! (errno). If FILENAME is omitted, uses $_.


D:\Z>





tag: perl
Perl | 펄

0 Comments:

Post a Comment

<< Home RSS 2.0 feed

구글 Google 에서 제공하는 무료 블로그 서비스인 블로거 Blogger 의 인터넷 주소는 www.blogger.com 입니다. Blogger 에 블로그를 만들면, blogspot.com 이라는 주소에 블로그가 생성됩니다.
블로그를 직접 방문하지 않고도 최신 게시물을 구독하려면 RSS 2.0 feed 주소를 리더기에 등록하시면 됩니다.
Previous Posts
Monthly Archives
Top