How to create multiple folders and name them by reading lines from text file?

Here's a Powershell script that does what you want.

$folder="X:\Test";             # Directory to place the new folders in.
$txtFile="X:\Test\Export.txt"; # File with list of new folder-names
$pattern="\d+.+";              # Pattern that lines must match      


get-content $txtFile | %{

    if($_ -match $pattern)
    {
        mkdir "$folder\$_";
    }
}

This will include the digits in the name.

To run the script, do the following.

  1. Run Powershell as administrator.
  2. Type in Set-ExecutionPolicy RemoteSigned and press Enter to enable running of scripts. Press Y when prompted.
  3. Close Powershell.
  4. Copy and paste the script above into Notepad or Notepad++.
  5. Replace X:\Test with the absolute path to the location where you want to create your new folders.
  6. Replace X:\Test\Export.txt with the absolute path to the text file that contains the names you want to use for these folders.
  7. Replace \d+.+ with the pattern the lines must match. \d+ matches any number .+ matches any character.
  8. Save it as "FileName.ps1" or whatever name you want. Just make sure to keep the .ps1 extension.
  9. Open Windows Explorer and go to the location where you saved your ps1 file. Right click on it and select "Run with Powershell".

Screenshots...

a b c d


Batch / VBS hybrid solution

Tested with Windows XP SP3, Vista SP2 and Windows 7 SP1. It's a specially crafted batch script with an embedded VB Script which does the actual job. This way you get a script which should be compatible with any operating system released after XP SP2. The main credit goes to jeb and dbenham who come up with (and refined) the hybrid technique used here.

REM^ &@echo off>nul
REM^ &if "%~2" == "" exit /b 2
REM^ &pushd "%~2"
REM^ &cscript //e:vbscript //nologo "%~f0" "%~1"
REM^ &popd
REM^ &exit /b

Dim stream, text, lines, fso
Set stream = CreateObject("ADODB.Stream")
stream.Open
stream.Type = 2
stream.Charset = "utf-8"
stream.LoadFromFile Wscript.Arguments(0)
text = stream.ReadText
stream.Close

lines = Split(text, vbCrLf)
Set fso = CreateObject("Scripting.FileSystemObject")

For i = 0 To UBound(lines)
fso.CreateFolder(lines(i))
Next

Instructions

  1. Copy and paste the script above into Notepad or any other plain text editor (e.g. Notepad++).

  2. Save it as CreateFolders.cmd or whatever name you want. Just make sure to keep the .cmd extension.

  3. Open a command prompt and navigate to the folder where you saved the file:

    cd /d "X:\Folder\containing\CreateFolders.cmd"
    
  4. Run the batch script by specifying the list file and the destination folder as the first and second parameter, respectively:

    CreateFolders.cmd "X:\Some\folder\Export.txt" "X:\Destination\folder"
    

Screenshots

b c

References

  • Command-Line Reference
  • Is it possible to embed and execute VBScript within a batch file without using a temporary file?
  • Stream Object (ADO)

PowerShell solution

The following PowerShell script replicates the solution above. It accepts two parameters, the first being the list file (which is assumed to be saved as UTF-8), and the second is the destination folder. Tested with Windows XP SP3, Vista SP2 and Windows 7 SP1.

Note Windows PowerShell 2.0 is bundled with Windows 7, but needs to be manually installed in XP/Vista. As for Windows XP, you need to have .NET Framework 2.0 SP1/SP2 installed, or .NET Framework 3.5 SP1, which include .NET Framework 2.0 SP2. Windows 8 and Windows 8.1 include PowerShell 3.0 and 4.0, respectively.

if ($args.Count -gt 1)
{
    $file=$args[0];
    $dest=$args[1];
    Get-Content $file -Encoding UTF8 | %{ md "$dest\$_" >$null; }
}

Instructions

  1. Copy and paste the script above into Notepad or any other plain text editor (e.g. Notepad++).

  2. Save it as CreateFolders.ps1 (or any other name, as long as you keep the proper extension).

  3. To run the script you can launch PowerShell and then either:

    • Navigate to the actual path, and then execute it:

      cd "C:\Some folder"
      & ".\CreateFolders.ps1" "X:\Some\folder\Export.txt" "X:\Destination\folder"
      
    • Execute it by specifying the full path directly:

      & "C:\Some folder\CreateFolders.ps1" "X:\Some\folder\Export.txt" "X:\Destination\folder"
      

    & is the call operator.

    As an alternative you can start it from a regular command prompt:

    powershell -ExecutionPolicy Bypass -NoLogo -NoProfile -File "C:\Some folder\CreateFolders.ps1" "X:\Some\folder\Export.txt" "X:\Destination\folder"
    

References

  • Running Windows PowerShell Scripts
  • More Powerful Ways to Launch Windows PowerShell
  • Get-Content for FileSystem

Previous (kind of working) solutions

Warning! Do not use them unless you known in advance the targeted system, and the data you're dealing with. If you still want to, make sure they work as expected.

Batch script

The following script will loop through all the lines of the list file and create as many folders, naming them after the actual line content. The script accepts two parameters: the first one is the path where the list is stored, which is hard-coded as Export.txt (feel free to put a different file name, but avoid spaces); the latter is the destination folder.

@echo off
setlocal

REM make sure there are enough parameters
if "%~2" == "" exit /b 2

REM set the working directory
pushd "%~1"

REM loop through the list and create the folders
for /f "delims=" %%G in (Export.txt) do (md "%~2\%%~G")

REM restore the working directory and exit
popd
endlocal & exit /b

Known limitations

  • Either ASCII or ANSI input files only.

Batch script - alternate UTF-8 version

This script is similar to the above one, but in this case the text file is not read directly but parsed through the type command output. The chcp is required first in order to change the encoding to UTF-8. In this case the list is not hard-coded but has to be specified as part of the first parameter. The second parameter is the destination folder.

@echo off
setlocal

REM make sure there are enough parameters
if "%~2" == "" exit /b 2

REM set the working directory
pushd "%~2"

REM set the list file
set file="%~1"

REM set the encoding to UTF-8
chcp 65001 >nul

REM loop through the list and create the folders
for /f "delims=" %%G in ('type %file%') do (md "%%~G")

REM restore the working directory and exit
popd
endlocal & exit /b

Known limitations

  • On English locales, the chcp 65001 command will halt the batch script execution, thus skipping any other command.