Validate for File naming scheme - validation

I have a file that drops into a folder location. I'm looking for a file named like this.
ID _Data_Tape_CCYYMMDD_hhmmss.txt
How do i accomplish this?

How about using a regular expression? in Racket, this will match the given file name:
(define name "42_Data_Tape_11220331_234532.txt")
(regexp-match #px"\\d+_Data_Tape_\\d{8}_\\d{6}.txt" name)
=> '("42_Data_Tape_11220331_234532.txt")
(regexp-match #px"\\d+_Data_Tape_\\d{8}_\\d{6}.txt" "something-else")
=> #f
To list all the files in a directory use directory-list or an equivalent procedure in your interpreter, then match their names against the above regular expression - and there, you found the files with the expected name.

Related

How can I read file loaded in a variable in guile?

I am new to to guile and scheme and what I am trying to do right now is take a scheme file (file.scm) and load it up into a variable so I will be able to parse it, and I am having trouble finding how to do this anywhere.
What I have right now is
(define var (load "file.scm")) ; loads file scheme
but I am unsure how to start reading the lines.
load parses and runs the scheme code in a file. If you just want to read a file, use open-input-file.
(define file (open-input-file "file address here"))
(display (read-line file))
If you just want to read an entire file as a string, there's a function for that in the textual-ports module. You'd use it something like:
(define contents (call-with-input-file "file.txt" get-string-all))
(You can use call-with-input-file and with-input-from-file to avoid having to manually open and close a file port, which is handy)

How is it possible to filter a list of directories via "directory-exists?"?

I recently discovered a strange behaviour of racket: Whenever I try to filter a list of directories created via directory-list my REPL returns me an empty list, but when I try the same with an quasiquoted list my REPL returns a correctly filtered list.
My questions is now: Why it's impossible to filter a list of directories via directory-exists? and how it's possible to list only directories in racket?
Some examples:
When I use directory-exists? in combination with filter and directory-list the REPL returns me everytime an empty list:
(filter directory-exists? (map path->complete-path
(directory-list (expand-user-path "~"))))
;; '()
Now, when I filter an quasiquoted list like in the following example. The REPL returns a correct filtered list, with all existing directories (without the imaginary directory ~/blablubb)
(filter directory-exists?
(map path->complete-path `(,(path->complete-path (expand-user-path "~/documents"))
,(expand-user-path "~/blablubb" ))))
;; '(#<path:/home/niklas/documents>)
FWIW running your snippet
(filter directory-exists?
(map path->complete-path
(directory-list (expand-user-path "~"))))
on OS X gives me a list of directories.
You are clearly seeing something else.
Two questions:
1. What is the result of:
(map path->complete-path
(directory-list (expand-user-path "~"))))
2. Which OS are you using?
directory-list returns relative paths by default, which are later implicitly resolved with respect to the (current-directory), which might be different from the directory you passed to directory-list.
Kind of like this:
~ $ ls dir
file1 subdir2
~ $ cat file1
cat: file1: No such file or directory
From the current directory, the file is at path dir/file1, not just file1.
One solution is to ask directory-list to include the directory in the paths returned:
(directory-list the-dir #:build? #t)
Then if the-dir is a complete path, the results will also be complete paths.

How to load file from dir using Racket?

I'm trying to set a dir to load files from using racket. I want to set the dir and then use command (load "extract.rktl") to load the file.
I'm on windows environment.
Command I'm trying is :
(add-to-list 'load-path ("c:/Users/racket/")
I receive error :
add-to-list: undefined;
cannot reference undefined identifier
context...:
the dir c:\Users\racket exists. Are commands correct ?
Update : this helped : How do I include files in DrScheme?
In Racket, path is a type and strings are not paths. So convert the name of a path with string->path.
(define default-dir
(string->path "c:\\user\\racket"))
Notes:
The Windows separator character '\' must be escaped as '\'.
Many Racket functions that act on paths will implicitly convert strings to paths without an explicit call to string->path.
However string operations cannot be permed on path objects.
>(string-split default-dir "\\")
string-split: contract violation
expected: string?
given: #<path:c:\user\racket>
Alternatively, a GUI can be used:
> (require racket/gui)
> (define my-file (get-file))
> my-file
#<path:/home/ben/Documents/racket/my-module.rkt>

Racket Scheme: Include externel rkt file with dynamic file name

I want to load external rkt file in racket scheme from a parameter of a function.
E.G.,
(define (test fileName)
(include fileName)
)
However, I am getting error indicating that fileName is not a pathname string, file' form, orlib' form.
Is there a way to fix this or is there another better way to include a file from dynamic filename?
The best way to do this is to make the external file a module, and use dynamic-require.

Create Files Through Racket

How would I use Racket to create a file to be able to store and edit user-inputted data, or, for example, a high score. I've read through some of the documentation and haven't found a clear answer on how to do this.
The Racket Guide has a chapter on Input and Output. The first section explains reading and writing files, with examples. It says
Files: The open-output-file function opens a file for writing, and
open-input-file opens a file for reading.
Examples:
> (define out (open-output-file "data"))
> (display "hello" out)
> (close-output-port out)
> (define in (open-input-file "data"))
> (read-line in)
"hello"
> (close-input-port in)
If a file exists already, then open-output-file raises an exception by
default. Supply an option like #:exists 'truncate or #:exists 'update
to re-write or update the file:
and so on.
There are some simple functions for reading and writing a file in the 2htdp/batch-io library: http://docs.racket-lang.org/teachpack/2htdpbatch-io.html . They are somewhat limited in that they access a file only in the same directory as the program itself, but you can do something like:
(require 2htdp/batch-io)
(write-file "highscore.txt" "Alice 25\nBob 40\n")
to write data to a file (the \n means a newline character), and then
(read-lines "highscore.txt")
to get back the lines of the file, as a list of strings.

Resources