Script um Ordner in gewähltem Verzeichnis zu erstellen
Hallo Community
Ich habe folgendes Thema. In einem Script möchte ich mehrere Ordnerpfade eingeben können, ich welchen dann überprüft wird, ob bestimmte Ordner bereits angelegt sind. Wenn nicht, sollen diese erstellt werden, wenn sie bereits vorhanden sind, sollen sie unberührt bleiben.
Als Beispiel:
In den Ordner
C:\Temp\Auftrag_A
C:\Temp\Auftrag_B
C:\Temp\Auftrag_C
sollen, falls noch nicht vorhanden, die Unterordner
Auftragsdokumente
Ablage
erstellt werden.
Ich stelle mir das so vor, dass in dem Script die Pfade hineinkopiert werden können und über einen Button werden die Ordner erstellt.
Danke für euer Feedback.
Gruss Reto
Ich habe folgendes Thema. In einem Script möchte ich mehrere Ordnerpfade eingeben können, ich welchen dann überprüft wird, ob bestimmte Ordner bereits angelegt sind. Wenn nicht, sollen diese erstellt werden, wenn sie bereits vorhanden sind, sollen sie unberührt bleiben.
Als Beispiel:
In den Ordner
C:\Temp\Auftrag_A
C:\Temp\Auftrag_B
C:\Temp\Auftrag_C
sollen, falls noch nicht vorhanden, die Unterordner
Auftragsdokumente
Ablage
erstellt werden.
Ich stelle mir das so vor, dass in dem Script die Pfade hineinkopiert werden können und über einen Button werden die Ordner erstellt.
Danke für euer Feedback.
Gruss Reto
Please also mark the comments that contributed to the solution of the article
Content-Key: 353314
Url: https://administrator.de/contentid/353314
Printed on: April 26, 2024 at 08:04 o'clock
5 Comments
Latest comment
Powershell:
function GenerateForm {
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
$form1 = New-Object System.Windows.Forms.Form
$btnStart = New-Object System.Windows.Forms.Button
$txtFoldernames = New-Object System.Windows.Forms.TextBox
$label2 = New-Object System.Windows.Forms.Label
$label1 = New-Object System.Windows.Forms.Label
$txtPaths = New-Object System.Windows.Forms.TextBox
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
$handler_btnStart_Click=
{
$folderscreated = @()
$names = $txtFoldernames.Text -split "[\r\n]+" | ?{$_ -notmatch '^\s*$'} | %{$_.trim()}
$txtPaths.Text -split '[\r\n]+' |?{$_ -notmatch '^\s*$'} | %{$_.trim()} | %{
$path = $_
$names | ?{!(Test-Path "$path\$_")} | %{
md "$path\$_" -Force
$folderscreated += "$path\$_"
}
}
if ($folderscreated.count -gt 0){
[System.Windows.Forms.MessageBox]::Show("Folgende nicht existente Ordner wurden angelegt:`n`n$($folderscreated -join "`r`n")","Info",0,64)
}else{
[System.Windows.Forms.MessageBox]::Show("Alle Ordner sind bereits vorhanden","Info",0,64)
}
}
$OnLoadForm_StateCorrection=
{#Correct the initial state of the form to prevent the .Net maximized form issue
$form1.WindowState = $InitialFormWindowState
}
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 272
$System_Drawing_Size.Width = 292
$form1.ClientSize = $System_Drawing_Size
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$form1.Name = "form1"
$form1.Text = "Folder-Checker"
$btnStart.Anchor = 10
$btnStart.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 205
$System_Drawing_Point.Y = 241
$btnStart.Location = $System_Drawing_Point
$btnStart.Name = "btnStart"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 75
$btnStart.Size = $System_Drawing_Size
$btnStart.TabIndex = 4
$btnStart.Text = "Start"
$btnStart.UseVisualStyleBackColor = $True
$btnStart.add_Click($handler_btnStart_Click)
$form1.Controls.Add($btnStart)
$txtFoldernames.Anchor = 14
$txtFoldernames.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 148
$txtFoldernames.Location = $System_Drawing_Point
$txtFoldernames.Multiline = $True
$txtFoldernames.Name = "txtFoldernames"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 87
$System_Drawing_Size.Width = 268
$txtFoldernames.Size = $System_Drawing_Size
$txtFoldernames.TabIndex = 3
$form1.Controls.Add($txtFoldernames)
$label2.Anchor = 6
$label2.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 128
$label2.Location = $System_Drawing_Point
$label2.Name = "label2"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 17
$System_Drawing_Size.Width = 134
$label2.Size = $System_Drawing_Size
$label2.TabIndex = 2
$label2.Text = "Ordnernamen einfügen"
$form1.Controls.Add($label2)
$label1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 9
$label1.Location = $System_Drawing_Point
$label1.Name = "label1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 13
$System_Drawing_Size.Width = 100
$label1.Size = $System_Drawing_Size
$label1.TabIndex = 1
$label1.Text = "Pfade einfügen"
$form1.Controls.Add($label1)
$txtPaths.Anchor = 15
$txtPaths.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 25
$txtPaths.Location = $System_Drawing_Point
$txtPaths.Multiline = $True
$txtPaths.Name = "txtPaths"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 100
$System_Drawing_Size.Width = 268
$txtPaths.Size = $System_Drawing_Size
$txtPaths.TabIndex = 0
$form1.Controls.Add($txtPaths)
$InitialFormWindowState = $form1.WindowState
$form1.add_Load($OnLoadForm_StateCorrection)
$form1.ShowDialog()| Out-Null
}
GenerateForm
VBS
Set fso = CreateObject("Scripting.FileSystemObject")
arrFolders = Split(InputBox ("Bitte Ordnerpfade mit Semikolon getrennt angeben:","Ordnerpfade angeben","C:\Temp\Auftrag_A;C:\Temp\Auftrag_B"),";",-1,1)
arrNames = Split(InputBox("Bitte die Ordnernamen mit Semikolon getrennt angeben:","Ordnernamen angeben","Auftragsdokumente;Ablage"),";",-1,1)
For Each folder In arrFolders
folder = Trim(folder)
If Not fso.FolderExists(folder) Then fso.CreateFolder folder
For Each name In arrNames
name = Trim(name)
newFolder = fso.BuildPath(folder, name)
If Not fso.FolderExists(newFolder) Then
fso.CreateFolder newFolder
End If
Next
Next
MsgBox "Fehlende Ordner wurden erstellt.",vbInformation