Question

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.

Was it helpful?

Solution

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
}

OTHER TIPS

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top