Creating folder from file, copy initial file into folder and add prefix

浪尽此生 提交于 2019-12-13 23:47:59

问题


A folder should be created with file names after a torrent is finished. The files should be copied (not moved) and a prefix should be added. This is my actual .bat

for /F "Tokens=*" %%i in ('Dir /B *.mp4') do md "%%~ni"|copy "%%i" "%%~ni"

This works so far but I was not able to get a prefix added. That prefx should be added to the newly created file in the folder.

A kind of progress bar like "xx MB of yy MB at aa MB/s Speed" would be nice but not essential.


回答1:


Try this

for /f "tokens=*" %%A in ('dir /b *.mp4') do (
    md "%%~nA"
    copy "%%~fA" "%%~nA\prefix_%%~nxA"
)

This will copy abc.mp4 -> abc\prefix_abc.mp4

To output progress

@echo off
setlocal

set _cmd='dir /b *.mp4'
set _prefix=movie_

set _progress_width=40
set _progress_char1=+
set _progress_char2=-
set _progress_char3=+
set _progress_fill=*
set _count=0
set _i=1

rem  Counting files
for /f "tokens=*" %%A in (%_cmd%) do set /a "_count+=1"

call :print_scale

for /f "tokens=*" %%A in (%_cmd%) do (
    md "%%~nA" >nul 2>&1
    copy "%%~fA" "%%~nA\%_prefix%%%~nxA" >nul 2>&1

    rem  Output progress
    call :progress _i _count
    call title Completed [%%_i%%/%%_count%%]
    set /a "_i+=1"
)

endlocal
exit /b 0

:print_scale
set /a "_width=_progress_width-2"
set "_fill="
for /l %%B in (1,1,%_width%) do call set "_fill=%%_fill%%%%_progress_char2%%"
echo %_progress_char1%%_fill%%_progress_char3%
exit /b 0

:progress
call set _current=%%%1%%
call set _total=%%%2%%
set /a "_width=_progress_width"
set /a "_pos=_width*_current/_total-_width*(_current-1)/_total"
for /l %%B in (1,1,%_pos%) do echo|set /p _z=%_progress_fill%
exit /b 0

This will output progress like

+--------------------------------------+
*************


来源:https://stackoverflow.com/questions/33150045/creating-folder-from-file-copy-initial-file-into-folder-and-add-prefix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!