Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
248 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this

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

This will copy abc.mp4 -> abcprefix_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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...