Increment Variable

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Posts: 2
Joined: 16 Jun 2008 08:20

Increment Variable

#1 Post by jem00 » 18 Jun 2008 18:10

Hey guys,

Is it possible to increment the value of a variable during an xcopy?

For example:

Code: Select all

set /a counter = 1
xcopy /d /e/ h/ y c:\folder c:\anotherFolder

There are 100 files in the folder.
For the above example is it possible to increment the value of counter after one file has copied? so 1 out of 100?

Any advice is welcome

Posts: 12
Joined: 30 Jun 2008 02:50
Location: My computer


#2 Post by Thebetr1 » 30 Jun 2008 03:29

You cant.

you cant use a command while you are using another command.

just use dir "blabla" >> file.txt and count the number of lines.

if anyone knows a way to do this let me know :D

Posts: 239
Joined: 18 Feb 2006 19:54

#3 Post by DosItHelp » 03 Jul 2008 22:41


The /L option of XCOPY lists the files that would be copied. You can iterate this list using a FOR command and copy and count each item as you go, like this:

Code: Select all

@echo off

set "sourcedir=c:\folder"
set "targetdir=c:\anotherFolder"
set xcopyoptions=/d /e /h /y

rem -- count the files, show progress in window title
set /a cnt=0
for /f "tokens=*" %%A in ('"xcopy %xcopyoptions% /L "%sourcedir%" "%targetdir%""') do (
    set /a cnt+=1
    title counting files to copy: !cnt!

rem -- copy the files, show progress in window title
for /f "tokens=*" %%A in ('"xcopy %xcopyoptions% /L "%sourcedir%" "%targetdir%""') do (
    title Copying files, remaining: !cnt!
    echo. -- xcopy %xcopyoptions% "%%A" "%targetdir%"
    set /a cnt-=1
title done!

DosItHelp? :wink:

Post Reply