q0987
q0987

Reputation: 35982

How to use batch for loop to create auto-incremental files?

I need to create a huge number of files with the following command

fsutil file createnew h000001.cpp 1000 # create a file with 1K size

Question> How to use the for loop so that I can iterate a sequence of number so that I can create files named from h000001.cpp to h999999.cpp?

Thank you

Upvotes: 0

Views: 936

Answers (2)

Aacini
Aacini

Reputation: 67216

@echo off
setlocal EnableDelayedExpansion
for /L %%i in (1,1,999999) do (
    set n=00000%%i
    fsutil file createnew h!n:~-6!.cpp 1000
)

Upvotes: 2

Murray McDonald
Murray McDonald

Reputation: 631

This should work -- i tested it using echo "hello there" > h!n!.cpp with a max loop of 150 and it created files h000001.cpp to h000150.cpp -- have fun!

  @setlocal enabledelayedexpansion
  for /L %%i in (1,1,999999) do (
    set n="%%i"
    if %%i lss 100000 (set n=0!n!)
    if %%i lss 10000 (set n=0!n!)
    if %%i lss 1000 (set n=0!n!)
    if %%i lss 100 (set n=0!n!)
    if %%i lss 10 (set n=0!n!)
      fsutil file createnew h!n!.cpp 1000 
  ) 

Upvotes: 1

Related Questions