3

I've had a look around, and I found some stuff that kinda worked, but I'm new to all this batch stuff.

I'm looking for a batch script that will copy one folder and everything in it. The folder is called

Package Master

I then want it to make 1,000 copies of that folder and rename each one sequentially:

AE 0001

to

AE 1000

If anyone is able to help, it'd be great if they could explain the process too so I can learn how it comes together and put myself in a stronger position for tweaking later.

Hennes
  • 65,142
Kilm
  • 33
  • 1
    What did you already try? Did you use double % signs? – Hennes Aug 13 '15 at 08:53
  • I found some code which was able to generate the folders in CMD but, uh, lost the code and I'm on a work system that's fairly locked down, so I can't even check history to find the page I got it from and I can't find it again. :( – Kilm Aug 13 '15 at 08:56
  • Does this need to be a batch file specifically? It's probably much easier to accomplish in Powershell. – user Aug 13 '15 at 09:00
  • 2
    I'd say batch, just because I know I can use bat files - these systems are locked down really, really tightly and don't know what Powershell is or if it'd work on here. Can't install anything, limited rights to run things, etc. – Kilm Aug 13 '15 at 09:02

1 Answers1

7

How do I copy a folder 1000 times naming it "AE 0001" up to "AE 1000"

Use the following batch file:

@echo off
setlocal
setlocal EnableDelayedExpansion
rem loop 1000 times
for /l %%i in (1,1,1000) do (
  rem prefix the count with leading zeros
  set "_number=000%%i"
  rem recursively copy the directory and its contents
  rem target name is "AE " + 4 digit count with leading zeros
  xcopy "Package Master" "AE !_number:~-4!" /s /i
  )
endlocal

Is there a way to set it to start from a specific number and pick up from there?

So start at AE 0140 and climb from there for example?

Just change the for /l loop as follows:

for /l %%i in (140,1,1000) do (

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).
  • xcopy - Copy files and/or directory trees to another folder.
DavidPostill
  • 156,873
  • I get a parse error? – Kilm Aug 13 '15 at 10:17
  • Apologies, Was missing a " after "Package Master on the xcopy. Answer updated. – DavidPostill Aug 13 '15 at 10:38
  • That's amazing - you've literally shaved hours off of my working day and let me focus on my real job!! Is there a way to set it to start from a specific number and pick up from there? So start at AE 0140 and climb from there for example? – Kilm Aug 13 '15 at 10:48
  • Yes, of course. Answer updated. Please read the reference links. – DavidPostill Aug 13 '15 at 10:51
  • You're a gent - thanks a ton. I think I've done the right thing to accept your answer? – Kilm Aug 19 '15 at 11:34