1

I am currently trying to write a quick batch script which will loop 1 through X and create a directory with an empty .txt file.

Here is what I have written so far:

set x = 10

:DoUntil

MkDir "Test Location %x%"
echo. 2>"Test Location %x%\EmptyFile.txt"
set x -= 1

IF x > 0 goto DoUntil 

%x% isn't writing out 1,2,3... in each loop and the file isn't being created because the folder doesn't exist.

Can anyone help so that I can fix this script to have the following directories and files created?

Test Folder 1\EmptyFile.txt
Test Folder 2\EmptyFile.txt
Test Folder 3\EmptyFile.txt
Test Folder 4\EmptyFile.txt
Test Folder 5\EmptyFile.txt
Test Folder 6\EmptyFile.txt
Test Folder 7\EmptyFile.txt
Test Folder 8\EmptyFile.txt
Test Folder 9\EmptyFile.txt
Test Folder 10\EmptyFile.txt

I am open for a PowerShell implementation or something better if you have a suggestion. It's to help a QA person test a File Pickup service which will go out and collect files from specific directories all over the network.

Mark
  • 3,717
  • 3
  • 33
  • 48

2 Answers2

3

Thanks to: While loop in batch

setlocal enableextensions enabledelayedexpansion
set /a "x = 0"
:while1
    if %x% leq 10 (
        echo %x%
        MkDir "Test Location %x%"
        echo. 2>"Test Location %x%\EmptyFile.txt"
        set /a "x = x + 1"
        goto :while1
    )
endlocal
Community
  • 1
  • 1
Adam
  • 3,014
  • 5
  • 33
  • 59
  • Thanks worked perfectly and if I could accept 2 answers I'd accept yours and @mikeblake's – Mark Mar 30 '11 at 15:50
1

In Powershell

foreach($i in 1..100)
{
    md "Test Folder $i" -force | out-null #or New-Item
Set-Content -Path (join-path "Test Folder $i" "EmptyFile.txt") -Value $null
}
Adam
  • 3,014
  • 5
  • 33
  • 59
Michael Blake
  • 2,068
  • 2
  • 18
  • 31