我有一个* .txt文件,包含要移动的很长文件列表(大约30k),每行包含要以完整路径移动的文件的文件名和包含完整路径的目标文件夹字符串 to
。
列出文件示例:
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.
如何逐行处理此列表文件以将所有文件移动到指定文件夹?
答案 0 :(得分:0)
最简单的方法是在文本编辑器中打开列表文件,支持Perl正则表达式查找/替换并运行Perl正则表达式从搜索字符串^(.+?) to (.+),$
替换所有文件顶部并替换字符串{ {1}}。结果将是:
@move "\1" "\2"
然后修改后的列表文件可以保存下来,例如用ANSI编码的@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\"
并双击它执行,或者在命令提示符窗口中查看,以防文件无法显示因各种原因被移动。
但是,这是处理列表文件MoveFiles.bat
的此任务的小批处理文件。
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
call /?
echo /?
endlocal /?
for /?
goto /?
if /?
if /?
move /?
rem /?
set /?
另见: