1

I have a *.txt file that consist of a very long list of files (about 30k) to be moved and where each line contains the file name of the file to move with full path and the target folder with full path separated by the string  to .

List file example:

C:\USER\BDG\anto\12.jpg to D:\USER\BDG\,
C:\USER\SMG\kent\311.jpg to D:\USER\SMG\,
C:\USER\JKT\lydia\13121.jpg to D:\USER\JKT\,
C:\USER\NYC\tiffany\1e1b1.jpg to D:\USER\NYC\,
C:\USER\MNC\regent\1eb1be1.jpg to D:\USER\MNC\,
etc.

How to process this list file line by line to move all the files to specified folder?

Mofi
  • 46,139
  • 17
  • 80
  • 143
Robin Gui
  • 11
  • 4

1 Answers1

0

The easiest method would have been opening the list file in a text editor with support for Perl regular expression find/replace and running a Perl regular expression replace all from top of file with search string ^(.+?) to (.+),$ and replace string @move "\1" "\2". The result would have been:

@move "C:\USER\BDG\anto\12.jpg" "D:\USER\BDG\"
@move "C:\USER\SMG\kent\311.jpg" "D:\USER\SMG\"
@move "C:\USER\JKT\lydia\13121.jpg" "D:\USER\JKT\"
@move "C:\USER\NYC\tiffany\1e1b1.jpg" "D:\USER\NYC\"
@move "C:\USER\MNC\regent\1eb1be1.jpg" "D:\USER\MNC\"

Then modified list file could have been saved next for example as MoveFiles.bat with ANSI encoding and executed with double clicking on it, or from within a command prompt window to see possible error messages in case of a file could not be moved for various reasons.

However, here is a small batch file for this task processing the list file ListFile.txt.

@echo off
if not exist "ListFile.txt" goto :EOF

setlocal EnableExtensions DisableDelayedExpansion
set "MoveError=0"

for /F "usebackq eol=| delims=" %%L in ("ListFile.txt") do call :ProcessFile "%%L"

if %MoveError% == 1 echo/ & pause
endlocal
goto :EOF

:ProcessFile
set "ListLine=%~1"

rem Remove comma at end of line if there is one at all.
if "%ListLine:~-1%" == "," set "ListLine=%ListLine:~0,-1%"

rem Replace " to " by a vertical bar being an invalid character in a file
rem name. The string " to " exists hopefully only once in each line.
set "ListLine=%ListLine: to =|%"

rem Split up the line into two substrings using | as delimiter.
rem First string is the file to move, second the target directory.
rem Execute the file move and output an error message on failure.
for /F "tokens=1,2 eol=| delims=|" %%I in ("%ListLine%") do (
    move "%%~I" "%%~J" >nul 2>&1
    if not errorlevel 1 goto :EOF

    if %MoveError% == 1 echo/
    echo ERROR: Failed to move the file
    echo        "%%~I"
    echo        to "%%~J"
    set "MoveError=1"
)
goto :EOF

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • call /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • if /?
  • move /?
  • rem /?
  • set /?
  • setlocal /?

See also:

Mofi
  • 46,139
  • 17
  • 80
  • 143