R list.files ignore system files - regex

I am using a command like below to get list of files in a folder.
My pattern argument is not working correctly. I want to pull a list of only jpg or csv files. How should i set my pattern argument?
Moreover, that folder contains .RData and .Rhistory files that are created by the system. I didnt put them. In such case is there an easier way to ignore system files when i compile a list of files in a folder?
filenames=list.files(path = "//c:/ch7data", pattern = "*.jpg|*.csv")
-------------------------update1
as per suggestions provided in one of the answers I used below code, but it didnt seem to work :(.
I got .Rdata and .rhistory and also . and .. I dont want these files/values.
I want only jpg and csv and xlsx files in the variable filenames
filenames=list.files(path = "//C:/ch7data", all.files = TRUE)
> filenames
[1] "." ".." ".RData" ".Rhistory"
[5] "CH7Data_20130401T130110.csv" "CH7Data_20130401T130110.jpg" "CH7Data_20130401T130610.csv" "CH7Data_20130401T130610.jpg"
[9] "CH7Data_20130401T131610.csv" "CH7Data_20130401T131610.jpg" "CH7Data_20130401T135010.csv" "CH7Data_20130401T135010.jpg"
[13] "ffa.xlsx" "Thumbs.db"
-----------------------update2
I used a command as below and it avoided . , .. , .Rdata and .rhistory :)
any way to avoid thumbs.db ?
> filenames=list.files(path = "//C:/ch7data", all.files = FALSE, no..=TRUE)
> filenames
[1] "CH7Data_20130401T130110.csv" "CH7Data_20130401T130110.jpg" "CH7Data_20130401T130610.csv" "CH7Data_20130401T130610.jpg"
[5] "CH7Data_20130401T131610.csv" "CH7Data_20130401T131610.jpg" "CH7Data_20130401T135010.csv" "CH7Data_20130401T135010.jpg"
[9] "ffa.xlsx" "Thumbs.db"

Another alternative:
Sys.glob(file.path("c:","ch7data",c("*.jpg","*.csv")))
Better to use globs rather than regular expressions
Use file.path to create path in OS independent manner.

To get only files that ends with .jpg or .csv, you can use the following pattern:
list.files(path = "//c:/ch7data", pattern = "^(.*)+(\\.jpg|\\.csv)$")

Related

How do I list files and directories those are not hidden in current directory using crystal language?

I wrote my own minimal version of "ls" command (Linux) using crystal language and here is my code:
require "dir"
require "file"
def main()
pwd = Dir.current
list_dir = Dir.children(pwd)
puts("[+] location: #{pwd}")
puts("------------------------------------------")
list_dir.each do |line|
check = File.file?(line)
if check == true
puts("[+] file : #{line}")
elsif check == false
puts("[+] directory: #{line}")
else
puts("[+] unknown : #{line}")
end
end
end
main
It works but it also listing all hidden files and directories (.files & .directories) too and I do not want to show those. I want the result more like "ls -l" command's result not like "ls -la".
So, what I need to implement to stop showing hidden files and directories?
There is nothing special about "hidden" files. It's just a convention to hide file names starting with a dot in some contexts by default. Dir.children does not follow that convention and expects the user to apply approriate filtering.
The recommended way to check if a file name starts with a dot is file_name.starts_with?(".").

Matlab Use Regex Expression to load file from directory

I have looked a lot for this but have not found anything. I am very new to matlab and regex in general.
My problem is, have a directory path 'dir' with only one .txt file in it. I do however not know the filename of the txt file. I want to load this file.
I have tried multiple things but cannot find the solution.
foo = load(fullfile(dir, '-regexp', '*.txt'))
Thank you for your help!
That syntax isn't valid for fullfile, and dir is an in-built function which it appears you're using as a variable... Here is something a little clearer which should work when you have a single txt file within a given folder
folder = 'my\folder\path\';
files = dir( fullfile( folder, '*.txt' ) );
if numel( files ) ~= 1
error( 'More or less than one .txt file found!' );
end
filepath = fullfile( files(1).folder, files(1).name );
foo = load( filepath ); % load is designed for .mat files, if your .txt contains anything
% non-numeric then you may want something more like readtable here...

Running EXE files in a folder sequence

I have a sequence of folders (planx 1 , planx 2 ,.......,planx 35) each folder contains an exe file triton.exe, I wrote the following code but it gives me a syntax error,apparently the format I wrote especially '+str(i)' is wrong.
I tried to correct that by adding " "before +str(i) but it reads the folder name (planx1) without space and surly there is no such folder.
what can I do to make it work ?
import sys, string, os
for i in range(1, 35):
os.chdir( 'E:\\project\\x\\CR 0\\planx'+str(i))
os.system( '"E:\\project\\x\\CR 0\\planx'+str(i)'\\triton.exe"')
print('done')
You are missing the + after the str(i) to add the third string as well:
'...\planx'+str(i)+'\\trit...'
# ^ this one
although you may want to use os.path.join instead of adding them together

listing directories having "-" character in directories name

I want to list the directories in current directory having "-" character in directories name. I used os.listdir(path). Its giving me error :
"WindowsError: [Error 123] The filename, directory name, or volume
label syntax is incorrect:"
Any help will be greatly appreciated
Use os.listdir to get directory contents and then filter using os.path.isdir to check if each item is a dir:
dirs_with_hyphen = []
for thing in os.listdir(os.getcwd()):
if os.path.isdir(thing) and '-' in thing:
dirs_with_hyphen.append(thing)
print dirs_with_hyphen # or return, etc.
And that can be shortened using list comprehension:
dirs_with_hyphen = [thing for thing in os.listdir(os.getcwd()) if os.path.isdir(thing) and '-' in thing]
I'm using os.getcwd but you could pass in any string that represents a folder.
If you're getting an error about the filename being incorrect, you're probably not escaping correctly or it's not pointing to the right folder (absolute vs relative path issue).
I did some testing and I managed to get your error. I don't know if this is what you did to get the error though since no example has been provided.
What I did though is give an invalid drive path. Not one that could be valid and doesn't exist, one that is always wrong eg.'C::\' or 'CC:\' just anything that's not 'C:\'. As for your question.
Path should generally look like this, prefixing with r to ignore backslash as escape character or double backslash.
import os
path = r"C:\Users\Steven\Documents\"
path = "C:\\Users\\Steven\\Documents\"
for file in os.listdir(path):
if os.path.isdir(path+file) and '-' in file:
print path + file
#List Comp
[path+file for file in os.listdir(path) if os.path.isdir(path+file) and '-' in file]

How to get a file to be used as input of the program that ends with special character in python

I have an output file from a code which its name will ends to "_x.txt" and I want to connect two codes which second code will use this file as an input and will add more data into it. Finally, it will ends into "blabla_x_f.txt"
I am trying to work it out as below, but seems it is not correct and I could not solve it. Please help:
inf = str(raw_input(*+"_x.txt"))
with open(inf+'_x.txt') as fin, open(inf+'_x_f.txt','w') as fout:
....(other operations)
The main problem is that the "blabla" part of the file could change to any thing every time and will be random strings, so the code needs to be flexible and just search for whatever ends with "_x.txt".
Have a look at Python's glob module:
import glob
files = glob.glob('*_x.txt')
gives you a list of all files ending in _x.txt. Continue with
for path in files:
newpath = path[:-4] + '_f.txt'
with open(path) as in:
with open(newpath, 'w') as out:
# do something