Solved Batch File to find files in listed .txt file, move to folder

May 15, 2020 at 10:29:41
Specs: Windows 7, 32GB
My working folder is e:\photos
It has 20 sub-folders.
I have a video list text file "familymovies.txt" to parse for file names.
The batch file will search e:\photos for the first occurance of each file and move it to e:\photos\family-movies
It would be nice to log.txt two things:
1. Original full path and file name if found and moved.
2. List of any file names NOT found

Many thanks,
GregS


See More: Batch File to find files in listed .txt file, move to folder

Reply ↓  Report •

#1
May 15, 2020 at 10:58:12
does this list of filenames have the full path as well like:

E:\Photos\Sub-Folder\File.ext

or is it just "File.ext"?


Reply ↓  Report •

#2
May 15, 2020 at 13:08:54
No folder names. Simply the filename.

Reply ↓  Report •

#3
May 15, 2020 at 13:34:57
✔ Best Answer
Got this ugly powershell script you can give a try..
The logging is really bad, but it works tho, it will copy the files if the names match.

$PSScriptRoot is the folder the script is located in, so the log.txt will be in the same folder as the script.
Only have to change the paths with "E:\" in them if needed

Let me know how it goes if you test it

# Script below

Start-Transcript -Path $PSScriptRoot\log.txt -Append -Force -NoClobber

foreach ($Line in (Get-Content -Path "E:\familymovies.txt")) {

$File = Get-ChildItem -Path "E:\Photos" -Filter "*$Line*" -Recurse
$Path = $File.FullName
$Test = Test-Path $Path

if ($Test -eq $True) {

Move-Item -Path $Path -Destination "E:\Photos\Family-Movies" -Force
Write-Host "Moved file: $Path"

} else {

Write-Host "Not found: $Line"

}

}

Stop-Transcript

Pause

message edited by Kilavila


Reply ↓  Report •

Related Solutions

#4
May 15, 2020 at 14:39:15
If you just want to move all video files from these folders to that new folder there is a better way tho:

$Folder = Get-ChildItem "E:\Photos" -Recurse
New-Item "E:\log.txt"

foreach ($i in $Folder){
if ($i.Extension -eq '.avi' -or $i.Extension -eq '.mp4' -or $i.Extension -eq '.mov'){
$File = $i.FullName
Move-Item -Path $File -Destination "E:\Photos\Family-Movies"
Add-Content -Path "E:\log.txt" -Value "Copied file: $File to: E:\Photos\Family-Movies"
}
}

This will move all .avi, .mp4 and .mov files from E:\Photos to E:\Photos\Family-Movies


Reply ↓  Report •

#5
May 15, 2020 at 15:09:30
Is this a batch file?
It doesn't run.

message edited by GregSch


Reply ↓  Report •

#6
May 15, 2020 at 15:23:23
oh, no its powershell.
save it as .ps1 and run file with powershell.exe

Edit:
You can also run powershell, then drag and drop the script into powershell and press enter.

message edited by Kilavila


Reply ↓  Report •

#7
May 15, 2020 at 16:51:19
Can this batch file be modified to work?

@echo off & setlocal enabledelayedexpansion
type nul>e:\photos\log.txt
set log=e:\photos\log.txt
set list=familymovies.txt
set cnt=0
set number=0
for /f "delims=" %%a in ('type %list%') do (
set /A cnt +=1
set file!cnt!=%%a
)
E:
cd E:\Photos
:LOOP
if %number%==%cnt% exit /b
set /A number +=1
dir /s !file%number%! || echo.!file%number%!>> e:\photos\log.txt
Goto :LOOP


Reply ↓  Report •

#8
May 16, 2020 at 02:38:23
You can use batch, but i just know the basics tho as im not using batch scripts myself.

Reply ↓  Report •

#9
May 16, 2020 at 07:41:18
Thank you sooo much. I've installed Powershell and going to try.

Reply ↓  Report •

Ask Question