Find a file with a certain extension in folder
Look at the System.IO.Directory
class and the static method GetFiles
. It has an overload that accepts a path and a search pattern. Example:
string[] files = System.IO.Directory.GetFiles(path, "*.txt");
List all files in a directory with a certain extension
I suggest you to firstly search the files and then perform the ls -l
(or whatever else). For example like this:
find . -name "*php" -type f -exec ls -l {} \;
and then you can pipe the awk
expression to make the addition.
Find all files in a directory with extension .txt in Python
You can use glob
:
import glob, os
os.chdir("/mydir")
for file in glob.glob("*.txt"):
print(file)
or simply os.listdir
:
import os
for file in os.listdir("/mydir"):
if file.endswith(".txt"):
print(os.path.join("/mydir", file))
or if you want to traverse directory, use os.walk
:
import os
for root, dirs, files in os.walk("/mydir"):
for file in files:
if file.endswith(".txt"):
print(os.path.join(root, file))
Recursively find files with a specific extension
My preference:
find . -name '*.jpg' -o -name '*.png' -print | grep Robert
Get all files with a specific extension
Just add an if
statement:
@echo off
for %%f in (*.ext) do (
if "%%~xf"==".ext" echo %%f
)
How can I find all the folders that contain certain file extensions in Bash?
You can use find's -printf
action to print out just the directory name, and then remove duplicates:
find . -name '*.tf' -not -path '*.terraform*' -printf '%h\n' | sort -u
The -printf '%h\n'
prints out the name of the directory containing matched files, while the sort -u
at the end removes duplicates.
How to retrieve recursively any files with a specific extensions in PowerShell?
If sorting by Length is not a necessity, you can use the -Name
parameter to have Get-ChildItem
return just the name, then use [System.IO.Path]::GetFileNameWithoutExtension()
to remove the path and extension:
Get-ChildItem -Path .\ -Filter *.js -Recurse -File -Name| ForEach-Object {
[System.IO.Path]::GetFileNameWithoutExtension($_)
}
If sorting by length is desired, drop the -Name
parameter and output the BaseName
property of each FileInfo
object. You can pipe the output (in both examples) to clip
, to copy it into the clipboard:
Get-ChildItem -Path .\ -Filter *.js -Recurse -File| Sort-Object Length -Descending | ForEach-Object {
$_.BaseName
} | clip
If you want the full path, but without the extension, substitute $_.BaseName
with:
$_.FullName.Remove($_.FullName.Length - $_.Extension.Length)
Copy all files with a certain extension from all subdirectories
--parents
is copying the directory structure, so you should get rid of that.
The way you've written this, the find
executes, and the output is put onto the command line such that cp
can't distinguish between the spaces separating the filenames, and the spaces within the filename. It's better to do something like
$ find . -name \*.xls -exec cp {} newDir \;
in which cp
is executed for each filename that find
finds, and passed the filename correctly. Here's more info on this technique.
Instead of all the above, you could use zsh and simply type
$ cp **/*.xls target_directory
zsh
can expand wildcards to include subdirectories and makes this sort of thing very easy.
INQUIRE to find files with certain extension in Fortran
This kind of file system interaction is a bit hard. You'd think that someone would have created a module for that, but there are precious few, and I haven't come across one that was easy to use.
What most people do is either use a specific call to the system
or execute_command_line
subroutines to let the shell do the work, see here:
program my_ls
use iso_fortran_env
implicit none
character(len=*), parameter :: ls_file = '/tmp/my_ls.tmp'
integer :: u, ios
character(len=30) :: filename
call execute_command_line('ls -1 > '//ls_file, wait=.TRUE., exitstat=ios)
if (ios /= 0) stop "Unable to get listing"
open(newunit=u, file=ls_file, iostat=ios, status="old", action="read")
if ( ios /= 0 ) stop "Error opening listing file "
do
read(u, *, iostat=ios) filename
if (is_iostat_end(ios)) exit
if (ios /= 0) STOP "Unexpected error while reading listing file"
if (index(filename, ".cel") > 0) then
print*, filename
end if
end do
close(u)
call execute_command_line('rm '//ls_file, wait=.FALSE.)
end program my_ls
But what would be easier is to give the filenames as a command line argument when starting the program:
program my_ls2
use iso_fortran_env
implicit none
integer :: num_files, i
character(len=64), allocatable :: filenames(:)
num_files = command_argument_count()
allocate(filenames(num_files))
do i = 1, num_files
call get_command_argument(i, filenames(i))
end do
write(*, '(A)') filenames
end program my_ls2
Calling this program with
$ my_ls2 *.cel
will give you every file you want.
Related Topics
Export Datatable to Excel File
How to Sum Up an Array of Integers in C#
How Is Gethashcode() of C# String Implemented
Error in Process.Start() -- the System Cannot Find the File Specified
Get Image Orientation and Rotate as Per Orientation
Fast Way to Convert a Two Dimensional Array to a List ( One Dimensional )
How to Use JSON.Net for JSON Modelbinding in an MVC5 Project
Default Value of a Type at Runtime
Use Xml Includes or Config References in App.Config to Include Other Config Files' Settings
Open Xml Reading from Excel File
Generic Method Multiple (Or) Type Constraint
Value Cannot Be Null. Parameter Name: Source
How to Determine for Which Platform an Executable Is Compiled