How to copy files with the same filenames as those in another folder to that same folder?

Hello All

A similar question like this was asked before but I need to change part of the question.

I've two folders, Folder A contains some image files in 150 subfolders;
Folder B contains text files in 350 subfolders.

All image files in Folder A have the same filename as the text files in Folder B.

That is to say, regardless of file extension, 10000.txt files in subfolders of Folder B share the same filename as 10000.tif files in subfolders of Folder A. For example 00011020.tif in subfolder 0120 of Folder A is able to find its corresponding 00011020.txt file in subfolder 0300 of Folder B. So same filename are not in the corresponding subfolders.

My question is:

How to copy the text files that share the same filename as those image files to the corresponding subfolders in Folder A. I want to combine all files in the subfolders of Folder A.

Thanks in advance for your help.

Chlade

Using shell script

#!/usr/bin/ksh
for TXTFILE in $(find FolderB -type f -name "*.txt")
do
 BASE=$(echo $(basename $TXTFILE) | cut -d. -f 1)
 TIFFILE=$(find FolderA -type f -name "${BASE}.tif")
 echo cp $TXTFILE $(dirname $TIFFILE)
done

Replace the red foldernames with actual directory names.

After reviewing the output you may remove the blue echo to actually copy the text files to corresponding image directories.

find . -name *.tif | xargs -I % bash -c 'val=%; find . -name $(basename ${val/.tif/.txt}) | xargs -I % cp % $(dirname $val) '

Run from root directory common to both folders or replace the . in find command with required folder names.

--ahamed

The first echo is not needed.

BASE=$(basename $TXTFILE | cut -d. -f 1)

And, if you have a filename like a.b.txt , this will give you a while I expect that you really want a.b . So, BASE=$(basename $TXTFILE .txt) would be better. But the loop could be made more efficient using:

#!/usr/bin/ksh
for TXTFILE in $(find FolderB -type f -name "*.txt")
do
 BASE=${TXTFILE%.txt}
 TIFFILE=$(find FolderA -type f -name "${BASE}.tif")
 echo cp $TXTFILE ${TIFFILE%/*}
done

But, even if there is only one file in each of the 350 subdirectories in folderB, this will still be running find 351 times (which will make this a very slow script). And, if there is a missing .txt file, you won't know about it; and if there is a missing .tif file you'll have a malformed cp command.

The following script runs find twice, and awk once no matter how many files need to be copied and prints diagnostics for missing .txt and .tif files:

#!/usr/bin/ksh
(find folderB -name '*.txt';find folderA -name '*.tif') | awk -F/ '
/txt$/ {d[substr($NF, 0, length($NF)-4)] = substr($0, 0, length($0)-length($NF))
        next
}
{       b = substr($NF, 0, length($NF) - 4)
        if(b in d) {
                printf("cp \"%s\" \"%s\"\n", $0, d)
                delete d
        } else {
                printf("printf \"No .txt file for %%s\\n\" \"%s\" >&2\n", $0)
        }
}
END {   for(i in d) 
                printf("printf \"No .tif file for %%s\\n\" \"%s\" >&2\n",
                        d i ".txt")
}' | /usr/bin/ksh

I suggest that you remove the | /bin/ksh from the last line of the script until you see that it will do what you want. (Note that the awk script is printing shell commands to be run by the shell; not running the commands themselves.)

You may need to change the pathname to the Korn shell (in both the 1st and last line of the script) if you move to a different system. And, if you're using a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk , /usr/xpg6/bin/awk , or nawk .

Everybody, thanks for the responses. I'm an idiot. I was so happy to find someone with my issue that I didn't bother looking at the OS. I needed this for Windows. I've found a solution for Windows.

Thanks for your help and sorry for wasting your time.

Chlade