Need some oldschool DOS help

notarat

2[H]4U
Joined
Mar 28, 2010
Messages
2,501
I'm using the following DOS command and I need to add an increment to it so I know which line of the filelist.txt input file has been processed.

for /f "delims=" %%i in (filelist.txt) do echo F|xcopy "K:\%%i" "D:\T2\%%i" /i /z /y

For those who don't understand that command, let me give you a general explanation:

K: is a Network Drive
D: is a Local Drive
Filelist.txt contains the full path and filename of every file on my K Drive that is > 6yrs old.

example of data in filelist.txt:
test.txt
folder1\test.doc
folder2\anothertest.xls
windows\system32\icon.dll
windows\temp\lkhsdfq3423.jpg
on and on and on and on....

The command looks into filelist.txt and reads the first line, and then uses that information in the xcopy command.

As a result, the file located at K:\mainfolder\subfolder2\subfolder3\filename1.doc is copied to D:\T2\mainfolder\subfolder2\subfolder3\filename1.doc

The command line switches aren't really that important but, if you want to know, they are explained below:

/i -- If the destination folder doesn't exist, and I'm copying more than 1 file to that destination folder, assume the destination is a file in that folder

/z -- Copies files in networked restartable mode (if server is rebooted, command starts where it left off when the server comes back up)

/y -- Suppress confirmation of copy process

I have the following example of a simple counter in a batch file I made earlier this morning:

@ECHO OFF
set /A Counter=1
echo Initial value of Counter: %Counter%
echo.
:START
DIR (Used in place of the command above)
set /A Counter+=1
echo Counter After Increment: %Counter%
echo.
GOTO START

Here's the problem: In this example the counter is incremented and displayed after the command is executed.

1 command executes (DIR)
2 counter incremented and displayed
3 batchfile loops back to label called START
4 repeat DIR command
5 increment and display counter
6 batchfile loops back to label called START (repeat, repeat, etc)

I need the counter to display and increment but the command itself never loops. It goes through the text file one line at a time. If I were to do something like:

@ECHO OFF
set /A Counter=1
echo Initial value of Counter: %Counter%
echo.
:START
for /f "delims=" %%i in (filelist.txt) do echo F|xcopy "D:\%%i" "D:\T2\%%i" /i /z /y
set /A Counter+=1
echo Counter After Increment: %Counter%
echo.
GOTO START

The counter would not increment and display until all files in the filelist.txt were processed.

How can I get the counter to increment after each line in the text file is processed?
 
Adding the following:

The reason for using this command in the first place is because I am forbidden from purchasing specialized software to do this archival process. (in case anyone is wondering)

I'm wondering if I'm on the right track with the following:

set /A Counter=1
echo Initial value of Counter: %Counter%
for /f "delims=" %%i in (filelist.txt) do echo counter|echo F|xcopy "D:\%%i" "D:\T2\%%i" /i /z /y|set /A Counter+=1|echo Counter Increment: %Counter%
 
Last edited:
A few versions back on, Microsoft added blocks to most commands. I am not sure if for was one, but you could try:

Code:
for /f "delims=" %5i in (filelist) do (first command
second command
.
.
last command)

If the for command supports blocks, then this should run through the commands between ( and ) for each file, or whatever your for command feeds it.

If it doesn't support it, then you could get creative with an if that would always return true. I know if supports blocks.

I would check it for you, but I am on my tablet and don't have access to my pc right now.
 
A few versions back on, Microsoft added blocks to most commands. I am not sure if for was one, but you could try:

Code:
for /f "delims=" %5i in (filelist) do (first command
second command
.
.
last command)

If the for command supports blocks, then this should run through the commands between ( and ) for each file, or whatever your for command feeds it.

If it doesn't support it, then you could get creative with an if that would always return true. I know if supports blocks.

I would check it for you, but I am on my tablet and don't have access to my pc right now.

Thanks mang. I'll give it a shot tomorrow.

I'm processing up to 836,000 files and it takes seemingly forever. I'm really hoping the counter works so I can at least see how much progress is achieved per day
 
I know you're used to this script, but have you looked at teracopy?
they have a free version.
 
I figured this one out for you, but -- you'll have to open your command prompt with a special parameter. You'll need to open it like this:

Code:
cmd.exe /v

If you don't use the /v to enable variable expansion during execution, the counter will actually error out because of the way that variables are processed in batch.

You should be able to easily modify this script that I wrote. My file (test.txt) is just a list of files in a text file. Probably similar to what you have, so you can just change the name:

Code:
:Init
  @Echo Off
  Set /A Counter=0
  Cls

:Main
  Echo Beginning File Operation
  Echo ===============================================================================
  for /f "delims=" %%i in (.\test.txt) do (
     Set /A Counter=Counter+1
     Echo [!Counter!] Processing File: %%i
  )

:End
  Echo ===============================================================================
  Echo Operation Copmleted Successfully
  Echo %Counter% files processed


The "gotcha" with this script is:
1. Have to open your shell with the /V prompt to enable execution time variable expansion
2. Have to use the ! instead of the % when referencing a variable to tell the shell that you want to expand it.

Do a set /? to read more on variable expansion -- it's down towards the bottom and could easily be glazed over if you weren't looking for it.

Hope it helps.
 
I figured this one out for you, but -- you'll have to open your command prompt with a special parameter. You'll need to open it like this:

Code:
cmd.exe /v
If you don't use the /v to enable variable expansion during execution, the counter will actually error out because of the way that variables are processed in batch.

You should be able to easily modify this script that I wrote. My file (test.txt) is just a list of files in a text file. Probably similar to what you have, so you can just change the name:

Code:
:Init
  @Echo Off
  Set /A Counter=0
  Cls

:Main
  Echo Beginning File Operation
  Echo ===============================================================================
  for /f "delims=" %%i in (.\test.txt) do (
     Set /A Counter=Counter+1
     Echo [!Counter!] Processing File: %%i
  )

:End
  Echo ===============================================================================
  Echo Operation Copmleted Successfully
  Echo %Counter% files processed
The "gotcha" with this script is:
1. Have to open your shell with the /V prompt to enable execution time variable expansion
2. Have to use the ! instead of the % when referencing a variable to tell the shell that you want to expand it.

Do a set /? to read more on variable expansion -- it's down towards the bottom and could easily be glazed over if you weren't looking for it.

Hope it helps.


Instead of running cmd.exe with the /v parameter you can just put the following line at the top of the batch file:

SETLOCAL ENABLEDELAYEDEXPANSION
 
Back
Top