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?
Creating empty folders or files from a text file
Moderators: white, Hacker, petermad, Stefan2
PowerShell: Creating empty folders or files from a text file
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"}}
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}}
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.
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
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?
# # #
Update for "Stefan Dummy File Creator"
$LIST='.\Your Files list.txt'
$LIST='_list.txt'
#$LIST=$($args[0])
Get-Content $LIST |
# For each line from $LIST:
ForEach{
# Replace unwanted chars:
$N=$_-replace":","_";
# If the line ends with an "\", create an folder:
If($N[-1] -eq '\'){
# If the folder not already exists, create him (this Test-Path-check will prevent error messages):
If( -not (Test-Path $N)){ New-Item -Path . -ItemType Directory $N}
}else{
# Else create a file:
# Only if the file not already exists, create it (that way don't overwrite already existing files):
# "-Path ." refers to the current working directory):
If ( -not (Test-Path $N)){ New-Item -Path . -Name $N -ItemType "file" -Value "This is a text string." -Force}
}
}
#
Or shorter (with aliases and better syntax):
#--------------------------------------
TOTALCMD#BAR#DATA
PowerShell -NoExit
GC '%N'|%%{$N=$_-replace':','_';If(-not(Test-Path $N)){If($N[-1] -eq '\'){MD $N}else{NI $N -Force}}}
C:\WINDOWS\System32\WindowsPowerShell\v1.0\PowerShell.exe
Create empty dummy files and folders (form SELECTED file with the list) in current working directory
1
-1
#--------------------------------------
#
# create always an folder:
GC '%N'|%%{ $N=$_-replace':','_'; If(-not(Test-Path $N)){MD $N} }
# create always a file (with path if asked for):
GC '%N'|%%{ $N=$_-replace':','_'; If(-not(Test-Path $N)){NI $N -Force} }
#
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?
# # #
Update for "Stefan Dummy File Creator"
$LIST='.\Your Files list.txt'
$LIST='_list.txt'
#$LIST=$($args[0])
Get-Content $LIST |
# For each line from $LIST:
ForEach{
# Replace unwanted chars:
$N=$_-replace":","_";
# If the line ends with an "\", create an folder:
If($N[-1] -eq '\'){
# If the folder not already exists, create him (this Test-Path-check will prevent error messages):
If( -not (Test-Path $N)){ New-Item -Path . -ItemType Directory $N}
}else{
# Else create a file:
# Only if the file not already exists, create it (that way don't overwrite already existing files):
# "-Path ." refers to the current working directory):
If ( -not (Test-Path $N)){ New-Item -Path . -Name $N -ItemType "file" -Value "This is a text string." -Force}
}
}
#
Or shorter (with aliases and better syntax):
#--------------------------------------
TOTALCMD#BAR#DATA
PowerShell -NoExit
GC '%N'|%%{$N=$_-replace':','_';If(-not(Test-Path $N)){If($N[-1] -eq '\'){MD $N}else{NI $N -Force}}}
C:\WINDOWS\System32\WindowsPowerShell\v1.0\PowerShell.exe
Create empty dummy files and folders (form SELECTED file with the list) in current working directory
1
-1
#--------------------------------------
#
# create always an folder:
GC '%N'|%%{ $N=$_-replace':','_'; If(-not(Test-Path $N)){MD $N} }
# create always a file (with path if asked for):
GC '%N'|%%{ $N=$_-replace':','_'; If(-not(Test-Path $N)){NI $N -Force} }
#
Re: Creating empty folders or files from a text file
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.
Re: Creating empty folders or files from a text file
Or, well, if you have a list of folders:
FolderList.txt:
FolderList.cmd:
FileList.cmd:
HTH
Roman
FolderList.txt:
- Folder1
- Folder2
- Folder3
FolderList.cmd:
- md Folder1
- md Folder2
- md Folder3
FileList.cmd:
- . > File1
- . > File2
- . > File3
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.
Re: Creating empty folders or files from a text file
Thank you very much Hacker.