Creating empty folders or files from a text file

English support forum

Moderators: white, Hacker, petermad, Stefan2

Post Reply
drbeat
Junior Member
Junior Member
Posts: 57
Joined: 2017-01-01, 01:13 UTC

Creating empty folders or files from a text file

Post by *drbeat »

Hi,
I want to create empty folder or file names from a text file (each line contains one corresponding name) How can I do it?
User avatar
Stefan2
Power Member
Power Member
Posts: 4157
Joined: 2007-09-13, 22:20 UTC
Location: Europa

PowerShell: Creating empty folders or files from a text file

Post by *Stefan2 »

You can do that with any scripting language.
Always depends on what you want to achieve; make absolute paths or create in current active folder?

But how to distinguish, in your list, folders from files?
TC add an trailing backslash on directories:
C:\Windows\Help\en-US\
C:\Windows\Help\en-US\credits.rtf
C:\Windows\Help\mui\0407\cliconf.chm
C:\Windows\Help\mui\0407\mmc.CHM
C:\Windows\Help\mui\0409\
C:\Windows\Help\OEM\ContentStore\
C:\Windows\Help\OEM\IndexStore\

For such a list you can use f.ex. PowerShell (or VBS or AHK or...)



PowerShell: create files and folders from text listfile.txt, create FOLDERs if the line from the listfile has trailing backslash (\), else create FILEs :

Get-Content '.\Your Files list.txt'|ForEach{$N=$_-replace":","_";If($N[-1] -eq '\'){MD $N}else{New-Item -Path . -Name "$N" -ItemType "file" -Value "string if wanted"}}

Code: Select all

Get-Content '.\Your Files list.txt'|
  ForEach{
    $N=$_-replace":","_";
    If($N[-1] -eq '\'){
      MD $N
    }else{
      New-Item -Path . -Name "$N" -ItemType "file" -Value "This is a text string."
    }
}



Or, the other way around:
PowerShell: create files and folders from text listfile.txt, create FILEs if the line from the listfile
has an dot as fourth (.HTM;.JPG) or fifth (.HTML;.JPEG;myFolder.2020\) sign backwards from the end , else create FOLDERs:

Get-Content '.\Files list Dateien liste-1.txt'|ForEach{$N=$_-replace":","_";IF($N[-4..-5] -eq '.'){New-Item -Path . -Name "$N" -ItemType "file" -Value "string if wanted."}else{MD $N}}

Code: Select all

Get-Content '.\Your Files list.txt'|
  ForEach{
    $N=$_-replace":","_";
    IF($N[-4..-5] -eq '.'){
      New-Item -Path . -Name "$N" -ItemType "file" -Value "This is a text string."
    }else{
      MD $N
    }
}


To get no error if file/folder already exists, prefix the command by: $ErrorActionPreference = "SilentlyContinue";
Example: $ErrorActionPreference = "SilentlyContinue"; Get-Content '.\Your Files list.txt'|ForEach{ ... }


We can make an button out of this which will work with the selected filelist.txt, or other things if you want.

Note on MD-command

PS X:\Temp> get-alias md

CommandType Name Version Source
----------- ---- ------- ------
Alias md -> mkdir


PS X:\Temp> get-command mkdir

CommandType Name Version Source
----------- ---- ------- ------
Function mkdir


PS X:\Temp> get-content function:mkdir

<#
.FORWARDHELPTARGETNAME New-Item
.FORWARDHELPCATEGORY Cmdlet
#>

[CmdletBinding(DefaultParameterSetName='pathSet',
. . . .
... ...
. . . . . . read more on your own console window ;-)
Help New-Item -Examples

PS C:\> help new-item -exa
PS C:\>New-Item -Path . -Name "testfile1.txt" -ItemType "file" -Value "This is a text string."
PS C:\>New-Item -Path "c:\" -Name "logfiles" -ItemType "directory" <<<<< Short: md "C:\logfiles"

What do you think about that?
drbeat
Junior Member
Junior Member
Posts: 57
Joined: 2017-01-01, 01:13 UTC

Re: Creating empty folders or files from a text file

Post by *drbeat »

Wow very complicated issue than I thought. I found a tool called Text2Folders. I copy folder and file names then add to text file and create them as folder successful. I think this tool is enough for me. Thank you very much for your extensive work and sorry for taking your time.
User avatar
Hacker
Moderator
Moderator
Posts: 13064
Joined: 2003-02-06, 14:56 UTC
Location: Bratislava, Slovakia

Re: Creating empty folders or files from a text file

Post by *Hacker »

Or, well, if you have a list of folders:

FolderList.txt:
  • Folder1
  • Folder2
  • Folder3
you prepend an "md" before the folder names in a text editor of your choice and rename the file to .cmd:

FolderList.cmd:
  • md Folder1
  • md Folder2
  • md Folder3
and then click on FolderList.cmd. As for creating files, you could use . > this way:

FileList.cmd:
  • . > File1
  • . > File2
  • . > File3
This, however, assumes, that you can split the lists into a file list and a folder list.

HTH
Roman
Mal angenommen, du drückst Strg+F, wählst die FTP-Verbindung (mit gespeichertem Passwort), klickst aber nicht auf Verbinden, sondern fällst tot um.
drbeat
Junior Member
Junior Member
Posts: 57
Joined: 2017-01-01, 01:13 UTC

Re: Creating empty folders or files from a text file

Post by *drbeat »

Thank you very much Hacker.
Post Reply