Fehler bei Konvertierung von PS nach Batch
Hallo in die Runde,
ich habe den Powershell Code - siehe unten- ins batch konvertiert aber ich erhalte nun die Fehlermeldung; "Das die Syntax für den Dateinamen, Verzeichnisnamen oder die Datenträgerbezeichnung ist falsch." - siehe Bild - Komme aber nicht darauf wo hier jetzt der Fehler liegt
Der Powershell stammt aus dem anderen Beitrag
PS Code
PS-Code
Batch
Grüße
ich habe den Powershell Code - siehe unten- ins batch konvertiert aber ich erhalte nun die Fehlermeldung; "Das die Syntax für den Dateinamen, Verzeichnisnamen oder die Datenträgerbezeichnung ist falsch." - siehe Bild - Komme aber nicht darauf wo hier jetzt der Fehler liegt
Der Powershell stammt aus dem anderen Beitrag
PS Code
PS-Code
# Quell-Pfad
$quelle = "C:\Users\tanc\Documents\Test\Quelle"
# Zielpfad
$ziel = "C:\Users\tanc\Documents\Test\Ziel"
# Datum abfragen
$date = read-host "Datum des Ordners eingeben (yyyymmdd)"
# ordner ermitteln und diesen dann verarbeiten
foreach($folder in Get-ChildItem "$quelle\${date}_*" -Directory){
# Dateien nach SN gruppieren
foreach($filegroup in Get-ChildItem "$($folder.Fullname)\Dokumente" -File -Filter *.pdf | ?{$_.Basename -match '^[FPS]_(\d{6})_'} | group {$matches[1]}){
# SN Ordner inkl. Unterordner erstellen
$destination = "$ziel\$($filegroup.Name)"
if (!(Test-Path $destination)){
New-Item -ItemType Dir -Path $ziel -Name $filegroup.Name -Force | out-null
"A,B,C,D,E".split(",") | %{md "$destination\$_" -Force | out-null}
"Test,Vortest".split(",") | %{md "$destination\B\$_" -Force | out-null}
"A,B,C,D".split(",") | %{md "$destination\E\$_" -Force | out-null}
}
# Dateien in den SN Ordner im Unterordner "\E\C" kopieren
$filegroup.Group | Copy-Item -Destination "$destination\E\C" -verbose
# passende Subordner in "\B\Test" kopieren
Get-ChildItem "$($folder.Fullname)\Vortest\*$($filegroup.Name)" -Directory | Copy-Item -Destination "$destination\B\Vortest" -Recurse -verbose -Force
# passende Subordner in "\B\Vortest" kopieren
Get-ChildItem "$($folder.Fullname)\Test\*$($filegroup.Name)" -Directory | Copy-Item -Destination "$destination\B\Test" -Recurse -verbose -Force
}
# in der nächste Zeile das "-whatif" entfernen um den Ordner dann anschließend tatsächlich löschen zu lassen
remove-item $folder.Fullname -Recurse -Force -whatif
}
Batch
@echo off
setlocal
REM Set source and destination paths
set quelle=""C:\Users\tanc\Documents\Test\Quelle""
set ziel=""C:\Users\tanc\Documents\Test\Ziel""
REM Prompt for date
set /p date="Datum des Ordners eingeben (yyyymmdd): "
REM Loop through folders in source directory
for /d %%A in ("%quelle%\%date%_*") do (
REM Group PDF files in Dokumente subfolder
for %%B in ("%%A\Dokumente\*.pdf") do (
for /f "tokens=1 delims=_" %%C in ("%%~nB") do (
REM Create SN folder and subfolders
set destination=""%ziel%\%%C""
if not exist "%destination%" (
mkdir "%destination%"
for %%D in (A B C D E) do mkdir "%destination%\%%D"
for %%E in (Test Vortest) do mkdir "%destination%\B\%%E"
for %%F in (A B C D) do mkdir "%destination%\E\%%F"
)
REM Copy files to SN subfolder
copy "%%B" "%destination%\E\C\" /y
REM Copy matching subfolders to destination
for /d %%G in ("%%A\Vortest\*%%C") do (
robocopy "%%G" "%destination%\B\Vortest" /e /njh /njs /ndl /nc /ns /np /nfl /ndl /r:0 /w:0
)
for /d %%H in ("%%A\Test\*%%C") do (
robocopy "%%H" "%destination%\B\Test" /e /njh /njs /ndl /nc /ns /np /nfl /ndl /r:0 /w:0
)
)
)
REM Remove processed folder
REM rd /s /q "%%A"
REM Pause after processing each folder
pause
)
endlocal
Grüße
Please also mark the comments that contributed to the solution of the article
Content-Key: 1720072945
Url: https://administrator.de/contentid/1720072945
Printed on: May 3, 2024 at 04:05 o'clock
26 Comments
Latest comment
set destination=""%ziel%\%%C""
if not exist "%destination%" (
wieder doppelt gemoppelte Anführungszeichen und zusätzlich Stichwort DelayedExpansion und setzen von Variablen innerhalb von Klammernif not exist "%destination%" (
@echo off &setlocal enabledelayedexpansion
REM ........
set "destination=%ziel%\%%C"
if not exist "!destination!" (
set quelle=""C:\Users\tanc\Documents\Test\Quelle""
set ziel=""C:\Users\tanc\Documents\Test\Ziel""
Doppelt gemoppelte Anführungszeichenset ziel=""C:\Users\tanc\Documents\Test\Ziel""
set "quelle=C:\Users\tanc\Documents\Test\Quelle"
set "ziel=C:\Users\tanc\Documents\Test\Ziel"
Gruß sid
Gut ExecutionPolicy muss man setzen. Kann man auch global oder mit Zertifikaten arbeiten.
Wie hast du es denn geschafft? Normal ist der Syntax immer geich. Du kannst PS auch von CMD aufrufen und die ExecutionPolicy anpassen.
Sind nur ein paar Wörter mehr. Wenn es generell geht, ersparst du dir Arbeit - auch mit dem setzen der EP....,.
Wie hast du es denn geschafft? Normal ist der Syntax immer geich. Du kannst PS auch von CMD aufrufen und die ExecutionPolicy anpassen.
Sind nur ein paar Wörter mehr. Wenn es generell geht, ersparst du dir Arbeit - auch mit dem setzen der EP....,.
ich hatte es einmal geschafft es ausführen, aber das war dann mit der executionPolicy zu umständlich.
Executionpolicy ist eh nur Schall und Rauch, lässt sich einfach mittels Bypass übergehenpowershell -EP Bypass -File "C:\pfad/zum\script.ps1"
Lt. MS: https://learn.microsoft.com/en-en/powershell/module/microsoft.powershell ...
"If the execution policy in all scopes is Undefined, the effective execution policy is Restricted for Windows clients and RemoteSigned for Windows Server."
"Restricted
The default execution policy for Windows client computers.
Permits individual commands, but does not allow scripts.
Prevents running of all script files, including formatting and configuration files (.ps1xml), module script files (.psm1), and PowerShell profiles (.ps1)."
also gilt bei dir restricted.
Wenn du remotesigned nimmt: wie hast du denn das Script signiert?
"If the execution policy in all scopes is Undefined, the effective execution policy is Restricted for Windows clients and RemoteSigned for Windows Server."
"Restricted
The default execution policy for Windows client computers.
Permits individual commands, but does not allow scripts.
Prevents running of all script files, including formatting and configuration files (.ps1xml), module script files (.psm1), and PowerShell profiles (.ps1)."
also gilt bei dir restricted.
Wenn du remotesigned nimmt: wie hast du denn das Script signiert?
Stichwort findstr
16 set "destination=ziel\%%C"
Nur mit "set /a ..." werden %...% weggelassen, für Verrechnungen mit Zahlen-Variablen.
14 for /f "tokens=2 delims=_" %%C in ('findstr /b "[0-9][0-9][0-9][0-9][0-9][0-9]" "%%~nB"') do (
Denke, findstr müsste aber in den 'do'-Sektor. Genauer kann ich auf die Schnelle leider
nicht werden, ohne vorher ausgiebig zu testen.
... naja wollen wir mal nicht so sein, weil heute Montag und nicht Freitag ist ....
..und wech ...
for /f "delims=" %%B in ('dir /b /a-d "%%A\Dokumente\*.pdf" ^|findstr /irc:"_[0-9]*\.pdf$" 2^>nul') do (
Dann hast es falsch eingebaut klappt hier probemlos ...
Wie wärs einfach mal mit RTFM? Dann bist du selbst schlauer und musst nicht jeden Furz erfragen .
Man so viel Zeit will ich mal für so ein Kindergartengewäsch bekommen .
Wie wärs einfach mal mit RTFM? Dann bist du selbst schlauer und musst nicht jeden Furz erfragen .
Man so viel Zeit will ich mal für so ein Kindergartengewäsch bekommen .
Nö! Es wird nur Zeile 13 Komplett damit ersetzt, nichts hinzugefügt ...