#31 12 Apr 2018 14:39

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,042

Re: Progress Indicator For Robocopy File

Thanks Shadow Thief.

This is quite the blast from the past.  Looking at the script, I think the backspace belongs here:

call set "bar.del=%%bar.back:%bar.backchar%=%%"

So, to (hopefully) fix this, add Shadow Thief's code above at the start of the script (shortly after @echo off) and change the above line to:

call set "bar.del=%%bar.back:%bar.backchar%=%BS%%%"

No promises; there might be other places it's needed, but i'm not near a Windows machine to test this out.

Last edited by bluesxman (12 Apr 2018 16:26)


cmd | *sh | Ruby | Chef | Puppet | PowerShell | AutoIT3

Offline

#32 17 Apr 2018 18:21

redeemer666
Member
Registered: 11 Apr 2018
Posts: 8

Re: Progress Indicator For Robocopy File

Thank you both of you guys, will try it out ASAP and let you know here. If this work, I will make a statue of you both smile

Offline

#33 18 Apr 2018 07:29

redeemer666
Member
Registered: 11 Apr 2018
Posts: 8

Re: Progress Indicator For Robocopy File

Here I am again,

THANK YOU GUYS, it works flawlessly (except for the very first "dot" but who cares smile).

Following Chimaera post, you have to put the backslash variable also here:

for /l %%a in (1,1,%bar.position%) do call set "bar=%bar.char%%%bar%%%BS%" otherwise the bar will grow longer and longer.

Thanks again guys, you simply are THE BEST.

Offline

#34 18 Apr 2018 12:30

lohmann-k
New Member
Registered: 18 Apr 2018
Posts: 3

Re: Progress Indicator For Robocopy File

Dear all,
i try to get it work under win10.

Robocopy works fine, but but my progress bar still look as:

[-**********************************************************

so i can´t see any progress

and only the CMD-Window Title change to "0 / -1" - i still see only one and same Window

What do i wrong? hmm

My Code:

@echo off
REM add for Backspace:
for /F %%A in ('echo prompt $H ^| cmd') do set "BS=%%A"

REM ---set "source=t:\ME_SHARE"
set "source=g:\dell"
set "target=d:\_test"
set "rc=robocopy.exe"

set "rc.command=%rc% "%source%." "%target%." /s"

REM ---SET _robo_vista_1=/COPY:DAT /S /B /XJD /XJF /R:0 /W:0 /XA:SH /NJH /XF %_exc_vista_files% %_exc_prog_files% /XD %target% %_exc_vista_folder% %_exc_prog_folder% %_exc_special_folder%
REM ---set "rc.command=%rc% "%source%." "%target%." "%_back_what%" %_robo_vista_1%"

REM make the bar as wide as possible
REM ---for /f "usebackq tokens=2" %%a in (`mode con ^| find "Columns:"`) do set /a bar.width=%%a - 3

REM or you can force the width on the next line (no sanity checking is done on this value)
set "bar.width=50"
set "bar.char=*"
set "bar.backchar=-"
set "bar.size=0"
set "bar.back="
set "bar.del="
set "bar.position=0"
set "bar.position.modifier=0"
set "bar.check=0"
set "loop.delay=3"

set "done=0"
set "total=-1"
set "abort="

set "window.title=%rc%_%date%_%time: =0%"

echo:START

REM spawn the robocopy command with a (hopefully) unique window title that we'll need later
start /min "%window.title%" %rc.command%

REM find the total number of files, so we can shrink the bar to fit the total, if necessary
for /f "usebackq tokens=3,4,5 delims= " %%a in (`%rc.command% /l /njh /nfl /ndl ^| find "Files"`) do (
    set /a "total=%%a,done=%%c"
)

set /a "bar.check=bar.width / total" 2>nul

if %bar.check% EQU 1 set /a bar.width=total

REM draw the empty bar 
for /l %%a in (1,1,%bar.width%) do (call set "bar.back=%%bar.back%%%bar.backchar%")
call set "bar.del=%%bar.back:%bar.backchar%=%BS%%%"
set /p "out=[%bar.back%]%bar.del%" <nul

set /a loop.delay+=1

title Please stand by...

call :loop

echo:

if defined abort (echo:ABORT) ELSE (echo:END)

pause

goto :EOF
:::::::::::::::::::::::::::::::::::::::::::::::::::::
:loop
:::::::::::::::::::::::::::::::::::::::::::::::::::::

set bar=

REM if all the files have been copied, draw a full bar (in case it didn't get filled
REM on the previous iteration) and exit
if %done% GEQ %total% (
    title %done% / %total%
    for /l %%a in (1,1,%bar.width%) do call set "bar=%bar.char%%%bar%%%BS%"
    call set /p "out=%%bar%%" <nul
    set abort=
    goto :EOF
)

REM if the robocopy child process wasn't running on the previous iteration and there
REM are still files left uncopied then we assume that robocopy died unexpectedly
if defined abort goto :EOF

REM check for the robocopy child process (using out "unique" window title)
tasklist /fi "imagename eq robocopy.exe" /fi "windowtitle eq %window.title%" /fo csv /nh 2>nul | findstr "." >nul

REM if it's not found, set a flag (it'll be dealt with on the next interation)
if errorlevel 1 set abort=1 & set loop.delay=1

REM run a duplicate robocopy process with the "/L" switch to so we can extract
REM the total number of files and those that have been copied from the output
for /f "usebackq tokens=3,4,5 delims= " %%a in (`%rc.command% /l /njh /nfl /ndl ^| find "Files"`) do (
    set /a "remain=%%b,done=%%c"
)

REM figure out (roughly) how many files need to be copied to increase the progress
REM bar by one step
set /a bar.step=(total / bar.width) - 1

REM in case its less than one...
if %bar.step% LEQ 0 set bar.step=1

REM calculate the bar modifier, which takes effect if the total number of files
REM is significantly lower than the bar width.
set /a bar.position.modifier=bar.width / total
if %bar.position.modifier% LEQ 0 set /a bar.position.modifier=1

REM calculate the position using the number of copied files and the step value
set /a bar.position=(done / bar.step) * bar.position.modifier

REM if for some reason the position is greater than the width, fix it
REM (this would occur if the number of files is not much more than
REM the defined bar width)
if %bar.position% GTR %bar.width% set /a bar.position=bar.width

REM draw the bar (we're redrawing the whole thing on each interation)
for /l %%a in (1,1,%bar.position%) do call set "bar=%bar.char%%%bar%%%BS%"

set /p "out=%bar%" <nul 

title %done% / %total%

REM delay before interating so that the script doesn't thrash the system
ping 127.1 -n %loop.delay% -w 1000 >nul

goto :loop
:::::::::::::::::::::::::::::::::::::::::::::::::::::

Offline

#35 18 Apr 2018 12:53

redeemer666
Member
Registered: 11 Apr 2018
Posts: 8

Re: Progress Indicator For Robocopy File

Hello Lohmann

Remove the %BS% on this line "for /l %%a in (1,1,%bar.width%) do call set "bar=%bar.char%%%bar%%%BS%"" and try again

Offline

#36 18 Apr 2018 12:58

redeemer666
Member
Registered: 11 Apr 2018
Posts: 8

Re: Progress Indicator For Robocopy File

If someone can explain me why the first dot remain "stuck", and the bar cover the closed parenthesys at the end fo the process (]) it will be perfect. I have tried to study the process but can't find any clue about this little issue

Offline

#37 19 Apr 2018 16:52

redeemer666
Member
Registered: 11 Apr 2018
Posts: 8

Re: Progress Indicator For Robocopy File

OK guys,

here I am bothering you again. I need a little help, since I've tried everything with no luck:

there is a way to bring onto the main script (the one with the bar) the exit code of the robocopy command?

I have this script that make copies of some folders into an external HD, and before I added your code into it I had a sort of "translator" of the robocopy exit codes that I used to save into a log file. Of course, now that robocopy runs on an external cmd window, this doesn't work anymore.

I have tried enabling the delayed extensions and piping with (&) the %rc_command% with a !errorlevel! saved into a file, but it saves the exit code of the start command, that is always 0.

Any idea?

Offline

#38 19 Apr 2018 16:53

lohmann-k
New Member
Registered: 18 Apr 2018
Posts: 3

Re: Progress Indicator For Robocopy File

Hi redeemer666,

thanks for your help.
I have implemented your changes, but i can't still see progress in the bar.

My first window looks like:

Window titel: 0/-1

START
[-***********************************************************
END

and it all comes immidietly after START an i can´t see any progress.

My child (robocopy) window has titel: robocopy.exe_19.04.2018_17:05:00,99

My current code of copy.bat:

@echo off
REM add for Backspace:
for /F %%A in ('echo prompt $H ^| cmd') do set "BS=%%A"

REM ---set "source=t:\ME_SHARE"
set "source=b:\infos"
set "target=d:\infos"
set "rc=robocopy.exe"

set "rc.command=%rc% "%source%." "%target%." /s"

REM ---SET _robo_vista_1=/COPY:DAT /S /B /XJD /XJF /R:0 /W:0 /XA:SH /NJH /XF %_exc_vista_files% %_exc_prog_files% /XD %target% %_exc_vista_folder% %_exc_prog_folder% %_exc_special_folder%
REM ---set "rc.command=%rc% "%source%." "%target%." "%_back_what%" %_robo_vista_1%"

REM make the bar as wide as possible
REM ---for /f "usebackq tokens=2" %%a in (`mode con ^| find "Columns:"`) do set /a bar.width=%%a - 3

REM or you can force the width on the next line (no sanity checking is done on this value)
set "bar.width=50"
set "bar.char=*"
set "bar.backchar=-"
set "bar.size=0"
set "bar.back="
set "bar.del="
set "bar.position=0"
set "bar.position.modifier=0"
set "bar.check=0"
set "loop.delay=3"

set "done=0"
set "total=-1"
set "abort="

set "window.title=%rc%_%date%_%time: =0%"

echo:START

REM spawn the robocopy command with a (hopefully) unique window title that we'll need later
start /min "%window.title%" %rc.command%

REM find the total number of files, so we can shrink the bar to fit the total, if necessary
for /f "usebackq tokens=3,4,5 delims= " %%a in (`%rc.command% /l /njh /nfl /ndl ^| find "Files"`) do (
    set /a "total=%%a,done=%%c"
)

set /a "bar.check=bar.width / total" 2>nul

if %bar.check% EQU 1 set /a bar.width=total

REM draw the empty bar 
for /l %%a in (1,1,%bar.width%) do (call set "bar.back=%%bar.back%%%bar.backchar%")
call set "bar.del=%%bar.back:%bar.backchar%=%BS%%%"
set /p "out=[%bar.back%]%bar.del%" <nul

set /a loop.delay+=1

title Please stand by...

call :loop

echo:

if defined abort (echo:ABORT) ELSE (echo:END)

pause

goto :EOF
:::::::::::::::::::::::::::::::::::::::::::::::::::::
:loop
:::::::::::::::::::::::::::::::::::::::::::::::::::::

set bar=

REM if all the files have been copied, draw a full bar (in case it didn't get filled
REM on the previous iteration) and exit
if %done% GEQ %total% (
    title %done% / %total%
    for /l %%a in (1,1,%bar.width%) do call set "bar=%bar.char%%%bar%%"
    call set /p "out=%%bar%%" <nul
    set abort=
    goto :EOF
)

REM if the robocopy child process wasn't running on the previous iteration and there
REM are still files left uncopied then we assume that robocopy died unexpectedly
if defined abort goto :EOF

REM check for the robocopy child process (using out "unique" window title)
tasklist /fi "imagename eq robocopy.exe" /fi "windowtitle eq %window.title%" /fo csv /nh 2>nul | findstr "." >nul

REM if it's not found, set a flag (it'll be dealt with on the next interation)
if errorlevel 1 set abort=1 & set loop.delay=1

REM run a duplicate robocopy process with the "/L" switch to so we can extract
REM the total number of files and those that have been copied from the output
for /f "usebackq tokens=3,4,5 delims= " %%a in (`%rc.command% /l /njh /nfl /ndl ^| find "Files"`) do (
    set /a "remain=%%b,done=%%c"
)

REM figure out (roughly) how many files need to be copied to increase the progress
REM bar by one step
set /a bar.step=(total / bar.width) - 1

REM in case its less than one...
if %bar.step% LEQ 0 set bar.step=1

REM calculate the bar modifier, which takes effect if the total number of files
REM is significantly lower than the bar width.
set /a bar.position.modifier=bar.width / total
if %bar.position.modifier% LEQ 0 set /a bar.position.modifier=1

REM calculate the position using the number of copied files and the step value
set /a bar.position=(done / bar.step) * bar.position.modifier

REM if for some reason the position is greater than the width, fix it
REM (this would occur if the number of files is not much more than
REM the defined bar width)
if %bar.position% GTR %bar.width% set /a bar.position=bar.width

REM draw the bar (we're redrawing the whole thing on each interation)
for /l %%a in (1,1,%bar.position%) do call set "bar=%bar.char%%%bar%%%BS%"

set /p "out=%bar%" <nul 

title %done% / %total%

REM delay before interating so that the script doesn't thrash the system
ping 127.1 -n %loop.delay% -w 1000 >nul

goto :loop
:::::::::::::::::::::::::::::::::::::::::::::::::::::

Offline

#39 19 Apr 2018 16:58

redeemer666
Member
Registered: 11 Apr 2018
Posts: 8

Re: Progress Indicator For Robocopy File

Hi Lohmann,

I think you are not on an English PC (same as me). I've solved a similar issue changing this line

tasklist /fi "imagename eq robocopy.exe" /fi "windowtitle eq %window.title%" /fo csv /nh 2>nul | findstr "." >nul

instead of "." (dot), try "," (comma)

Tomorrow I will check my script and see if I have made some other "refinements"

Regards

Offline

#40 19 Apr 2018 17:00

lohmann-k
New Member
Registered: 18 Apr 2018
Posts: 3

Re: Progress Indicator For Robocopy File

Hi again,
after implementation of code changes from thread #8 (bluesxman) it looks like follow:

START
[----------------------------------------------------------]

I still can´t see the progress (any changes in the bar) and the "END" does not come
though robocopy window is closed.

My current code of copy1.bat:

@echo off
REM add for Backspace:
for /F %%A in ('echo prompt $H ^| cmd') do set "BS=%%A"

REM ---set "source=t:\ME_SHARE"
set "source=b:\infos"
set "target=d:\infos"
set "rc=robocopy.exe"

set "rc.command=%rc% "%source%." "%target%." /s"

REM ---SET _robo_vista_1=/COPY:DAT /S /B /XJD /XJF /R:0 /W:0 /XA:SH /NJH /XF %_exc_vista_files% %_exc_prog_files% /XD %target% %_exc_vista_folder% %_exc_prog_folder% %_exc_special_folder%
REM ---set "rc.command=%rc% "%source%." "%target%." "%_back_what%" %_robo_vista_1%"

REM make the bar as wide as possible
REM ---for /f "usebackq tokens=2" %%a in (`mode con ^| find "Columns:"`) do set /a bar.width=%%a - 3

REM or you can force the width on the next line (no sanity checking is done on this value)
set "bar.width=50"
set "bar.char=*"
set "bar.backchar=-"
set "bar.size=0"
set "bar.back="
set "bar.del="
set "bar.position=0"
set "bar.position.modifier=0"
set "bar.check=0"
set "loop.delay=3"

set "done=0"
set "total=1"
set "get.total=1"
set "abort="

set "window.title=%rc%_%date%_%time: =0%"

echo:START

REM spawn the robocopy command with a (hopefully) unique window title that we'll need later
start /min "%window.title%" %rc.command%

REM find the total number of files, so we can shrink the bar to fit the total, if necessary
for /f "usebackq tokens=3,4,5 delims= " %%a in (`%rc.command% /l /njh /nfl /ndl ^| find "Files"`) do (
    set /a "total=%%a,done=%%c"
)

set /a "bar.check=bar.width / total" 2>nul

if %bar.check% EQU 1 set /a bar.width=total

REM draw the empty bar 
for /l %%a in (1,1,%bar.width%) do (call set "bar.back=%%bar.back%%%bar.backchar%")
call set "bar.del=%%bar.back:%bar.backchar%=%BS%%%"
set /p "out=[%bar.back%]%bar.del%" <nul

set /a loop.delay+=1

title Please stand by...

call :loop

echo:

if defined abort (echo:ABORT) ELSE (echo:END)

pause

goto :EOF
:::::::::::::::::::::::::::::::::::::::::::::::::::::
:loop
:::::::::::::::::::::::::::::::::::::::::::::::::::::

set bar=

REM if all the files have been copied, draw a full bar (in case it didn't get filled
REM on the previous iteration) and exit
if %done% GEQ %total% (
    title %done% / %total%
    for /l %%a in (1,1,%bar.width%) do call set "bar=%bar.char%%%bar%%"
    call set /p "out=%%bar%%" <nul
    set abort=
    goto :EOF
)

REM if the robocopy child process wasn't running on the previous iteration and there
REM are still files left uncopied then we assume that robocopy died unexpectedly
if defined abort goto :EOF

REM check for the robocopy child process (using out "unique" window title)
tasklist /fi "imagename eq robocopy.exe" /fi "windowtitle eq %window.title%" /fo csv /nh 2>nul | findstr "." >nul

REM if it's not found, set a flag (it'll be dealt with on the next interation)
if errorlevel 1 set abort=1 & set loop.delay=1

REM run a duplicate robocopy process with the "/L" switch to so we can extract
REM the total number of files and those that have been copied from the output
for /f "usebackq tokens=3,4,5 delims= " %%a in (`%rc.command% /l /njh /nfl /ndl ^| find "Files"`) do (
    if defined get.total (set /a total=%%a&set get.total=)
    set /a remain=%%b,done=%%c
)

REM figure out (roughly) how many files need to be copied to increase the progress
REM bar by one step
set /a bar.step=(total / bar.width) - 1

REM in case its less than one...
if %bar.step% LEQ 0 set bar.step=1

REM calculate the bar modifier, which takes effect if the total number of files
REM is significantly lower than the bar width.
set /a bar.position.modifier=bar.width / total
if %bar.position.modifier% LEQ 0 set /a bar.position.modifier=1

REM calculate the position using the number of copied files and the step value
set /a bar.position=(done / bar.step) * bar.position.modifier

REM if for some reason the position is greater than the width, fix it
REM (this would occur if the number of files is not much more than
REM the defined bar width)
if %bar.position% GTR %bar.width% set /a bar.position=bar.width

REM draw the bar (we're redrawing the whole thing on each interation)
for /l %%a in (1,1,%bar.position%) do call set "bar=%bar.char%%%bar%%%BS%"

set /p "out=%bar%" <nul 

title %done% / %total%

REM delay before interating so that the script doesn't thrash the system
ping 127.1 -n %loop.delay% -w 1000 >nul

goto :loop
:::::::::::::::::::::::::::::::::::::::::::::::::::::

Could you please post your working code?

Greetings
lohmann

Offline

#41 20 Apr 2018 07:05

redeemer666
Member
Registered: 11 Apr 2018
Posts: 8

Re: Progress Indicator For Robocopy File

Hello Lohmann,

here you are:

@echo off
set "source=YOUR SOURCE HERE"
set "target=YOUR DESTINATION HERE"
set "rc=robocopy.exe"
for /F %%A in ('echo prompt $H ^| cmd') do set "BS=%%A"

set "rc.command=%rc% "%source%." "%target%." /s"

REM make the bar as wide as possible
for /f "usebackq tokens=2" %%a in (`mode con ^| find "Columns:"`) do set /a bar.width=%%a - 3

REM or you can force the width on the next line (no sanity checking is done on this value)
REM set "bar.width=80"
set "bar.char=þ"
set "bar.backchar=ú"
set "bar.size=0"
set "bar.back="
set "bar.del="
set "bar.position=0"
set "bar.position.modifier=0"
set "bar.check=0"
set "loop.delay=3"

set "done=0"
set "total=-1"
set "abort="

set "window.title=%rc%_%date%_%time: =0%"

echo:START

REM spawn the robocopy command with a (hopefully) unique window title that we'll need later
start "%window.title%" /min %rc.command%

REM find the total number of files, so we can shrink the bar to fit the total, if necessary
for /f "usebackq tokens=3,4,5 delims= " %%a in (`%rc.command% /l /njh /nfl /ndl ^| find "Files"`) do (
    set /a "total=%%a,done=%%c"
)

set /a "bar.check=bar.width / total" 2>nul

if %bar.check% EQU 1 set /a bar.width=total

REM draw the empty bar 
for /l %%a in (1,1,%bar.width%) do (call set "bar.back=%%bar.back%%%bar.backchar%")
call set "bar.del=%%bar.back:%bar.backchar%=%BS%%%"
set /p "out=[%bar.back%]%bar.del%" <nul

set /a loop.delay+=1

title Please stand by...

call :loop

echo:

if defined abort (echo:ABORT) ELSE (echo:END)

pause

goto :EOF
:::::::::::::::::::::::::::::::::::::::::::::::::::::
:loop
:::::::::::::::::::::::::::::::::::::::::::::::::::::

set bar=

REM if all the files have been copied, draw a full bar (in case it didn't get filled
REM on the previous iteration) and exit
if %done% GEQ %total% (
    title %done% / %total%
    for /l %%a in (1,1,%bar.width%) do call set "bar=%bar.char%%%bar%%"
    call set /p "out=%%bar%%" <nul
    set abort=
    goto :EOF
)

REM if the robocopy child process wasn't running on the previous iteration and there
REM are still files left uncopied then we assume that robocopy died unexpectedly
if defined abort goto :EOF

REM check for the robocopy child process (using out "unique" window title)
tasklist /fi "imagename eq robocopy.exe" /fi "windowtitle eq %window.title%" /fo csv /nh 2>nul | findstr "," >nul

REM if it's not found, set a flag (it'll be dealt with on the next interation)
if errorlevel 1 set abort=1 & set loop.delay=1

REM run a duplicate robocopy process with the "/L" switch to so we can extract
REM the total number of files and those that have been copied from the output
for /f "usebackq tokens=3,4,5 delims= " %%a in (`%rc.command% /l /njh /nfl /ndl ^| find "Files"`) do (
    set /a "remain=%%b,done=%%c"
)

REM figure out (roughly) how many files need to be copied to increase the progress
REM bar by one step
set /a bar.step=(total / bar.width) - 1

REM in case its less than one...
if %bar.step% LEQ 0 set bar.step=1

REM calculate the bar modifier, which takes effect if the total number of files
REM is significantly lower than the bar width.
set /a bar.position.modifier=bar.width / total
if %bar.position.modifier% LEQ 0 set /a bar.position.modifier=1

REM calculate the position using the number of copied files and the step value
set /a bar.position=(done / bar.step) * bar.position.modifier

REM if for some reason the position is greater than the width, fix it
REM (this would occur if the number of files is not much more than
REM the defined bar width)
if %bar.position% GTR %bar.width% set /a bar.position=bar.width

REM draw the bar (we're redrawing the whole thing on each interation)
for /l %%a in (1,1,%bar.position%) do call set "bar=%bar.char%%%bar%%%BS%"

set /p "out=%bar%" <nul 

title %done% / %total%

REM delay before interating so that the script doesn't thrash the system
ping 127.1 -n %loop.delay% -w 1000 >nul

goto :loop
:::::::::::::::::::::::::::::::::::::::::::::::::::::

Offline

Board footer

Powered by FluxBB