new files

This commit is contained in:
Ingy döt Net 2013-04-10 12:38:42 -07:00
parent 3af7344581
commit 86c034bb8b
1364 changed files with 21352 additions and 0 deletions

View file

@ -0,0 +1 @@
In this task, the job is to verify that a file called "input.txt" and the directory called "docs" exist. This should be done twice: once for the current working directory and once for a file and a directory in the filesystem root.

View file

@ -0,0 +1,2 @@
---
note: File System Operations

View file

@ -0,0 +1,17 @@
with Ada.Text_IO; use Ada.Text_IO;
procedure File_Exists is
function Does_File_Exist (Name : String) return Boolean is
The_File : Ada.Text_IO.File_Type;
begin
Open (The_File, In_File, Name);
Close (The_File);
return True;
exception
when Name_Error =>
return False;
end Does_File_Exist;
begin
Put_Line (Boolean'Image (Does_File_Exist ("input.txt" )));
Put_Line (Boolean'Image (Does_File_Exist ("\input.txt")));
end File_Exists;

View file

@ -0,0 +1,20 @@
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Directories; use Ada.Directories;
procedure File_Exists is
procedure Print_File_Exist (Name : String) is
begin
Put_Line ("Does " & Name & " exist? " &
Boolean'Image (Exists (Name)));
end Print_File_Exist;
procedure Print_Dir_Exist (Name : String) is
begin
Put_Line ("Does directory " & Name & " exist? " &
Boolean'Image (Exists (Name) and then Kind (Name) = Directory));
end Print_Dir_Exist;
begin
Print_File_Exist ("input.txt" );
Print_File_Exist ("/input.txt");
Print_Dir_Exist ("docs");
Print_Dir_Exist ("/docs");
end File_Exists;

View file

@ -0,0 +1,28 @@
ON ERROR GOTO ohNo
f$ = "input.txt"
GOSUB opener
f$ = "\input.txt"
GOSUB opener
'can't directly check for directories,
'but can check for the NUL device in the desired dir
f$ = "docs\nul"
GOSUB opener
f$ = "\docs\nul"
GOSUB opener
END
opener:
e$ = " found"
OPEN f$ FOR INPUT AS 1
PRINT f$; e$
CLOSE
RETURN
ohNo:
IF (53 = ERR) OR (76 = ERR) THEN
e$ = " not" + e$
ELSE
e$ = "Unknown error"
END IF
RESUME NEXT

View file

@ -0,0 +1,14 @@
ON ERROR GOTO ohNo
d$ = "docs"
CHDIR d$
d$ = "\docs"
CHDIR d$
END
ohNo:
IF 76 = ERR THEN
PRINT d$; " not found"
ELSE
PRINT "Unknown error"
END IF
RESUME NEXT

View file

@ -0,0 +1,21 @@
f$ = "input.txt"
GOSUB opener
f$ = "\input.txt"
GOSUB opener
'can't directly check for directories,
'but can check for the NUL device in the desired dir
f$ = "docs\nul"
GOSUB opener
f$ = "\docs\nul"
GOSUB opener
END
opener:
d$ = DIR$(f$)
IF LEN(d$) THEN
PRINT f$; " found"
ELSE
PRINT f$; " not found"
END IF
RETURN

View file

@ -0,0 +1,28 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <unistd.h>
/* Check for regular file. */
int check_reg(const char *path) {
struct stat sb;
return stat(path, &sb) == 0 && S_ISREG(sb.st_mode);
}
/* Check for directory. */
int check_dir(const char *path) {
struct stat sb;
return stat(path, &sb) == 0 && S_ISDIR(sb.st_mode);
}
int main() {
printf("input.txt is a regular file? %s\n",
check_reg("input.txt") ? "yes" : "no");
printf("docs is a directory? %s\n",
check_dir("docs") ? "yes" : "no");
printf("/input.txt is a regular file? %s\n",
check_reg("/input.txt") ? "yes" : "no");
printf("/docs is a directory? %s\n",
check_dir("/docs") ? "yes" : "no");
return 0;
}

View file

@ -0,0 +1,17 @@
(import '(java.io File))
(defn kind [filename]
(let [f (File. filename)]
(cond
(.isFile f) "file"
(.isDirectory f) "directory"
(.exists f) "other"
:else "(non-existent)" )))
(defn look-for [filename]
(println filename ":" (kind filename)))
(look-for "input.txt")
(look-for "/input.txt")
(look-for "docs")
(look-for "/docs")

View file

@ -0,0 +1,23 @@
#!/usr/bin/escript
file_exists(Filename) ->
{ Flag, _ } = file:read_file_info(Filename), Flag == ok.
dir_exists(Filename) ->
{ Flag, Info } = file:read_file_info(Filename),
(Flag == ok) andalso (element(3, Info) == directory).
print_result(Flag, Filename) ->
Tail = if
Flag -> "exists";
true -> "does not exist"
end,
io:put_chars(lists:concat([Filename, " ", Tail, "\n"])).
check_file(Filename) -> print_result(file_exists(Filename), Filename).
check_dir(Filename) -> print_result(dir_exists(Filename), Filename).
main(_) ->
check_file("input.txt"),
check_dir("docs"),
check_file("/input.txt"),
check_dir("/docs").

View file

@ -0,0 +1,5 @@
: .exists ( str len -- ) 2dup file-status nip 0= if type ." exists" else type ." does not exist" then ;
s" input.txt" .exists
s" /input.txt" .exists
s" docs" .exists
s" /docs" .exists

View file

@ -0,0 +1,4 @@
LOGICAL :: file_exists
INQUIRE(FILE="input.txt", EXIST=file_exists) ! file_exists will be TRUE if the file
! exists and FALSE otherwise
INQUIRE(FILE="/input.txt", EXIST=file_exists)

View file

@ -0,0 +1,9 @@
logical :: dir_e
! a trick to be sure docs is a dir
inquire( file="./docs/.", exist=dir_e )
if ( dir_e ) then
write(*,*), "dir exists!"
else
! workaround: it calls an extern program...
call system('mkdir docs')
end if

View file

@ -0,0 +1,24 @@
package main
import (
"fmt"
"os"
)
func printStat(p string) {
switch i, err := os.Stat(p); {
case err != nil:
fmt.Println(err)
case i.IsDir():
fmt.Println(p, "is a directory")
default:
fmt.Println(p, "is a file")
}
}
func main() {
printStat("input.txt")
printStat("/input.txt")
printStat("docs")
printStat("/docs")
}

View file

@ -0,0 +1,13 @@
import System.IO
import System.Directory
check :: (FilePath -> IO Bool) -> FilePath -> IO ()
check p s = do
result <- p s
putStrLn $ s ++ if result then " does exist" else " does not exist"
main = do
check doesFileExist "input.txt"
check doesDirectoryExist "docs"
check doesFileExist "/input.txt"
check doesDirectoryExist "/docs"

View file

@ -0,0 +1,18 @@
import java.io.File;
public class FileExistsTest {
public static boolean isFileExists(String filename) {
boolean exists = new File(filename).exists();
return exists;
}
public static void test(String type, String filename) {
System.out.println("The following " + type + " called " + filename +
(isFileExists(filename) ? " exists." : " not exists.")
);
}
public static void main(String args[]) {
test("file", "input.txt");
test("file", File.separator + "input.txt");
test("directory", "docs");
test("directory", File.separator + "docs" + File.separator);
}
}

View file

@ -0,0 +1,20 @@
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
public class FileExistsTest{
private static FileSystem defaultFS = FileSystems.getDefault();
public static boolean isFileExists(String filename){
return Files.exists(defaultFS.getPath(filename));
}
public static void test(String type, String filename){
System.out.println("The following " + type + " called " + filename +
(isFileExists(filename) ? " exists." : " not exists.")
);
}
public static void main(String args[]){
test("file", "input.txt");
test("file", defaultFS.getSeparator() + "input.txt");
test("directory", "docs");
test("directory", defaultFS.getSeparator() + "docs" + defaultFS.getSeparator());
}
}

View file

@ -0,0 +1,6 @@
var fso = new ActiveXObject("Scripting.FileSystemObject");
fso.FileExists('input.txt');
fso.FileExists('c:/input.txt');
fso.FolderExists('docs');
fso.FolderExists('c:/docs');

View file

@ -0,0 +1,12 @@
function output( s, b )
if b then
print ( s, " does not exist." )
else
print ( s, " does exist." )
end
end
output( "input.txt", io.open( "input.txt", "r" ) == nil )
output( "/input.txt", io.open( "/input.txt", "r" ) == nil )
output( "docs", io.open( "docs", "r" ) == nil )
output( "/docs", io.open( "/docs", "r" ) == nil )

View file

@ -0,0 +1,9 @@
require "lfs"
for i, path in ipairs({"input.txt", "/input.txt", "docs", "/docs"}) do
local mode = lfs.attributes(path, "mode")
if mode then
print(path .. " exists and is a " .. mode .. ".")
else
print(path .. " does not exist.")
end
end

View file

@ -0,0 +1,4 @@
if (file_exists('input.txt')) echo 'input.txt is here right by my side';
if (file_exists('docs' )) echo 'docs is here with me';
if (file_exists('/input.txt')) echo 'input.txt is over there in the root dir';
if (file_exists('/docs' )) echo 'docs is over there in the root dir';

View file

@ -0,0 +1,7 @@
use File::Spec::Functions qw(catfile rootdir);
# here
print -e 'input.txt';
print -d 'docs';
# root dir
print -e catfile rootdir, 'input.txt';
print -d catfile rootdir, 'docs';

View file

@ -0,0 +1,3 @@
(if (info "file.txt")
(prinl "Size: " (car @) " bytes, last modified " (stamp (cadr @) (cddr @)))
(prinl "File doesn't exist") )

View file

@ -0,0 +1,6 @@
import os
os.path.exists("input.txt")
os.path.exists("/input.txt")
os.path.exists("docs")
os.path.exists("/docs")

View file

@ -0,0 +1,7 @@
file.exists("input.txt")
file.exists("/input.txt")
file.exists("docs")
file.exists("/docs")
# or
file.exists("input.txt", "/input.txt", "docs", "/docs")

View file

@ -0,0 +1,4 @@
File.file?("input.txt")
File.file?("/input.txt")
File.directory?("docs")
File.directory?("/docs")

View file

@ -0,0 +1,4 @@
["input.txt", "/input.txt"].each { |f|
printf "%s is a regular file? %s\n", f, File.file?(f) }
["docs", "/docs"].each { |d|
printf "%s is a directory? %s\n", d, File.directory?(d) }

View file

@ -0,0 +1 @@
(file-exists? filename)

View file

@ -0,0 +1,2 @@
FileDirectory new fileExists: 'c:\serial'.
(FileDirectory on: 'c:\') directoryExists: 'docs'.

View file

@ -0,0 +1,4 @@
(Directory name: 'docs') exists ifTrue: [ ... ]
(Directory name: 'c:\docs') exists ifTrue: [ ... ]
(File name: 'serial') isFile ifTrue: [ ... ]
(File name: 'c:\serial') isFile ifTrue: [ ... ]

View file

@ -0,0 +1,15 @@
if { [file exists "input.txt"] } {
puts "input.txt exists"
}
if { [file exists [file nativename "/input.txt"]] } {
puts "/input.txt exists"
}
if { [file isdirectory "docs"] } {
puts "docs exists and is a directory"
}
if { [file isdirectory [file nativename "/docs"]] } {
puts "/docs exists and is a directory"
}