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) Given that `DestPath` is a variable, why are you referring to it as `DestPath` and not `%DestPath%`? (2) Given that `DestPath` is a variable that refers to an absolute pathname (begins with `C:\...`), why are you saying `..\DestPath`? (3) Why are you using `findstr`? (4) Have you tried saying `do echo %%A` to see what values `%%A` is getting? – Scott - Слава Україні Jun 23 '19 at 03:56
  • (1)+(2) corrected these (3) For each item > findstr - to search the logfile for a string across the multiple lines, then move each match to the destination path. I am probably on completely the wrong track here though. – StrangerThings Jun 23 '19 at 04:45
  • Some more thoughts: (5) You say “The relevant folders already exist within `C:\VIDEO` so do not require creation.”  This would appear to be inconsistent with the fact that you’re destroying and re-creating `C:\VIDEO` (a.k.a. ``%DestPath%``) at the beginning of your script.  (6) If the “log file” contains the names of the folders where you want the files to be moved to (and the relevant folders within `C:\VIDEO` already exist), then it seems that the batch file doesn’t need to know the destination path.  (7) Loop index variables are case-sensitive, so `%%a` is not the same as `%%A`. – Scott - Слава Україні Jun 23 '19 at 15:14
  • But, since you have put *some* effort into this, and nobody else is helping you, I’ll give you an answer. – Scott - Слава Україні Jun 23 '19 at 15:14
  • @Scott my poor understanding of the code unfortunately. I thought I was saying at the beginning to simply read and accept the Destination Path if it existed, but to Make Directory if it was missing (for any reason, although it should not be) – StrangerThings Jun 23 '19 at 15:35

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.

  • All pathnames are definitely absolute and are 2 dirs and a filename (e.g. C:\VIDEO\MyVideo.mkv). Also just thought of something else that is related. Is there a way to move to the items while keeping the previous/original modified date/time of the file/folder? Or maybe it is possible to include an extra line in the script to read these parameters from the file(s) first, then rewrite them to the file/folder? – StrangerThings Jun 23 '19 at 15:41
  • Well, `move` should retain the modification date/time of the files.  The others can be a bit trickier.  [This](https://superuser.com/q/146125/150988), [this](https://superuser.com/q/1054206/150988), [this](https://superuser.com/q/255691/150988), [this](https://superuser.com/q/109373/150988), and [this](https://superuser.com/q/393798/150988) suggest robocopy. – Scott - Слава Україні Jun 24 '19 at 02:13
  • Yes just looked more closely and that is correct. The date/time on the file is maintained, but the date/time on the folder gets updated. Is there maybe an extra line that could be added within the for loop after the move action, to read the properties of the date/time from the file and copy/write/overwrite this to the properties of the folder? In case you are wondering why, it matters because of sorting (as currently all folders get stuck with the same date/time). – StrangerThings Jun 24 '19 at 06:43
  • How can I change the code to use Robocopy for the move(s), so that date/time for the folders can be preserved? – StrangerThings Jun 25 '19 at 16:07
  • Please do more independent research (i.e., activities that *don’t* require any support from anybody else).  As I pointed out earlier, it looked like you didn’t put much effort into your code.  (For example, “findstr – to search the logfile for a string” doesn’t make any sense; you were doing ``findstr "^"``, which “finds” every line.  I get the feeling that you just copied that code from somewhere, without trying hard to understand it.)  This is probably why nobody else helped you.  I helped you only because I was feeling generous that day.  … (Cont’d) – Scott - Слава Україні Jun 25 '19 at 17:25
  • (Cont’d) …  Now I’ve given you a program name and five links.  Have you read the questions I linked to?  Have you done a search for ‘‘robocopy’’ and read its documentation?  Try to solve your problem yourself.  After you have tried something and hit a roadblock, come back, show us what you tried, explain why you got stuck, and ask for help. – Scott - Слава Україні Jun 25 '19 at 17:25
  • I have read all the links and everything within. Some suggest Robocopy works, some say it does not. A simple "Robocopy C:\Temp C:\VIDEO /A" _may_ work in cmd line, but this is within a script using variables. Even if it is something more complex like "Robocopy C:\Temp C:\VIDEO /A-:SH /DCOPY:T /COPYALL /E /R:0 /ZB /ETA /TEE /V /FP /XD" that cannot be used directly in script. – StrangerThings Jun 25 '19 at 17:47