How to copy all files from multiple directories into a single directory using Ant
You can use something like this:
<property name="src.dir" value="D:/ant/"/>
<property name="dest.dir" value="D:/ant/images"/>
<copy todir="${dest.dir}">
<fileset dir="${src.dir}" includes="**/images/*">
<type type="file"/>
</fileset>
<mapper type="flatten" />
</copy>
The flatten mapper is the key part.
The type="file"
element ensures that only files are copied. Without that you might also see (empty) directories copied.
Copying files from multiple directories into a single destination directory
This little BaSH script will do it both ways:
#!/bin/sh
#
# counter
i=0
# put your new directory here
# can't be similar to dir_*, otherwise bash will
# expand it too
mkdir newdir
for file in `ls dir_*/*`; do
# gets only the name of the file, without directory
fname=`basename $file`
# gets just the file name, without extension
name=${fname%.*}
# gets just the extention
ext=${fname#*.}
# get the directory name
dir=`dirname $file`
# get the directory suffix
suffix=${dir#*_}
# rename the file using counter
fname_counter="${name}_$((i=$i+1)).$ext"
# rename the file using dir suffic
fname_suffix="${name}_$suffix.$ext"
# copy files using both methods, you pick yours
cp $file "newdir/$fname_counter"
cp $file "newdir/$fname_suffix"
done
And the output:
$ ls -R
cp.sh*
dir_asdf/
dir_ljklj/
dir_qwvas/
newdir/
out
./dir_asdf:
file.txt
./dir_ljklj:
file.txt
./dir_qwvas:
file.txt
./newdir:
file_1.txt
file_2.txt
file_3.txt
file_asdf.txt
file_ljklj.txt
file_qwvas.txt
Copying files of the same name from multiple directories into one directory
This should do the magic:
import os
import shutil
if __name__ == '__main__':
child_dirs = next(os.walk('.'))[1]
os.mkdir('out')
num = 1
for dir in child_dirs:
shutil.copy2('{}/image.fits'.format(dir), 'out/{}_image.fits'.format(num))
num+=1
It does the following:
- Gets the current child folders.
- Creates a new folder called
out
. - Loops over the folder child folders and copies the files to the new folders.
Note: the script should be ran on the parent folder.
Copy files from multiple directories and create same structure in other separate folder [Windows]
As per my comment, a single line batch file should do as you intended, (just change C:\MyListwSources.txt
as necessary):
@(For /F Delims^=^ EOL^= %%I In ('Type "C:\MyListwSources.txt"')Do @"%__AppDir__%xcopy.exe" /HRKVY "%%~I" "%UserProfile%\Desktop\MyStackedFiles%%~pI")&Pause
Take a look at the output from for /?
, (entered at the Command Prompt), to see the for
variable expansion modifiers and what each does.
How to copy images from multiple directories into corresponding multiple destination directories and resize them?
I revised my original answer because it really didn't do what you wanted, it basically only did what the code in your questions does—process all the image files in a single folder.
The driver function is named process_tree()
and its job is to traverse the home directory's sub-directories looking for files in them that match any in a set of the user-specified filename patterns, and if any are found, to create the destination sub-directory, and then call a user-supplied function on each one of them, passing it the existing source file name and the desired output file name, along with any arguments the users wants also passed to the supplied function.
In this code below the sample user-specified function is named resize_image()
. As the name implies, it only processes one image file.
Note that the processed images have the same name as the source images (i.e. no -_new
suffix gets added to their filenames).
import fnmatch
import os
from PIL import Image
verbose = True # Global printing flag for vprint().
def process_tree(src, dst, patterns, processing_func, *args):
vprint(' src: "{src}"'.format(src=src))
vprint('dst: "{dst}"'.format(dst=dst))
vprint()
for dirpath, subdirs, filenames in os.walk(src, topdown=False):
vprint('PROCESSING dirpath: "{}"'.format(dirpath))
if dirpath == src: # Root src directory?
if not os.path.exists(dst):
vprint('CREATING dst root: "{dst}"'.format(dst=dst))
os.makedirs(dst) # Make root dest folder.
vprint()
continue # Don't process files in root folder.
# Determine sub-directory of src being processed.
src_subdir = os.path.relpath(dirpath, src)
dst_subdir = os.path.join(dst, src_subdir)
# Determine which files in dirpath match one or more of the patterns.
if isinstance(patterns, str):
patterns = (patterns,) # Convert to single element sequence.
processible = set(filename for pattern in patterns
for filename in fnmatch.filter(filenames, pattern))
if not processible:
vprint('no files to process') # Output directory not created.
else:
if os.path.isdir(dst_subdir):
vprint('PROCESSING directory "{}"'.format(dst_subdir))
elif os.path.exists(dst_subdir):
raise NotADirectoryError('Non-drectory "{}" exists"'.format(dst_subdir))
else:
vprint('CREATING directory "{}"'.format(dst_subdir))
os.makedirs(dst_subdir)
vprint('processing files:')
for filename in filenames:
if filename in processible:
src_file_path = os.path.join(dirpath, filename)
dst_file_path = os.path.join(dst_subdir, filename)
try:
processing_func(src_file_path, dst_file_path, *args)
except Exception as exc:
vprint(' EXCEPTION processing file:\n {!s}'.format(exc))
vprint()
vprint()
def resize_image(src, dst, scale_factor):
""" Resize image src by scale_factor and save result to dst. """
vprint('resizing image:\n'
' src: "{src}"\n'
' scale factor: {scale_factor}'.format(**locals()))
img = Image.open(src)
# Calcuate new size.
new_width = round(img.width * scale_factor)
new_height = round(img.height * scale_factor)
if new_width < 1 or new_height < 1:
vprint(' width and/or height of scaled version of image "{filename}"\n'
' is less than 1 pixel - skipped'.format(filename=os.path.basename(src)))
return
resampling_method = Image.BICUBIC
img = img.resize((new_width, new_height), resample=resampling_method)
img.save(dst)
vprint(' resized image saved to "{}"'.format(dst))
def vprint(*args, **kwargs):
""" Only prints if global flag is set. """
if verbose:
return print(*args, **kwargs)
if __name__ == '__main__':
inputpath = r'\my\path\to\_source_images'
outputpath = r'\my\path\to\_processed_images'
process_tree(inputpath, outputpath, ('*.jpg',), resize_image, .5)
Related Topics
Any Reason for Using "*/" in Command "Ls -D */" to List Directories
Replace Text Between Two Lines with Contents of a File Stored in a Variable in Sed
Print Differences of File1 to File2 Without Deleting Anything from File2
Phusion Passenger Nginx Module Installer V3.0.17 Issue on Debian 6.0.5 Amd64 Due to Broken Package
Is an Operating System Kernel an Interpeter for All Other Programs
How to Get Gcc to Skip Errors, But Still Output Them
Dotnetcore: Cross Platform Version of Getinvalidfilenamechars
Replace Line with Space and Backslash with a String Containing Spaces
Script to Get User That Has Process with Most Memory Usage
How to Create a Bash Variable Like $Random
How to Merge Two Rows in a Same Row from a Text File in Linux Shell Script
Compile Git for 32-Bit Linux on Shared Hosting
How to Start/Restart/Stop Apache Server on Linux as Non-Root User
Shell Script to Delete Files When Disk Is Full
Get the Pid of a Running Playbook for Use Within the Playbook
How to Mount a Directory Inside a Docker Container on Linux Host