File Delete

From Rosetta Code

Jump to: navigation, search

Programming Task
This is a programming task. It lays out a problem which Rosetta Code users are encouraged to solve, using languages they know.

Code examples should be formatted along the lines of one of the existing prototypes.

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.

Contents

[edit] Ada

 
with Ada.Directories; use Ada.Directories;
 

and then

 
Delete_File ("input.txt");
Delete_File ("/input.txt");
Delete_Tree ("docs");
Delete_Tree ("/docs");
 

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.

[edit] 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")
)

[edit] AutoHotkey

 
FileDelete, input.txt
FileDelete, \input.txt
FileRemoveDir, docs, 1
FileRemoveDir, \docs, 1
 

[edit] AWK

Assuming we are ona Unix/Linux or at least Cygwin system:

system("rm input.txt");
system("rm /input.txt");
system("rm -rf docs");
system("rm -rf /docs");


[edit] C

ISO C:

#include <stdio.h>
 
int main() {
remove("input.txt");
remove("/input.txt");
remove("docs");
remove("/docs");
return 0;
}

POSIX:

#include <unistd.h>
 
int main() {
unlink("input.txt");
unlink("/input.txt");
rmdir("docs");
rmdir("/docs");
return 0;
}

[edit] C++

#include <cstdio>
#include <direct.h>
 
int main() {
remove( "input.txt" );
remove( "/input.txt" );
_rmdir( "docs" );
_rmdir( "/docs" );
 
return 0;
}

[edit] C#

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);
}
}
}
}

[edit] Common Lisp

(delete-file (make-pathname :name "input.txt"))
(delete-file (make-pathname :directory '(:absolute "") :name "input.txt"))

To delete directories we need an implementation specific extension. In clisp this is ext:delete-dir. Works with: CLISP

(let ((path (make-pathname :directory '(:relative "docs"))))
(ext:delete-dir path))
 
(let ((path (make-pathname :directory '(:absolute "docs"))))
(ext:delete-dir path))

Or you can use the portability library CL-FAD:

Library: CL-FAD

(let ((path (make-pathname :directory '(:relative "docs"))))
(cl-fad:delete-directory-and-files path))

[edit] D

Library: Tango

import tango.io.Path;
 
void main() {
remove("input.txt");
remove("/input.txt");
remove("docs");
remove("/docs");
}

Library: Tango

POSIX:

import tango.stdc.posix.unistd;
 
void main() {
unlink("input.txt");
unlink("/input.txt");
rmdir("docs");
rmdir("/docs");
}

[edit] E

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

[edit] Forth

There is no means to delete directories in ANS Forth.

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

[edit] 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")

[edit] Haskell

import System.IO
import System.Directory

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

[edit] Io

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

[edit] 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);
}
}

[edit] Logo

Works with: UCB Logo UCB Logo has no means to delete directories.

erasefile "input.txt
erasefile "/input.txt
</logo>

=={{header|MAXScript}}==
There's no way to delete folders in MAXScript
 -- Here
 deleteFile "input.txt"
 -- Root
 deleteFile "\input.txt"

=={{header|Objective-C}}==

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

[fm removeFileAtPath:@"input.txt" handler:nil];
[fm removeFileAtPath:@"docs" handler:nil];

[edit] OCaml

Sys.remove "input.txt";;
Sys.remove "/input.txt";;

with the Unix library:

#load "unix.cma";;
Unix.unlink "input.txt";;
Unix.unlink "/input.txt";;
Unix.rmdir "docs";;
Unix.rmdir "/docs";;

[edit] Perl

use File::Spec::Functions qw(catfile rootdir);
# here
unlink 'input.txt';
rmdir 'docs';
# root dir
unlink catfile rootdir, 'input.txt';
rmdir catfile rootdir, 'docs';

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

[edit] PowerShell

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

[edit] Python

import os
# current directory
os.remove("output.txt")
os.rmdir("docs")
# root directory
os.remove("/output.txt")
os.rmdir("/docs")

[edit] Raven

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

[edit] Ruby

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

[edit] Slate

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

(File newNamed: 'input.txt') delete.
(File newNamed: '/input.txt') delete.
(Directory newNamed: 'docs') delete.
(Directory newNamed: '/docs') delete.

[edit] Smalltalk

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

File remove: 'input.txt'.
File remove: 'docs'.
File remove: '/input.txt'.
File remove: '/docs'

[edit] Standard ML

OS.FileSys.remove "input.txt";
OS.FileSys.remove "/input.txt";
OS.FileSys.rmDir "docs";
OS.FileSys.rmDir "/docs";

[edit] Tcl

file delete input.txt /input.txt 
 
# preserve directory if non-empty
file delete docs /docs
 
# delete even if non-empty
file delete -force docs /docs

[edit] Toka

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

[edit] UNIX Shell

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

[edit] Vedit macro language

Vedit allows using either '\' or '/' as directory separator character, it is automatically converted to the one used by the operating system.

// In current directory
File_Delete("input.txt", OK)
File_Rmdir("docs")

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

[edit] 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")
Personal tools