...
This commit is contained in:
@@ -124,3 +124,48 @@ Each Path object contains:
|
||||
- `exist`: Existence status
|
||||
|
||||
This provides a safe and convenient API for all file system operations in V.
|
||||
|
||||
## 5. Sub-path Getters and Checkers
|
||||
|
||||
The `pathlib` module provides methods to get and check for the existence of sub-paths (files, directories, and links) within a given path.
|
||||
|
||||
```v
|
||||
// Get a sub-path (file or directory) with various options
|
||||
path.sub_get(name:"mysub_file.md", name_fix_find:true, name_fix:true)!
|
||||
|
||||
// Check if a sub-path exists
|
||||
path.sub_exists(name:"my_sub_dir")!
|
||||
|
||||
// Check if a file exists
|
||||
path.file_exists("my_file.txt")
|
||||
|
||||
// Check if a file exists (case-insensitive)
|
||||
path.file_exists_ignorecase("My_File.txt")
|
||||
|
||||
// Get a file as a Path object
|
||||
path.file_get("another_file.txt")!
|
||||
|
||||
// Get a file as a Path object (case-insensitive)
|
||||
path.file_get_ignorecase("Another_File.txt")!
|
||||
|
||||
// Get a file, create if it doesn't exist
|
||||
path.file_get_new("new_file.txt")!
|
||||
|
||||
// Check if a link exists
|
||||
path.link_exists("my_link")
|
||||
|
||||
// Check if a link exists (case-insensitive)
|
||||
path.link_exists_ignorecase("My_Link")
|
||||
|
||||
// Get a link as a Path object
|
||||
path.link_get("some_link")!
|
||||
|
||||
// Check if a directory exists
|
||||
path.dir_exists("my_directory")
|
||||
|
||||
// Get a directory as a Path object
|
||||
path.dir_get("another_directory")!
|
||||
|
||||
// Get a directory, create if it doesn't exist
|
||||
path.dir_get_new("new_directory")!
|
||||
```
|
||||
|
||||
@@ -97,6 +97,12 @@ pub fn (mut plbook PlayBook) exists_once(args FindArgs) bool {
|
||||
return res.len == 1
|
||||
}
|
||||
|
||||
pub fn (mut plbook PlayBook) exists(args FindArgs) bool {
|
||||
mut res := plbook.find(args) or { [] }
|
||||
return res.len > 0
|
||||
}
|
||||
|
||||
|
||||
pub fn (mut plbook PlayBook) find_one(args FindArgs) !&Action {
|
||||
mut res := plbook.find(args)!
|
||||
if res.len == 0 {
|
||||
|
||||
@@ -226,7 +226,7 @@ fn (mut self ReplaceInstructions) replace_in_dir_recursive(path1 string, extensi
|
||||
mut pathnew := ''
|
||||
mut count := 0
|
||||
|
||||
console.print_debug(" - replace in dir: ${path1}")
|
||||
console.print_debug(' - replace in dir: ${path1}')
|
||||
|
||||
for item in items {
|
||||
// println(item)
|
||||
@@ -241,14 +241,14 @@ fn (mut self ReplaceInstructions) replace_in_dir_recursive(path1 string, extensi
|
||||
|
||||
self.replace_in_dir_recursive(pathnew, extensions, dryrun, mut done)!
|
||||
} else {
|
||||
tmpext:=os.file_ext(pathnew)[1..] or { ""}
|
||||
tmpext := os.file_ext(pathnew)[1..] or { '' }
|
||||
ext := tmpext.to_lower()
|
||||
if extensions == [] || ext in extensions {
|
||||
// means we match a file
|
||||
txtold := os.read_file(pathnew)!
|
||||
txtnew := self.replace(text: txtold, dedent: false)!
|
||||
if txtnew.trim(' \n') == txtold.trim(' \n') {
|
||||
//console.print_header(' nothing to do : ${pathnew}')
|
||||
// console.print_header(' nothing to do : ${pathnew}')
|
||||
} else {
|
||||
console.print_header(' replace done : ${pathnew}')
|
||||
count++
|
||||
|
||||
Reference in New Issue
Block a user