Click here to Skip to main content
15,918,123 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
PowerShell
# 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
}


What I have tried:

I have a powershell script that I need in batch. Unfortunately I don't have the experience with it and I wanted to ask if someone can help me with it.
Posted
Updated 29-Aug-23 23:00pm
v2
Comments
Richard MacCutchan 30-Aug-23 5:00am    
Help in what way? You have not explained what the problem is.
Richard Deeming 30-Aug-23 5:18am    
It's simple enough to execute a Powershell script from a batch file:
powershell.exe -ExecutionPolicy Bypass -Command "Path\xxx.ps1"

Why do you think you need to rewrite the entire script as a batch file?
[no name] 30-Aug-23 5:22am    
Chatgpt translatet me, the code into batch, but it doesn't work. And i don't know the failure.


@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
[no name] 30-Aug-23 5:27am    
because even with the aforementioned

powershell.exe -ExecutionPolicy Bypass -Command "Path\xxx.ps1"

I can't run the script on my workstation, it's a corporate pc and the restrictions are too high.
CHill60 30-Aug-23 5:45am    
If it's a corporate PC with restrictions on running scripts you are unlikely to be able to run batch, powershell, vbscript or python. We have a similar situation here.
You could try translating it into VBA and running it via Excel (or Word)
Couple of other words of advice:
- "it doesn't work" doesn't tell us anything - be more specfic about your problem
- you lost me at "Chatgpt translatet me, the code into batch"
- instead of flailing around grabbing stuff of the internet and getting AI to "translate" it for you, try stating what it is you are actually trying to do

1 solution

To add to what the others have said, it doesn't quite work like that.
We do not do your work for you.
If you want someone to write your code, you have to pay - I suggest you go to Freelancer.com and ask there.

But be aware: you get what you pay for. Pay peanuts, get monkeys.

The idea of "development" is as the word suggests: "The systematic use of scientific and technical knowledge to meet specific objectives or requirements." BusinessDictionary.com[^]
That's not the same thing as "have a quick google and give up if I can't find exactly the right code".
So either pay someone to do it, or learn how to write it yourself. We aren't here to do it for you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900