1

I want to do some thing simple (I thought...)

I've got something like this:

Folder01 
  |_File1
  |_File2
  |_File3
Folder02
  |_File1
  |_File2
  |_File3
Folder03
  |_File1
  |_File2
  |_File3
Folder04
  |_File1
  |_File2
  |_File3

I just want to copy these Data with robocopy to a target (because I need a logfile and have to specify with attributes to copy).

But due to some limitations I can not copy the data in one run and have to split this Job (in real about 5000 Folders, 3TB).

So I want to specify the "starting folder" and the last folder like Folder00X (Folder001, +1, Folder100 ; the Job should start with Folder001 and with steps +1 it should copy the Folder002, Folder003....Folder100).

0

The job should start with Folder001, step +1, copy Folder002, ... Folder100

You can use a for /l loop in a batch file to do this.

The following batch file echoes the required list of folder names:

@echo off
setlocal enabledelayedexpansion
for /l %%i in (1,1,100) do (
   rem prefix the count with leading zeros
   set "_number=00%%i"
   rem extract the last 3 digits
   echo Folder!_number:~-3!
   )
endlocal

Notes:

  • The batch file should be located in the directory one level above Folder001
  • Replace the echo command with a robocopy command that does the copy for you.
  • To process names with 4 digits (for example Folder1000, ... Folder 2000) you need to change the batch file.
    • Replace (1,1,100) with (1000,1,2000)
    • Replace "_number=00%%i" with "_number=000%%i"
    • Replace Folder!_number:~-3! with Folder!_number:~-4!

Syntax

FOR /L %%parameter IN (start,step,end) DO command 

Key

  • start : The first number
  • step : The amount by which to increment the sequence
  • end : The last number

Source - for /l


Further Reading

  • An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
  • enabledelayedexpansion - Delayed Expansion will cause variables to be expanded at execution time rather than at parse time.
  • for /l - Conditionally perform a command for a range of numbers.
  • set - Display, set, or remove CMD environment variables. Changes made with SET will remain only for the duration of the current CMD session.
  • variables - Extract part of a variable (substring).

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.