Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move files from one folder to another from a list of filenames from a text file

I have a text file which contains a list of file names with extension(no spaces in the filename). enter image description here

I have two folders in two different paths, say, C:\ThirdParty and C:\Users. There will be subfolders in these paths.

What I need?

I need to search each filename in the text file on these two folders and its subfolders and move the exact matching files to another folders say, C:\Backup\ThirdParty and C:\Backup\Users.

How can i do this in Windows 7 with powershell or command prompt.

I have no idea how to start or I am zero at powershell and DOS commands.

like image 386
Sharon Avatar asked Nov 03 '25 04:11

Sharon


1 Answers

This script can be used for C:\ThirdParty and adapted for you needs.

$file_list = Get-Content C:\list.txt
$search_folder = "C:\ThirdParty"
$destination_folder = "C:\Backup\ThirdParty"

foreach ($file in $file_list) {
    $file_to_move = Get-ChildItem -Path $search_folder -Filter $file -Recurse -ErrorAction SilentlyContinue -Force | % { $_.FullName}
    if ($file_to_move) {
        Move-Item $file_to_move $destination_folder
    }
}

With Get-Content you retrieve the list of your files (one per line) in an array called $file_list.

You set a $search_folder and then a loop for each file looking recursively in the folder and its subfolders. If the file is found, is moved to $destination_folder

like image 62
Roberto Russo Avatar answered Nov 04 '25 20:11

Roberto Russo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!