Copying files from list

English support forum

Moderators: petermad, Stefan2, Hacker

Post Reply
User avatar
solid
Power Member
Power Member
Posts: 764
Joined: 2004-08-09, 11:20 UTC

Copying files from list

Post by *solid »

I have a file with a list with many filenames (only names, not paths) that need to be copied.
Files from the list are scattered among many subfolders together with many other files. Filenames are unique, if that matters. I need to copy them and to keep the existing folder structure.

I thought to use branch view or 'feed to listbox' from findfiles with 'get selection from file', but those lists don't support loading selections.

Is there a way in TC to perform this, or should i script it?
Fla$her
Power Member
Power Member
Posts: 4033
Joined: 2020-01-18, 04:03 UTC

Re: Copying files from list

Post by *Fla$her »

keep the existing folder structure
Relative to what? The path of the active panel?
Is there a way in TC to perform this, or should i script it?
Depends on the size of the list. If it's small, you can copy it to the clipboard (em_ListToClip: %Comspec% /q/c clip<C:\path\to\list.txt) and do something like cm_Copy /B1SGRW="%$CLIPNAME0%" (/T="%T" in the second field). But it's just an idea, I haven't tried it.
Overquoting is evil! 👎
User avatar
solid
Power Member
Power Member
Posts: 764
Joined: 2004-08-09, 11:20 UTC

Re: Copying files from list

Post by *solid »

Fla$her wrote: 2026-05-15, 19:55 UTC Relative to what? The path of the active panel?
If the file is in F:\upperfolder\starthere\files\morefiles\file.ext and the destination is T:\copyhere\, my source panel is at F:\upperfolder\starthere, then file should be copied to T:\copyhere\files\morefiles\file.ext

Another file is F:\upperfolder\starthere\files_5\evenmorefiles\af\file2.ext then copy to T:\copyhere\files_5\evenmorefiles\af\file2.ext

Fla$her wrote: 2026-05-15, 19:55 UTC Depends on the size of the list. If it's small, you can copy it to the clipboard (em_ListToClip: %Comspec% /q/c clip<C:\path\to\list.txt) and do something like cm_Copy /B1SGRW="%$CLIPNAME0%" (/T="%T" in the second field). But it's just an idea, I haven't tried it.
I haven't tried it either, but AFAI understand it, it will look for the file only in the source folder, not subfolders. I have only filenames in the list, not paths.
User avatar
white
Power Member
Power Member
Posts: 6941
Joined: 2003-11-19, 08:16 UTC
Location: Netherlands

Re: Copying files from list

Post by *white »

solid wrote: 2026-05-15, 17:42 UTC I have a file with a list with many filenames (only names, not paths) that need to be copied.
Files from the list are scattered among many subfolders together with many other files. Filenames are unique, if that matters. I need to copy them and to keep the existing folder structure.

I thought to use branch view or 'feed to listbox' from findfiles with 'get selection from file', but those lists don't support loading selections.
Yes they do. But normally you have to include path (search results) or part of the path (branch view). That is, unless you use wildcards. So transform your list to this format:

Code: Select all

*"file 1.txt"
*"file 2.exe"
*"file 3"
...
And then use the LOADSELECTION command to load the selection.



Another way is to utilize the search duplicate files function. For that you need actual files with the filenames. If you don't have files with these filenames, you can create them.

Suppose you have 1000 names in your list. You can create 1000 files without content by pressing Shift+F4 and using this template:

Code: Select all

<1-1000>[C]
Then select all files and press Ctrl+M to open the multi-rename tool. Then press F10, choose "Load names from file" and select your list of filenames. Then rename the files.

Once you have all the files, open search.
In the "Search in" field, enter your target folder and the folder with created empty files, separated by ";".
Use the Advanced tab to search for duplicates, and search for same name only.
Start the search and feed the result to the listbox.
Press Num + (the Select duplicate files dialog pops up) and use the appropriate options to select only the files you want to copy.

When using this method you can only copy with full path, so you will have to more work to get only the part of the path you want.
User avatar
white
Power Member
Power Member
Posts: 6941
Joined: 2003-11-19, 08:16 UTC
Location: Netherlands

Re: Copying files from list

Post by *white »

solid wrote: 2026-05-15, 21:22 UTC
Fla$her wrote: 2026-05-15, 19:55 UTC Depends on the size of the list. If it's small, you can copy it to the clipboard (em_ListToClip: %Comspec% /q/c clip<C:\path\to\list.txt) and do something like cm_Copy /B1SGRW="%$CLIPNAME0%" (/T="%T" in the second field). But it's just an idea, I haven't tried it.
I haven't tried it either, but AFAI understand it, it will look for the file only in the source folder, not subfolders. I have only filenames in the list, not paths.
It will filter the files you specify. So you copy a whole structure and you filter only the filenames you want. You can use the normal copy function and specify your filenames in the "Only file of this type" field. But the amount of filenames you can enter here is of course limited.
Fla$her
Power Member
Power Member
Posts: 4033
Joined: 2020-01-18, 04:03 UTC

Re: Copying files from list

Post by *Fla$her »

solid wrote: 2026-05-15, 21:22 UTCI haven't tried it either, but AFAI understand it, it will look for the file only in the source folder, not subfolders.
If you select everything with Ctrl+A, it will also copy from the subfolders.
In this case, /R will no longer be needed, since it is only required after Ctrl+B.
Overquoting is evil! 👎
User avatar
solid
Power Member
Power Member
Posts: 764
Joined: 2004-08-09, 11:20 UTC

Re: Copying files from list

Post by *solid »

white wrote: 2026-05-15, 21:48 UTC Yes they do. But normally you have to include path (search results) or part of the path (branch view). That is, unless you use wildcards. So transform your list to this format:

Code: Select all

*"file 1.txt"
*"file 2.exe"
*"file 3"
...
And then use the LOADSELECTION command to load the selection.
Thanks a lot for this hint. It is exactly what I needed.
Is this syntax actually documented somewhere?
Fla$her wrote: 2026-05-15, 22:29 UTC If you select everything with Ctrl+A, it will also copy from the subfolders.
In this case, /R will no longer be needed, since it is only required after Ctrl+B.
Thanks to you too.
@white already explained how it works, but I think I will stick to his other solution.


Problem solved!
User avatar
white
Power Member
Power Member
Posts: 6941
Joined: 2003-11-19, 08:16 UTC
Location: Netherlands

Re: Copying files from list

Post by *white »

solid wrote: 2026-05-16, 10:48 UTC Is this syntax actually documented somewhere?
I don't think so, but I did mention this syntax on the forum before.
Fla$her
Power Member
Power Member
Posts: 4033
Joined: 2020-01-18, 04:03 UTC

Re: Copying files from list

Post by *Fla$her »

solid wrote: 2026-05-16, 10:48 UTC but I think I will stick to his other solution.
I still want to know how many characters are in the list?
Overquoting is evil! 👎
User avatar
solid
Power Member
Power Member
Posts: 764
Joined: 2004-08-09, 11:20 UTC

Re: Copying files from list

Post by *solid »

white wrote: 2026-05-16, 11:37 UTC I don't think so, but I did mention this syntax on the forum before.
I guess I haven't seen it.
Fla$her wrote: 2026-05-16, 12:54 UTC I still want to know how many characters are in the list?
Don't know for sure, i can count them on Monday, but I believe it is below the limit.
User avatar
beb
Power Member
Power Member
Posts: 762
Joined: 2009-09-20, 08:03 UTC
Location: Odesa, Ukraine

Re: Copying files from list

Post by *beb »

Let's say you have:
  • a list file where the filenames are listed, e.g. 'd:\Downloads\copy_files_from_list.txt'
  • an active (source) directory in TC (%P) that can define:
    • a. (version 1) a source disk where the files in question can reside
    • b. (version 2) a source directory within which the files in question can reside
  • a passive (target) directory in TC (%T) as the destination
Then here are a user command, a button, and a script, which will:
  1. Read the list file
  2. Search for the files with the listed filenames within possible sources (see version 1, version 2)
  3. Copy the files into the destination, preserving the directory structures, respecting the specifics of the sources (see version 1, version 2)
The user command (usercmd.ini):

Code: Select all

[em_read_filenames_listed_in_a_text_file_then_find_those_files_who_knows_where_and_then_copy_them_into_the_destination_directory_preserving_original_directory_structure]
cmd=pwsh -c "d:\Downloads\copy_files_from_list.ps1"
param=%P '%T'
The user button:

Code: Select all

TOTALCMD#BAR#DATA
em_read_filenames_listed_in_a_text_file_then_find_those_files_who_knows_where_and_then_copy_them_into_the_destination_directory_preserving_original_directory_structure

WCMICON2.DLL
user command: read filenames from a text list file, find those files somewhere, and then  copy them into a dynamic destination preserving original directory structure


-1
The script (copy_files_from_list.ps1):

Code: Select all

$time = [diagnostics.stopwatch]::StartNew()

if(-not $args){
    'no input parameters have been passed; nothing to do here, exiting...'
    sleep -second 3
    exit
}

'input parameters:'|Write-Host -f Yellow
'[0] source path %P : {0}' -f $args[0];$sourcePath = $args[0] # %P current source path
'[1] target path %T : {0}' -f $args[1];$targetPath = $args[1] # %T current target path
''
$list = 'd:\Downloads\copy_files_from_list.txt'
if(-not [IO.File]::Exists($list)){
    'no valid filenames list has been found; nothing to do here, exiting...'
    sleep -second 3
    exit
}

$filenames = [IO.File]::ReadLines((Get-Item -path $list))|foreach{$_}
if($filenames.count -eq 0){
    'no filenames have been found in the provided list; nothing to do here, exiting...'
    sleep -second 3
    exit
}

'filenames list:'|Write-Host -f Yellow
for ($i=0;$i -lt $filenames.count;$i++){
    '[{0}] : {1}' -f $i,$filenames[$i]|Write-Host -f Green
}
''
'find and copy files from a filenames list into a destination preserving the directory structure...'|Write-Host -f Yellow
''
'phase 1 (version 1) : files are somewhere within the disk where the $sourcePath (%P) resides:'|Write-Host -f Yellow
$path = (Get-Item $sourcePath).PSDrive.Root
$files = Get-ChildItem -path $path -file -force -recurse -EA:silent|Where{$_.Name -in $filenames -and $_.FullName -notlike '*\$RECYCLE.BIN\*'}
foreach ($file in $files){
    # [IO.Path]::GetRelativePath($path,$file)
    $suffix = [IO.Path]::GetRelativePath($path,$file)
    $destination = [IO.FileInfo][IO.Path]::combine($targetPath,$suffix)
    $destinationPath = $destination.Directory
    if(-not [IO.Directory]::Exists($destinationPath)){
        New-Item $destinationPath -type directory -force|Out-Null
    }
    '{0} >> {1}' -f $file.FullName,$destination.FullName
    Copy-Item $file $destination -force
}
'{0} files have been copied' -f $files.count
$firstPhase =  $time.Elapsed
'{0:mm\:ss\.fff} : the first phase timing' -f $firstPhase
''
'phase 2 (version 2) : files are somewhere within the $sourcePath (%P):'|Write-Host -f Yellow
$path = $sourcePath
$files = Get-ChildItem -path $path -file -force -recurse -EA:silent|Where{$_.Name -in $filenames}
foreach ($file in $files){
    # [IO.Path]::GetRelativePath($path,$file)
    $suffix = [IO.Path]::GetRelativePath($path,$file)
    $destination = [IO.FileInfo][IO.Path]::combine($targetPath,$suffix)
    $destinationPath = $destination.Directory
    if(-not [IO.Directory]::Exists($destinationPath)){
        New-Item $destinationPath -type directory -force|Out-Null
    }
    '{0} >> {1}' -f $file.FullName,$destination.FullName
    Copy-Item $file $destination -force
}
'{0} files have been copied' -f $files.count
$secondPhase =  $time.Elapsed
'{0:mm\:ss\.fff} : the second phase timing' -f ($secondPhase-$firstPhase)
''
# finalizing
$time.Stop()
'{0:mm\:ss\.fff} : the total timing by {1}' -f $time.Elapsed,$MyInvocation.MyCommand.Name
sleep -second 33
Video illustration:
https://streamable.com/q1bz2d

Edit: code optimization
Last edited by beb on 2026-05-18, 22:26 UTC, edited 5 times in total.
#278521 User License
Total Commander [always the latest version, including beta] x86/x64 on Win10 x64/Windows 11/Android 17
User avatar
solid
Power Member
Power Member
Posts: 764
Joined: 2004-08-09, 11:20 UTC

Re: Copying files from list

Post by *solid »

2beb
Thanks for your effort.
+1 for Vivaldi on taskbar.
User avatar
beb
Power Member
Power Member
Posts: 762
Joined: 2009-09-20, 08:03 UTC
Location: Odesa, Ukraine

Re: Copying files from list

Post by *beb »

2solid
My pleasure.
But this is a simple example. We can easily make it a bit more complicated, for instance, building the list dynamically from the file under cursor, from selected files, from all or specific files from the current directory (flat or recursive), and so on.
solid wrote: 2026-05-18, 10:06 UTC +1 for Vivaldi on taskbar.
There ain't a lot of alternatives out there since Opera Presto said, "So long, and thanks for all the fish."
#278521 User License
Total Commander [always the latest version, including beta] x86/x64 on Win10 x64/Windows 11/Android 17
User avatar
white
Power Member
Power Member
Posts: 6941
Joined: 2003-11-19, 08:16 UTC
Location: Netherlands

Re: Copying files from list

Post by *white »

solid wrote: 2026-05-16, 10:48 UTC
white wrote: 2026-05-15, 21:48 UTC ... So transform your list to this format:

Code: Select all

*"file 1.txt"
*"file 2.exe"
*"file 3"
...
And then use the LOADSELECTION command to load the selection.
Thanks a lot for this hint. It is exactly what I needed.
Is this syntax actually documented somewhere?
I have now created the wiki page How-to: Copy files using filename list. So now this syntax is documented there ;)
Post Reply