how to write a function dir_info(directory) which returns 3 values - directory

(1) Number of files inside the directory (recursively)
(2) Number of directories inside the directory (recursively)
def dir_info(directory):
nfiles = how many files inside directory?
ndirs = how many sub directories inside directory?
return nfiles, ndirs

Related

Apple Script: when moving a file into a folder, is it possible to move duplicates inside that folder?

I have an apple script which moves all files within a folder into a new folder according to the first 6 characters of the filenames.
If the folder already exists, the files get moved into that existing folder instead.
If the file already exists in the new/existing folder, the file to be moved into the folder gets a "copy" extension to the filename – which is not in the script, it's a system function ;-)
My question:
Is it possible to move all existing (duplicate) files into a subfolder (named "_alt" for example), before the other files get moved within the folder (to prevent the "copy"-extension)? I would like to keep both files, the original files and the newly moved files.
Thanks.
This is my existing script:
set mgFilesFolder to (choose folder with prompt "Choose folder…")
(* get the files of the mgFilesFolder folder *)
tell application "Finder" to set fileList to files of mgFilesFolder
(* iterate over every file *)
repeat with i from 1 to number of items in fileList
set this_item to item i of fileList
set this_file_Name to name of this_item as string
set thisFileCode to characters 1 thru 6 of this_file_Name as text
log thisFileCode
tell application "Finder"
try
set nf to make new folder at mgFilesFolder with properties {name:thisFileCode}
end try
end tell
end repeat
set sourceFolder to mgFilesFolder
set destinationFolder to mgFilesFolder
tell application "Finder"
set the_files to (get files of mgFilesFolder)
repeat with this_file in the_files
set the_name to name of this_file
set the_short_name to items 1 thru 6 of the_name as string
if exists folder the_short_name of mgFilesFolder then move this_file to folder the_short_name of mgFilesFolder
end repeat
end tell

How do I list all files and directories in a directory in Julia?

In Python, I could use:
from os import listdir
os.listdir()
In Julia, how can I list everything inside a given directory?
readdir()
This lists all files and in a current directory folder as Vector{String}
Of course you can provide directory name:
readdir("dir/name")
On Windows a useful practice is raw String macro:
readdir(raw"c:\temp")
If you want to see what is in subdirectories use walkdir:
items = [item for item in walkdir("some/path")]
This walks through whole directory tree and returns a Vector of 3-element tuples where first element is directory, second list of it subdirectories and the third the list of files in a directory.

Delete empty folders inside working directory

I have number of empty folders inside a working directory. Want to remove those empty folders in R. Can somebody help me out on this.
Assuming the current directory is the one in which you want to delete the empty folders, you can do:
folders <- list.dirs(recursive = FALSE)
for(folder in folders){
if(length(dir(folder)) == 0){
unlink(folder, recursive = TRUE)
}
}
We can delete folders using:
unlink("path_to_non_empty_dir", recursive=TRUE)
Edit: removed part about non-empty folders
From ?unlink:
If recursive = FALSE directories are not deleted, not even empty ones.

Recursively search subdirectories to find the first instance of file

I have a complex dataset that is spread over 80 directories for each city (C). Each of these cities have multiple and unidentical subdirectories of varying depth. To clarify this means that for an example: city 1 can have 5 subdirectories a-e, where each subdirectory again can have multiple subdirectories. Now I need to find the first instance of a .txt file in each terminal subdirectory and apply a function to the txt file (logical function that is already written). There are no .txt files in the pre-terminal subdirectories.
lapply(list.dirs,function(x) {
if length(list.files(path=x,pattern=".txt"))==0 {
**apply function to .txt file}**
else {**lapply list.dirs etc---**}
However, I´m left with a neverending loop this way. How can this be done efficiently?
you may need something like this :
Treat_txt<-function(direct){
if(length(list.files(direct,pattern=".txt"))){
do what you need to do with the text file
} else {
dirs<-list.dirs(direct,full.names=T,recursive=F)
sapply(dirs,Treat_txt)
}
}
And then you can just call the function with the path of the "top" directory

Applying script in subfolders

I cd into a folder and start python. I want to apply a script to fix filenames in a directory and in sub folders.
import os
for dirname, subdirs, files in os.walk('.'):
os.rename(file, file.replace('\r', '').replace('\n', '').replace(' ', '_')
print 'Processed ' + file.replace('\r', '').replace('\n', '')
I get error "AttributeError: 'list" object has no attribute 'replace'. Help, please?
os.walk returns a 3-tuple that includes the root directory of the script, a list of subdirectories, and a list of files. You unpacked the 3-tuple in the for-loop and you're calling replace on the list of files.
You may want something like this:
for dirname, subdirs, files in os.walk('.'):
for file in files:
os.rename(file, file.replace('\r', '').replace('\n', '').replace(' ', '_')
print 'Processed ' + file.replace('\r', '').replace('\n', '')
You want to iterate through the list of the files and do you "replacing" on those individual files.

Resources