0

I have a logfile.txt that contains the following lines of text:

C:\VIDEO\My Video 1\My Video 1.mkv
C:\VIDEO\MyVideo2\MyVideo2.mkv
C:\VIDEO\My.Video.3\My.Video.3.mkv

and a folder C:\Temp that contains the files:

My Video 1.mkv
MyVideo2.mkv
My.Video.3.mkv

I need to use the logfile.txt to match the respective filename.mkv that is listed in the logfile, then move the file to its correct folder (as shown in the logfile).

E.g., My Video 1.mkv should be moved to C:\VIDEO\My Video 1\

The relevant folders already exist within C:\VIDEO and so do not require creation.

How can I write a batch file for this?

I am starting off right now with something like this, but I am now stuck:

@echo off
setlocal enableDelayedExpansion
set "logfile=C:\Temp\logfile.txt"
set "SourcePath=C:\Temp"
set "DestPath=C:\VIDEO"

if exist ..\%DestPath%\nul rd /s /q ..\%DestPath%
if not exist ..\%DestPath%\nul md ..\%DestPath%

for /f "delims=: tokens=1*" %%A in ('findstr /n "^" "%logfile%"') do move "%SourcePath%\%%a" "%DestPath%"

Can someone please assist?

1 Answers1

0

Try this:

@echo off
set "logfile=C:\Temp\logfile.txt"
set "SourcePath=C:\Temp"
REM We don't need to set DestPath, because we don’t use it.
REM set "DestPath=C:\VIDEO"
for /f "delims=\ tokens=1-4 usebackq" %%A in ("%logfile%") do (
    move "%SourcePath%\%%D" "%%A\%%B\%%C\%%D"
)

You need to split the pathnames apart at backslashes (\).
So, for example, C:\VIDEO\My Video 1\My Video 1.mkv will be split into

%%A = C:
%%B = VIDEO
%%C = My Video 1
%%D = My Video 1.mkv

Since the file name is in %%D we move from "%SourcePath%\%%D".
And we move to "%%A\%%B\%%C\%%D", because that’s the destination path/filename, reassembled.

This assumes that all the path/filenames in your “logfile” are all absolute pathnames (beginning with a drive letter and a \) that are exactly three segments long (i.e., two directories and a filename).  If they are variable-length, this becomes more difficult.