130834
Goto Top

(Batch) Datum Sortier-Mechanismus

Hallo zusammen.
Ich bin schon etwas länger am Batch schreiben, und war schon oft uneingeloggt hier, und jetzt hab ich ein Problem bei dem ich gerne selbst Fragen würde.
Es geht darum das ich gerade eine Batch baue die Dateien, in meinem Fall Bilder, im aktuellen Ordner nach erstell-Datum sortiert. Der aktuelle Code macht etwa das:
Der aktuelle Pfad wird genommen - Alle Kreations-daten werden mit Datum und Zeit in eine TXT geschrieben, dann von einem mir unbekannten codeblock sortiert ( kopierter codeblock :sort )
und dann Sortiert in eine neue TXT ausgegeben. Mein Problem: die Sortierung fällt etwas falsch aus & stimmt aus irgendwelchen Gründen nicht immer + Da ich den Block nicht verstehe, kann ich ihn nicht meinen Bedürfnissen anpassen.

System Infos:
Windows 10
cmd, keine Zusatz-tools ausser compiler

hier ein Ausschnitt des ganzen:
FOR /F "tokens=*" %%A in ('DIR /A:-D /B %folder%') do ( call :gDate "%%~fA" )  

:gDate <File>
set "gFile=%~1" & set "gFate="  
FOR /F "usebackq" %%B in (`wmic datafile where "Name='%gFile:\=\\%'" get^ creationdate^|find "+"`) do ( set gFate=%%~B )  
echo=%~nx1,%gFate:~6,2%/%gFate:~4,2%/%gFate:~,4% %gFate:~8,2%:%gFate:~10,2%;%gFate:~12,2%>>file2.txt
exit /b


:sort
(
FOR /f "tokens=1*delims=, " %%h IN (file2.txt) DO (  
 FOR /f "tokens=1-7delims=/: " %%a IN ("%%i") DO (  
  SET /a rmonth=10+%%a
  SET /a rday=10+%%b
  SET /a rhour=10+%%d
  IF %%d==12 (SET rhour=10)
  ECHO %%c!rmonth!!rday!%%f!rhour!%%e,%%h,%%i
 )
)
)>sorttemp.txt
(
FOR /f "tokens=1*delims=," %%i IN ('sort ^<sorttemp.txt') DO ECHO %%j  
)>output.txt
TYPE output.txt
DEL sorttemp.txt /F /Q

pause >nul
exit

Und hier ein paste des Outputs: http://pastebin.com/GYLmDH2w

wäre echt nett wenn mir jemand dabei helfen könnte ~
Lg clragon

Content-Key: 315162

Url: https://administrator.de/contentid/315162

Printed on: April 25, 2024 at 20:04 o'clock

Member: bastla
bastla Sep 13, 2016 at 21:31:55 (UTC)
Goto Top
Hallo clragon und willkommen als Mitglied im Forum!

Was ist denn Dein gewünschtes Ergebnis? Nach Erstellungsdatum sortieren kann dir auch selbst - im einfachsten Fall:
dir  /a-d /tc /od %folder%
An Dein Beispiel angepasst dann etwa so (mit Formatierung von Datum und Zeit lt Systemeinstellung):
<code">set "output=D:\output.txt"
:: in den Ordner %folder% wechseln
pushd "%folder%"
:: ev vorhandene Ausgabedatei löschen
del "%output%" 2>nul
:: sortierte Liste erzeugen
for /f "delims=" %%i in ('dir /b /a-d /tc /od') do for /f "tokens=1-2" %%a in ("%%~ti") do >>"%output%" echo %%i,%%a,%%b
:: zurück zum ursprünglichen Ordner
popd

Grüße
bastla
Mitglied: 130834
130834 Sep 14, 2016 updated at 05:59:35 (UTC)
Goto Top
Hallo Basta, schön mal in Echtzeit mit dir zu schreiben, hab deinen Namen schon so oft gesehen wenn ich Grade eine Lösung gesucht habe!
Also soweit ich weiss und das ausprobiert habe, gibt der tc switch nicht das eigentliche Erstellungsdatum aus sondern nur das letzte änderungsdatum. Ich brauche aber das Erstellungsdatum, und das funktioniert mit wmic auch ganz gut, nur meine Sortierung ist etwas irreführend. Ich möchte ganz gerne den Namen am Schluss, das Datum und die zeit am Anfang jeder Zeile, ( File2.txt ) das wäre ja kein Problem, ich könnte einfach die parameter umstellen, jedoch frisst der sortiert Mechanismus :sort das dann nicht mehr, und ich wess nicht wie er genau funktioniert, also kann ich ihn auch nicht ändern...
Edit: Das Ergebnis das ich gerne hätte ist eine TXT mit den Namen aller Dateien des aktuellen Ordners, dem Datum und der Zeit ihrer Erstellung. Weiterführend würde mein Script dann diese Dateien umbenennen, mit einer 4 stelligen Zahl startend mit der ältesten Datei. Das kann ich aber auch selber. Mit geht es nur darum das ich den :sort Mechanismus verstehe und beherrsche.
Mitglied: 129813
129813 Sep 14, 2016 updated at 06:22:31 (UTC)
Goto Top
Sorting? No problem at all...
@echo off
Powershell -Executionpolicy ByPass -Command "gci '.\*' -File | sort CreationTime | ft Name,CreationTime -AutoSize -Wrap" >Output.txt  
Regards
Mitglied: 130834
130834 Sep 14, 2016 at 11:01:59 (UTC)
Goto Top
I gonna give it a try, but I highly prefer pure batch solutions...
Mitglied: 129813
129813 Sep 14, 2016 at 11:09:33 (UTC)
Goto Top
Zitat von @130834:
but I highly prefer pure batch solutions...
Your solution above is also not pure batch, sort/wmic etc. are also external tools like powershell.exe is face-wink
Mitglied: 130834
130834 Sep 14, 2016 at 11:16:08 (UTC)
Goto Top
But power shell work's only above Windows 7, and I can't inbind the whole shell while compiling, and I guess wmic would work in XP, if I'm wrong, I could bind wmic in, otherwise, my solution is as "wrong" as yours...
Mitglied: 129813
129813 Sep 14, 2016 updated at 11:19:17 (UTC)
Goto Top
Zitat von @130834:
But power shell work's only above Windows 7,
No, XP can run the powershell as well, from Vista upwards its already integrated by default face-smile
and I can't inbind the whole shell while compiling,
What do you compile where?? You don't need to compile batch, it's done automatically.
Mitglied: 130834
130834 Sep 14, 2016 at 11:24:17 (UTC)
Goto Top
when a batch needs extensions, like consolenoclose, I compile them into exe flies to garantee acces to those.
if there is no wmic in XP or Vista, I would just inbind wmic, but also that solution would be completely wrong, because I want a batch who works on all windows versions without compiling it.
btw why was powershell even EVERYTHING invented, why they didn't updated batch...
I hate power shell.
but if your snippet works like it should, I gonna take it as solution.
Mitglied: 129813
129813 Sep 14, 2016 updated at 11:29:01 (UTC)
Goto Top
I hate power shell.
That is always said by people who don't know it face-wink

I would rather write a c#/C/AutoIT app where every dependencies are included by default.

Packing batches in exes is bullshit (most antivirus claim that they are suspicious)
Mitglied: 130834
130834 Sep 14, 2016 at 11:28:56 (UTC)
Goto Top
I'm anyways learning C#, but until it's done I'm messing around with the capability of batches , because I learned them first.
Mitglied: 130834
130834 Sep 14, 2016 at 11:41:35 (UTC)
Goto Top
Okey okey okey, that's a cool thingy you got there. is there a in-comand-parameter to remove Name und creation Date line title, or do i need to remove them by myself?
Mitglied: 129813
Solution 129813 Sep 14, 2016 updated at 11:51:14 (UTC)
Goto Top
Sure only by adding the additional parameter -HidetableHeaders
Powershell -Executionpolicy ByPass -Command "gci '.\*' -File | sort CreationTime | ft Name,CreationTime -AutoSize -Wrap -HidetableHeaders" >Output.txt  
Mitglied: 130834
130834 Sep 14, 2016 at 11:51:46 (UTC)
Goto Top
okey thanks, it works great... ~.~
Mitglied: 130834
130834 Sep 14, 2016 updated at 12:35:40 (UTC)
Goto Top
Errrm.... I'm getting another problem there. Can you switch me Date and name? bacuse as soon my Name gets max length, the date and time disappears, and if i switch it i can also better set up the get name and get date variable.
Mitglied: 129813
129813 Sep 14, 2016 updated at 12:40:13 (UTC)
Goto Top
simply swap the order, or better use export-csv for consistency.
Mitglied: 130834
130834 Sep 14, 2016 at 12:45:35 (UTC)
Goto Top
swapping is easy, when i take a closer look, but.. whats export-csv?
Member: bastla
Solution bastla Sep 14, 2016 at 19:07:23 (UTC)
Goto Top
Hallo clragon!
Also soweit ich weiss und das ausprobiert habe, gibt der tc switch nicht das eigentliche Erstellungsdatum aus sondern nur das letzte änderungsdatum.
Ein dir /? zeigt ua:
  /T         Bestimmt welche Zeit verwendet wird (nur für NTFS).
  Zeit        C  Erstellung
              A  Letzter Zugriff
              W  Letzter Schreibzugriff
/TC liefert tatsächlich das Erstellungsdatum (für das Änderungsdatum brauchst Du /T nicht, da Default).
Das Ergebnis das ich gerne hätte ist eine TXT mit den Namen aller Dateien des aktuellen Ordners, dem Datum und der Zeit ihrer Erstellung.
Mein (inzwischen sogar getesteter face-wink) Ansatz sähe für das von Dir gewünschte Ergebnis dann so aus:
set "output=D:\output.txt"  
:: in den Ordner %folder% wechseln
pushd "%folder%"  
:: ev vorhandene Ausgabedatei löschen
del "%output%" 2>nul  
:: sortierte Liste erzeugen
for /f "delims=" %%i in ('dir /b /a-d /tc /od') do for /f "tokens=1-2" %%a in ('dir /tc "%%~i"^|findstr "%%~i"') do >>"%output%" echo %%i,%%a,%%b  
:: zurück zum ursprünglichen Ordner
popd
Grüße
bastla
Mitglied: 130834
130834 Sep 14, 2016 updated at 20:05:26 (UTC)
Goto Top
Vielen Dank, genau sowas habe ich gesucht! okey, es erzeugt 2 Fehler, da es auf dem Desktop nach desktop.ini und Thumbs.db sucht, und diese versteckt sind, aber das script ist sehr Batch-Nah, ich muss mich nicht um die Powershell-Überlastungs-Abstürze kümmern, und ich verstehe sogar den Inhalt / kann mit ihm umgehen. Danke für die Mühe
Edit: unfazbar, mein script ist etwa 5 mal so schnell... Powershell scheint wohl doch nicht so gut zu sein.
lg clragon
Member: bastla
bastla Sep 14, 2016 at 21:33:17 (UTC)
Goto Top
Hallo clragon!

Wenn Du Verzeichinsse sowie versteckte / System-Dateien ausschließen willst, kannst Du
dir /a-d-h-s
verwenden ...

Grüße
bastla