Delete a file: Difference between revisions

From Rosetta Code
Content added Content deleted
(Ada solution added)
No edit summary
Line 4: Line 4:


=={{header|Ada}}==
=={{header|Ada}}==
<ada>
<lang ada>
with Ada.Directories; use Ada.Directories;
with Ada.Directories; use Ada.Directories;
</ada>
</lang>
and then
and then
<ada>
<lang ada>
Delete_File ("input.txt");
Delete_File ("input.txt");
Delete_File ("/input.txt");
Delete_File ("/input.txt");
Delete_Tree ("docs");
Delete_Tree ("docs");
Delete_Tree ("/docs");
Delete_Tree ("/docs");
</ada>
</lang>
Naming conventions for the file path are [[OS]]-specific. The language does not specify the encoding of the file paths, the directory separators or brackets, the file extension delimiter, the file version delimiter and syntax. The example provided works under [[Linux]] and [[Windows]].
Naming conventions for the file path are [[OS]]-specific. The language does not specify the encoding of the file paths, the directory separators or brackets, the file extension delimiter, the file version delimiter and syntax. The example provided works under [[Linux]] and [[Windows]].
=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
Note: <code>scratch</code> does not appear to do anything on [[ALGOL 68G]]. Also note that file names are Operating System dependent.
Note: <tt>scratch</tt> does not appear to do anything on [[ALGOL 68G]]. Also note that file names are Operating System dependent.
<pre>
<pre>
main:(
main:(
Line 39: Line 39:
=={{header|C}}==
=={{header|C}}==
ISO C:
ISO C:
<c>#include <stdio.h>
<lang c>#include <stdio.h>


int main() {
int main() {
Line 47: Line 47:
remove("/docs");
remove("/docs");
return 0;
return 0;
}</c>
}</lang>


POSIX:
POSIX:
<c>#include <unistd.h>
<lang c>#include <unistd.h>


int main() {
int main() {
Line 58: Line 58:
rmdir("/docs");
rmdir("/docs");
return 0;
return 0;
}</c>
}</lang>


=={{header|D}}==
=={{header|D}}==
{{libheader|Tango}}
{{libheader|Tango}}
<d>import tango.io.Path;
<lang d>import tango.io.Path;


void main() {
void main() {
Line 69: Line 69:
remove("docs");
remove("docs");
remove("/docs");
remove("/docs");
}</d>
}</lang>


{{libheader|Tango}}
{{libheader|Tango}}
POSIX:
POSIX:
<d>import tango.stdc.posix.unistd;
<lang d>import tango.stdc.posix.unistd;


void main() {
void main() {
Line 80: Line 80:
rmdir("docs");
rmdir("docs");
rmdir("/docs");
rmdir("/docs");
}</d>
}</lang>


=={{header|Forth}}==
=={{header|Forth}}==
Line 115: Line 115:
=={{header|Java}}==
=={{header|Java}}==


<java>import java.util.File;
<lang java>import java.util.File;
public class FileDeleteTest {
public class FileDeleteTest {
public static boolean deleteFile(String filename) {
public static boolean deleteFile(String filename) {
Line 132: Line 132:
test("directory", File.seperator + "docs" + File.seperator);
test("directory", File.seperator + "docs" + File.seperator);
}
}
}</java>
}</lang>


=={{header|MAXScript}}==
=={{header|MAXScript}}==
Line 143: Line 143:
=={{header|OCaml}}==
=={{header|OCaml}}==


<ocaml>Sys.remove "input.txt";;
<lang ocaml>Sys.remove "input.txt";;
Sys.remove "/input.txt";;</ocaml>
Sys.remove "/input.txt";;</lang>


with the Unix library:
with the Unix library:
<ocaml>#load "unix.cma";;
<lang ocaml>#load "unix.cma";;
Unix.unlink "input.txt";;
Unix.unlink "input.txt";;
Unix.unlink "/input.txt";;
Unix.unlink "/input.txt";;
Unix.rmdir "docs";;
Unix.rmdir "docs";;
Unix.rmdir "/docs";;</ocaml>
Unix.rmdir "/docs";;</lang>


=={{header|Perl}}==
=={{header|Perl}}==


<perl>use File::Spec::Functions qw(catfile rootdir);
<lang perl>use File::Spec::Functions qw(catfile rootdir);
# here
# here
unlink 'input.txt';
unlink 'input.txt';
Line 161: Line 161:
# root dir
# root dir
unlink catfile rootdir, 'input.txt';
unlink catfile rootdir, 'input.txt';
rmdir catfile rootdir, 'docs';</perl>
rmdir catfile rootdir, 'docs';</lang>


'''Without Perl Modules'''
'''Without Perl Modules'''
Line 183: Line 183:
=={{header|Python}}==
=={{header|Python}}==


<python>import os
<lang python>import os
# current directory
# current directory
os.remove("output.txt")
os.remove("output.txt")
Line 189: Line 189:
# root directory
# root directory
os.remove("/output.txt")
os.remove("/output.txt")
os.rmdir("/docs")</python>
os.rmdir("/docs")</lang>


=={{header|Raven}}==
=={{header|Raven}}==

Revision as of 15:32, 3 February 2009

Task
Delete a file
You are encouraged to solve this task according to the task description, using any language you may know.

In this task, the job is to delete a file called "input.txt" and delete a directory called "docs". This should be done twice: once "here", i.e. in the current working directory and once in the filesystem root.

Ada

<lang ada> with Ada.Directories; use Ada.Directories; </lang> and then <lang ada> Delete_File ("input.txt"); Delete_File ("/input.txt"); Delete_Tree ("docs"); Delete_Tree ("/docs"); </lang> Naming conventions for the file path are OS-specific. The language does not specify the encoding of the file paths, the directory separators or brackets, the file extension delimiter, the file version delimiter and syntax. The example provided works under Linux and Windows.

ALGOL 68

Note: scratch does not appear to do anything on ALGOL 68G. Also note that file names are Operating System dependent.

main:(
  PROC remove = (STRING file name)INT: 
  BEGIN
    FILE actual file;
    INT errno = open(actual file, file name, stand back channel);
    IF errno NE 0 THEN stop remove FI;
    scratch(actual file); # detach the book and burn it #
    errno
  EXIT
  stop remove:
      errno
  END;
  remove("input.txt");
  remove("/input.txt");
  remove("docs");
  remove("/docs")
)

C

ISO C: <lang c>#include <stdio.h>

int main() {

 remove("input.txt");
 remove("/input.txt");
 remove("docs");
 remove("/docs");
 return 0;

}</lang>

POSIX: <lang c>#include <unistd.h>

int main() {

 unlink("input.txt");
 unlink("/input.txt");
 rmdir("docs");
 rmdir("/docs");
 return 0;

}</lang>

D

Library: Tango

<lang d>import tango.io.Path;

void main() {

   remove("input.txt");
   remove("/input.txt");
   remove("docs");
   remove("/docs");

}</lang>

Library: Tango

POSIX: <lang d>import tango.stdc.posix.unistd;

void main() {

 unlink("input.txt");
 unlink("/input.txt");
 rmdir("docs");
 rmdir("/docs");

}</lang>

Forth

There is no means to delete directories in ANS Forth.

 s" input.txt" delete-file throw
s" /input.txt" delete-file throw

Fortran

Works with: Fortran version 90 and later

I don't know a way of deleting directories in Fortran

OPEN (UNIT=5, FILE="input.txt", STATUS="OLD")   ! Current directory
CLOSE (UNIT=5, STATUS="DELETE")
OPEN (UNIT=5, FILE="/input.txt", STATUS="OLD")  ! Root directory
CLOSE (UNIT=5, STATUS="DELETE")

Haskell

import System.IO
import System.Directory

main = do
  removeFile "output.txt"
  removeDirectory "docs"
  removeFile "/output.txt"
  removeDirectory "/docs"

Io

Directory fileNamed("input.txt") remove
Directory directoryNamed("docs") remove
RootDir := Directory clone setPath("/")
RootDir fileNamed("input.txt") remove
RootDir directoryNamed("docs") remove

Java

<lang java>import java.util.File; public class FileDeleteTest {

  public static boolean deleteFile(String filename) {
      boolean exists = new File(filename).delete();
      return exists;
  }
  public static void test(String type, String filename) {
      System.out.println("The following " + type + " called " + filename + 
          (deleteFile(filename) ? " was deleted." : " could not be deleted.")
      );
  }
  public static void main(String args[]) {
       test("file", "input.txt");
       test("file", File.seperator + "input.txt");
       test("directory", "docs");
       test("directory", File.seperator + "docs" + File.seperator);
  }

}</lang>

MAXScript

There's no way to delete folders in MAXScript

-- Here
deleteFile "input.txt"
-- Root
deleteFile "\input.txt"

OCaml

<lang ocaml>Sys.remove "input.txt";; Sys.remove "/input.txt";;</lang>

with the Unix library: <lang ocaml>#load "unix.cma";; Unix.unlink "input.txt";; Unix.unlink "/input.txt";; Unix.rmdir "docs";; Unix.rmdir "/docs";;</lang>

Perl

<lang perl>use File::Spec::Functions qw(catfile rootdir);

  1. here

unlink 'input.txt'; rmdir 'docs';

  1. root dir

unlink catfile rootdir, 'input.txt'; rmdir catfile rootdir, 'docs';</lang>

Without Perl Modules

Current directory

perl -e 'unlink input.txt'
perl -e 'rmdir docs'

Root Directory

perl -e 'unlink "/input.txt"'
perl -e 'rmdir "/docs"'

PowerShell

Delete-Item input.txt
# Can also use the del alias for the Delete-Item cmdlet
del input.txt


Python

<lang python>import os

  1. current directory

os.remove("output.txt") os.rmdir("docs")

  1. root directory

os.remove("/output.txt") os.rmdir("/docs")</lang>

Raven

'input.txt'  delete
'/input.txt' delete
'docs'  rmdir
'/docs' rmdir

Ruby

 #!/usr/bin/env ruby
 File.delete("output.txt", "/output.txt")
 Dir.delete("docs")
 Dir.delete("/docs")

Toka

 needs shell
 " docs" remove
 " input.txt" remove

UNIX Shell

rm -rf docs
rm input.txt
rm -rf /docs
rm /input.txt

Visual Basic .NET

Platform: .NET

Works with: Visual Basic .NET version 9.0+
'Current Directory
IO.Directory.Delete("docs")
IO.Directory.Delete("docs", True) 'also delete files and sub-directories
IO.File.Delete("output.txt")

'Root
IO.Directory.Delete("\docs")
IO.File.Delete("\output.txt")

'Root, platform independent
IO.Directory.Delete(IO.Path.DirectorySeparatorChar & "docs")
IO.File.Delete(IO.Path.DirectorySeparatorChar & "output.txt")