Copying Files from Multiple Directories into a Single Destination Directory

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:

  1. Gets the current child folders.
  2. Creates a new folder called out.
  3. 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



Leave a reply



Submit