Delete a file: Difference between revisions

From Rosetta Code
Content added Content deleted
m (→‎{{header|JScript}}: change to JavaScript, works with JScript)
m (Fixed lang tags.)
Line 14: Line 14:
=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
Note: <tt>scratch</tt> 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>main:(
<lang algol68>main:(
PROC remove = (STRING file name)INT:
PROC remove = (STRING file name)INT:
BEGIN
BEGIN
Line 30: Line 30:
remove("docs");
remove("docs");
remove("/docs")
remove("/docs")
)</pre>
)</lang>


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
Line 40: Line 40:
=={{header|AWK}}==
=={{header|AWK}}==
Assuming we are on a Unix/Linux or at least Cygwin system:
Assuming we are on a Unix/Linux or at least Cygwin system:
<lang awk> system("rm input.txt");
<lang awk>system("rm input.txt");
system("rm /input.txt");
system("rm /input.txt");
system("rm -rf docs");
system("rm -rf docs");
system("rm -rf /docs");</lang>
system("rm -rf /docs");</lang>


=={{header|C}}==
=={{header|C}}==
Line 147: Line 147:
=={{header|Forth}}==
=={{header|Forth}}==
There is no means to delete directories in ANS Forth.
There is no means to delete directories in ANS Forth.
s" input.txt" delete-file throw
<lang forth> s" input.txt" delete-file throw
s" /input.txt" delete-file throw
s" /input.txt" delete-file throw</lang>


=={{header|Fortran}}==
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
{{works with|Fortran|90 and later}}
I don't know a way of deleting directories in Fortran
I don't know a way of deleting directories in Fortran
<lang fortran> OPEN (UNIT=5, FILE="input.txt", STATUS="OLD") ! Current directory
<lang fortran>OPEN (UNIT=5, FILE="input.txt", STATUS="OLD") ! Current directory
CLOSE (UNIT=5, STATUS="DELETE")
CLOSE (UNIT=5, STATUS="DELETE")
OPEN (UNIT=5, FILE="/input.txt", STATUS="OLD") ! Root directory
OPEN (UNIT=5, FILE="/input.txt", STATUS="OLD") ! Root directory
CLOSE (UNIT=5, STATUS="DELETE")</lang>
CLOSE (UNIT=5, STATUS="DELETE")</lang>
=={{header|Haskell}}==
=={{header|Haskell}}==


<lang haskell> import System.IO
<lang haskell>import System.IO
import System.Directory
import System.Directory

main = do
main = do
removeFile "output.txt"
removeFile "output.txt"
removeDirectory "docs"
removeDirectory "docs"
removeFile "/output.txt"
removeFile "/output.txt"
removeDirectory "/docs"</lang>
removeDirectory "/docs"</lang>


=={{header|Io}}==
=={{header|Io}}==
<lang io> Directory fileNamed("input.txt") remove
<lang io>Directory fileNamed("input.txt") remove
Directory directoryNamed("docs") remove
Directory directoryNamed("docs") remove
RootDir := Directory clone setPath("/")
RootDir := Directory clone setPath("/")
RootDir fileNamed("input.txt") remove
RootDir fileNamed("input.txt") remove
RootDir directoryNamed("docs") remove</lang>
RootDir directoryNamed("docs") remove</lang>


=={{header|J}}==
=={{header|J}}==
Line 185: Line 185:


NB. Or all at once...
NB. Or all at once...
ferase 'input.txt';'/input.txt';'docs';'/docs'
ferase 'input.txt';'/input.txt';'docs';'/docs'</lang>
</lang>


The function above actually uses a foreign conjunction and defined in the <tt>files</tt> library like so:
The function above actually uses a foreign conjunction and defined in the <tt>files</tt> library like so:
Line 192: Line 191:
NB.*ferase v erases a file
NB.*ferase v erases a file
NB. Returns 1 if successful, otherwise _1
NB. Returns 1 if successful, otherwise _1
ferase=: (1!:55 :: _1:) @ (fboxname &>) @ boxopen
ferase=: (1!:55 :: _1:) @ (fboxname &>) @ boxopen</lang>
</lang>


This means that you can directly erase files and directories without loading the <tt>files</tt> library.
This means that you can directly erase files and directories without loading the <tt>files</tt> library.
<lang j> 1!:55 <'input.txt'
<lang j>1!:55 <'input.txt'
1!:55 <'\input.txt'
1!:55 <'\input.txt'
1!:55 <'docs'
1!:55 <'docs'
1!:55 <'\docs'
1!:55 <'\docs'</lang>
</lang>


=={{header|Java}}==
=={{header|Java}}==
Line 254: Line 251:
=={{header|MAXScript}}==
=={{header|MAXScript}}==
There's no way to delete folders in MAXScript
There's no way to delete folders in MAXScript
-- Here
<lang maxscript>-- Here
deleteFile "input.txt"
deleteFile "input.txt"
-- Root
-- Root
deleteFile "\input.txt"
deleteFile "\input.txt"</lang>


=={{header|Objective-C}}==
=={{header|Objective-C}}==
Line 342: Line 339:
=={{header|Raven}}==
=={{header|Raven}}==


<lang raven> 'input.txt' delete
<lang raven>'input.txt' delete
'/input.txt' delete
'/input.txt' delete
'docs' rmdir
'docs' rmdir
'/docs' rmdir</lang>
'/docs' rmdir</lang>


=={{header|Ruby}}==
=={{header|Ruby}}==


<lang ruby> File.delete("output.txt", "/output.txt")
<lang ruby>File.delete("output.txt", "/output.txt")
Dir.delete("docs")
Dir.delete("docs")
Dir.delete("/docs")</lang>
Dir.delete("/docs")</lang>


=={{header|Scheme}}==
=={{header|Scheme}}==
Line 394: Line 391:
=={{header|Toka}}==
=={{header|Toka}}==


needs shell
<lang toka>needs shell
" docs" remove
" docs" remove
" input.txt" remove
" input.txt" remove</lang>


=={{header|UNIX Shell}}==
=={{header|UNIX Shell}}==
<lang bash> rm -rf docs
<lang bash>rm -rf docs
rm input.txt
rm input.txt
rm -rf /docs
rm -rf /docs
rm /input.txt</lang>
rm /input.txt</lang>


=={{header|Vedit macro language}}==
=={{header|Vedit macro language}}==
Line 418: Line 415:


{{works with|Visual Basic .NET|9.0+}}
{{works with|Visual Basic .NET|9.0+}}
'Current Directory
<lang vbnet>'Current Directory
IO.Directory.Delete("docs")
IO.Directory.Delete("docs")
IO.Directory.Delete("docs", True) 'also delete files and sub-directories
IO.Directory.Delete("docs", True) 'also delete files and sub-directories
IO.File.Delete("output.txt")
IO.File.Delete("output.txt")

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

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


{{omit from|TI-83 BASIC}} {{omit from|TI-89 BASIC}} <!-- Does not have a filesystem, just namespaced variables. -->
{{omit from|TI-83 BASIC}} {{omit from|TI-89 BASIC}} <!-- Does not have a filesystem, just namespaced variables. -->

Revision as of 12:35, 20 November 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. <lang algol68>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")

)</lang>

AutoHotkey

<lang AutoHotkey>FileDelete, input.txt FileDelete, \input.txt FileRemoveDir, docs, 1 FileRemoveDir, \docs, 1</lang>

AWK

Assuming we are on a Unix/Linux or at least Cygwin system: <lang awk>system("rm input.txt"); system("rm /input.txt"); system("rm -rf docs"); system("rm -rf /docs");</lang>

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>

C++

<lang cpp>#include <cstdio>

  1. include <direct.h>

int main() { remove( "input.txt" ); remove( "/input.txt" ); _rmdir( "docs" ); _rmdir( "/docs" );

return 0; }</lang>

C#

<lang csharp>using System; using System.IO;

namespace RosettaCode {

   class Program {
       static void Main() {
           try {
               File.Delete("input.txt");
               Directory.Delete("docs");
               File.Delete(@"\input.txt");
               Directory.Delete(@"\docs");
           } catch (Exception exception) {
               Console.WriteLine(exception.Message);
           }
       }
   }

}</lang>

Common Lisp

<lang lisp>(delete-file (make-pathname :name "input.txt")) (delete-file (make-pathname :directory '(:absolute "") :name "input.txt"))</lang> To delete directories we need an implementation specific extension. In clisp this is ext:delete-dir.

Works with: CLISP

<lang lisp>(let ((path (make-pathname :directory '(:relative "docs"))))

 (ext:delete-dir path))

(let ((path (make-pathname :directory '(:absolute "docs"))))

 (ext:delete-dir path))</lang>

Or you can use the portability library CL-FAD:

Library: CL-FAD

<lang lisp>(let ((path (make-pathname :directory '(:relative "docs"))))

 (cl-fad:delete-directory-and-files path))</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>

E

<lang e><file:input.txt>.delete(null) <file:docs>.delete(null) <file:///input.txt>.delete(null) <file:///docs>.delete(null)</lang>

Forth

There is no means to delete directories in ANS Forth. <lang forth> s" input.txt" delete-file throw s" /input.txt" delete-file throw</lang>

Fortran

Works with: Fortran version 90 and later

I don't know a way of deleting directories in Fortran <lang 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")</lang>

Haskell

<lang haskell>import System.IO import System.Directory

main = do

 removeFile "output.txt"
 removeDirectory "docs"
 removeFile "/output.txt"
 removeDirectory "/docs"</lang>

Io

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

J

The J standard library comes with a set of file access utilities. <lang j> load 'files'

  ferase 'input.txt'
  ferase '\input.txt'
  ferase 'docs'
  ferase '\docs'

NB. Or all at once...

  ferase 'input.txt';'/input.txt';'docs';'/docs'</lang>

The function above actually uses a foreign conjunction and defined in the files library like so: <lang j>NB. ========================================================= NB.*ferase v erases a file NB. Returns 1 if successful, otherwise _1 ferase=: (1!:55 :: _1:) @ (fboxname &>) @ boxopen</lang>

This means that you can directly erase files and directories without loading the files library. <lang j>1!:55 <'input.txt' 1!:55 <'\input.txt' 1!:55 <'docs' 1!:55 <'\docs'</lang>

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>

JavaScript

Works with: JScript

<lang javascript>var fso = new ActiveXObject("Scripting.FileSystemObject");

fso.DeleteFile('input.txt'); fso.DeleteFile('c:/input.txt');

fso.DeleteFolder('docs'); fso.DeleteFolder('c:/docs');</lang>

or

<lang javascript>var fso = new ActiveXObject("Scripting.FileSystemObject"); var f; f = fso.GetFile('input.txt'); f.Delete(); f = fso.GetFile('c:/input.txt'); f.Delete(); f = fso.GetFolder('docs'); f.Delete(); f = fso.GetFolder('c:/docs'); f.Delete();</lang>

Works with: UCB Logo

UCB Logo has no means to delete directories. <lang logo>erasefile "input.txt erasefile "/input.txt</lang>

MAXScript

There's no way to delete folders in MAXScript <lang maxscript>-- Here deleteFile "input.txt" -- Root deleteFile "\input.txt"</lang>

Objective-C

<lang objc>NSFileManager *fm = [NSFileManager defaultManager];

[fm removeFileAtPath:@"input.txt" handler:nil]; [fm removeFileAtPath:@"docs" handler:nil];</lang>

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"'

PHP

<lang php><?php unlink('input.txt'); unlink('/input.txt'); rmdir('docs'); rmdir('/docs'); ?></lang>

PowerShell

<lang powershell># possible aliases for Remove-Item: rm, del, ri Remove-Item input.txt Remove-Item \input.txt # file system root

Remove-Item -Recurse docs # recurse for deleting folders including content Remove-Item -Recurse \docs</lang>

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>

R

<lang R>file.remove("input.txt") file.remove("/input.txt")

  1. or

file.remove("input.txt", "/input.txt")

  1. or

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

  1. directories needs the recursive flag

unlink("docs", recursive = TRUE) unlink("/docs", recursive = TRUE)</lang>

The function unlink allows wildcards (* and ?)

Raven

<lang raven>'input.txt' delete '/input.txt' delete 'docs' rmdir '/docs' rmdir</lang>

Ruby

<lang ruby>File.delete("output.txt", "/output.txt") Dir.delete("docs") Dir.delete("/docs")</lang>

Scheme

Works with: Scheme version R6RS

[1]

<lang scheme>(delete-file filename)</lang>

Slate

(It will succeed deleting the directory if it is empty)

<lang slate>(File newNamed: 'input.txt') delete. (File newNamed: '/input.txt') delete. (Directory newNamed: 'docs') delete. (Directory newNamed: '/docs') delete.</lang>

Smalltalk

(It will succeed deleting the directory if it is empty)

<lang smalltalk>File remove: 'input.txt'. File remove: 'docs'. File remove: '/input.txt'. File remove: '/docs'</lang>

Standard ML

<lang sml>OS.FileSys.remove "input.txt"; OS.FileSys.remove "/input.txt"; OS.FileSys.rmDir "docs"; OS.FileSys.rmDir "/docs";</lang>

Tcl

<lang tcl>file delete input.txt /input.txt

  1. preserve directory if non-empty

file delete docs /docs

  1. delete even if non-empty

file delete -force docs /docs</lang>

Toka

<lang toka>needs shell " docs" remove " input.txt" remove</lang>

UNIX Shell

<lang bash>rm -rf docs rm input.txt rm -rf /docs rm /input.txt</lang>

Vedit macro language

Vedit allows using either '\' or '/' as directory separator character, it is automatically converted to the one used by the operating system. <lang vedit>// In current directory File_Delete("input.txt", OK) File_Rmdir("docs")

// In the root directory File_Delete("/input.txt", OK) File_Rmdir("/docs")</lang>

Visual Basic .NET

Platform: .NET

Works with: Visual Basic .NET version 9.0+

<lang vbnet>'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")</lang>