new files
This commit is contained in:
parent
3af7344581
commit
86c034bb8b
1364 changed files with 21352 additions and 0 deletions
1
Task/Check-that-file-exists/0DESCRIPTION
Normal file
1
Task/Check-that-file-exists/0DESCRIPTION
Normal 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.
|
||||
2
Task/Check-that-file-exists/1META.yaml
Normal file
2
Task/Check-that-file-exists/1META.yaml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
---
|
||||
note: File System Operations
|
||||
17
Task/Check-that-file-exists/Ada/check-that-file-exists-1.ada
Normal file
17
Task/Check-that-file-exists/Ada/check-that-file-exists-1.ada
Normal 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;
|
||||
20
Task/Check-that-file-exists/Ada/check-that-file-exists-2.ada
Normal file
20
Task/Check-that-file-exists/Ada/check-that-file-exists-2.ada
Normal 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;
|
||||
|
|
@ -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
|
||||
|
|
@ -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
|
||||
|
|
@ -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
|
||||
28
Task/Check-that-file-exists/C/check-that-file-exists.c
Normal file
28
Task/Check-that-file-exists/C/check-that-file-exists.c
Normal 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;
|
||||
}
|
||||
|
|
@ -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")
|
||||
|
|
@ -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").
|
||||
|
|
@ -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
|
||||
|
|
@ -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)
|
||||
|
|
@ -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
|
||||
24
Task/Check-that-file-exists/Go/check-that-file-exists.go
Normal file
24
Task/Check-that-file-exists/Go/check-that-file-exists.go
Normal 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")
|
||||
}
|
||||
|
|
@ -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"
|
||||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
|
@ -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');
|
||||
12
Task/Check-that-file-exists/Lua/check-that-file-exists-1.lua
Normal file
12
Task/Check-that-file-exists/Lua/check-that-file-exists-1.lua
Normal 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 )
|
||||
|
|
@ -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
|
||||
|
|
@ -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';
|
||||
|
|
@ -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';
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
(if (info "file.txt")
|
||||
(prinl "Size: " (car @) " bytes, last modified " (stamp (cadr @) (cddr @)))
|
||||
(prinl "File doesn't exist") )
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
import os
|
||||
|
||||
os.path.exists("input.txt")
|
||||
os.path.exists("/input.txt")
|
||||
os.path.exists("docs")
|
||||
os.path.exists("/docs")
|
||||
7
Task/Check-that-file-exists/R/check-that-file-exists.r
Normal file
7
Task/Check-that-file-exists/R/check-that-file-exists.r
Normal 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")
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
File.file?("input.txt")
|
||||
File.file?("/input.txt")
|
||||
File.directory?("docs")
|
||||
File.directory?("/docs")
|
||||
|
|
@ -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) }
|
||||
|
|
@ -0,0 +1 @@
|
|||
(file-exists? filename)
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
FileDirectory new fileExists: 'c:\serial'.
|
||||
(FileDirectory on: 'c:\') directoryExists: 'docs'.
|
||||
|
|
@ -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: [ ... ]
|
||||
15
Task/Check-that-file-exists/Tcl/check-that-file-exists.tcl
Normal file
15
Task/Check-that-file-exists/Tcl/check-that-file-exists.tcl
Normal 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"
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue