Posts tagged ‘Powershell’

Mit Powershell Dateien umbenennen

Ich hatte vor kurzem die interessante Aufgabe, Dateien umzubenennen, deren Namen folgenden Aufbau hatten: texttexttext_ttmmjjjj.xyz

Diese Namen sollten zu jjjjmmtt_texttexttext.xyz werden. Gelöste habe ich die Aufgabe mit zwei kurzen Powershell-Skripten

# Finde alle Dateien im aktuellen Verzeichnis, die am Ende des Dateinamens acht Ziffern haben
ls |  % { if ($_.name -match "\d{8}\.\w{3}$") {
# Ersetze diesen Dateinamen  durch das Datum
rni $_.fullname $_.name.replace($_.name,$_.name.substring($_.name.length-12,8)
# verbunden mit dem Dateinamen ohne Datum
+ "_" + $_.name.substring(0,$_.name.length-13)
# und der Dateiendung.
+ $_.name.substring($_.name.length-4,4))
  }}

Der zweite Teil, die Umsortierung des Datum war dann auch schnell erledigt:

# Suche alle Dateien, die mit acht Ziffern anfangen
ls |  % { if ($_.name -match "^\d{8}.*") { 
# Benenne die Datei um, erst das Jahr, dann der Monat
# dann der Tag
rni $_.fullname ($_.name.substring(4,4) + $_.name.substring(3,2)
+ $_.name.substring(0,2) + "_" 
+ $_.name.replace($_.name,$_.name.substring(9,$_.name.length-9)))
}}

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website

Powershell Ressourcen im Netz

Da es eine Menge guter Seiten zur Powershell gibt, fange ich mal an, die wichtigsten Links und Dokumente zusammenzutragen: Powershell Link-Seite.

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website

Powertools für OpenXML (Microsoft Office)

Mit den Powertools für OpenXMl (http://www.codeplex.com/PowerTools) gibt es einen interessanten Weg, Excel-Dokumente aus Powershell heraus zu erzeugen:

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website

Mit Powershell Excel-Diagramme erzeugen

Hier ein kleines Beispiel, wie man per Powershell-Code ein Excel-Diagramm erzeugen lassen kann:

$excel = New-object -comobject Excel.Application
$excel.Visible = $true
$excel.DisplayAlerts = $false
$workbook = $excel.Workbooks.Add()
$sheet = $workbook.Worksheets.Item(1)
$sheet.Name = 'Hallo Welt'
 
foreach ($i in 1..25){
    $sheet.cells.item($i,1) = $i
    $sheet.cells.item($i,2) = $i*2
}
 
# Bar Chart
# Datenherkunft
$range = $sheet.range('a1:a25')
 
# Chart-Variable anlegen
$ch = $sheet.shapes.addChart().chart
# Typ zuweisen (Bar Chart)
$ch.chartType = 58
# Daten zuweisen
$ch.setSourceData($range)

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website

Mit Powershell alle TeX-Dateien eines Verzeichnisses übersetzen

Das Übersetzen aller TeX-Dateien eines Verzeichnisses (siehe http://uweziegenhagen.de/?p=399) lässt sich natürlich auch mit Powershell erledigen:

foreach ($file in gci *.tex)
{
pdflatex $file.name 
}

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website

Mit Powershell den ‚dir‘ Befehl emulieren

Aus einem Posting in microsoft.public.windows.powershell:

dir /a
 
Get-ChildItem | Where-Object {$_.PSIsContainer
 
dir /a-
 
Get-ChildItem | Where-Object {!$_.PSIsContainer}
 
dir /o
 
Get-ChildItem | Sort-Object Date
 
dir /od /a-
 
Get-ChildItem | Where-Object {!$_.PSIsContainer} | Sort-Object Dat
 
dir /a
 
Get-ChildItem -force -path C:\ | Where-Object {$_.Mode -match "h"
 
dir /a-
 
Get-ChildItem -force -path C:\ | Where-Object {!$_.Mode -match "h"
 
dir /
 
Get-ChildItem | Select-Object Name

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website

Mit Powershell und Regulären Ausdrücken Dateien umbenennen.

Ausgehend von Powershell und Reguläre Ausdrücke hier eine Erweiterung des Skripts dahingehend, dass der alte Pfad zerlegt wird und mit dem sauberen Dateinamen wieder zusammengebaut wird.

ls -recurse |  % {
if ($_.name -match "\d{8}_\d{8}.+") {
    $full  =  $_.fullname 
    $split =  $_.fullname.split("\") 
    $last  = $split[$split.length-1]
    $newpath = $split[0]
    foreach ($number in 1..($split.length-2) ) { $newpath += "\" + $split[$number] }
        $newpath += "\" + $last.substring(9,$last.length-9)
    }
    "mv"+ "`t" + $full + "`t" + $newpath
}

Geht sogar noch deutlich einfacher, wenn man einfach den Dateinamen im vollen Pfad sucht und durch die saubere Version ersetzt:

ls -recurse |  % {
if ($_.name -match "\d{8}_\d{8}.+") {
    rni $_.fullname $_.name.replace($_.name,$_.name.substring(9,$_.name.length-9))
}}

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website

Powershell und Reguläre Ausdrücke

Folgendes Skript findet alle Dateien, die ein Datum fälschlicherweise doppelt im Dateinamen haben.

ls -recurse |  % {if ($_.name -match "\d{8}_\d{8}-.+") {$_.fullname}}

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website

Excel-Automation mit Powershell

Hier mal ein kurzes Beispiel für COM-Automation mit Excel.

$excel = New-object -comobject Excel.Application
$excel.Visible = $false
$excel.DisplayAlerts = $false
$workbook = $excel.Workbooks.Add()
$sheet = $workbook.Worksheets.Item(1)
$sheet.Name = 'Hallo Welt'
$workbook.SaveAs('C:\hallowelt.xlsx')
$workbook.Close()
$excel.Quit()

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website

Background-Jobs mit Powershell 2

http://technet.microsoft.com/en-us/library/dd315273.aspx hat Informationen und Beispiele, wie man Jobs in der Powershell 2.0 im Hintergrund laufen lassen kann.

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website