2016-04-09 12 views
-1

Ich habe diesen Batch-Skript, das alle Dateien in einem Verzeichnis aufgelistet werden sollte:Batch SET/A in für Schleife Pausen Schleifenvariablen

SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION 
SET /A cn=0 
SET cwd=%~dp0 

ECHO Files: 
FOR %%I IN (%cwd%\*) DO (
    SET /A cn=cn+1 
    ECHO !cn! 
    IF "%%I"=="buthow"() 
    ECHO %%~nxI 
) 
EXIT /B 0 

sagen, wir haben das Dokument d1.txt, d2.txt, d3.txt im Ordner. Der obige Code funktioniert wie erwartet, aber wenn ich die if-Anweisung zu entfernen, die nichts tut, bekomme ich die folgende Ausgabe (?):

1 
d1.txt 
2 
cnd2.txt 
3 
cnd3.txt 

Warum ist das so?

+0

Der Code hat _two_ ECHO-Befehle:! Die 'ECHO cn' Das sollte zeige den Wert der 'cn'-Variablen und' ECHO %% ~ nxI' an, das den Namen und die Erweiterung jeder Datei anzeigen sollte, damit die Ausgabe, die du zeigst, dies beweist der Code "funktioniert wie erwartet" (ohne den seltsamen 'if' Befehl). – Aacini

+0

Kannst du eventuell noch schlampiger werden? Der gepostete Code "funktioniert wie erwartet"?!?! Ich denke nicht, da es einen schwerwiegenden Syntaxfehler mit Ihrer IF-Anweisung gibt. Aber die Ausgabe ohne IF ist genau so, wie es sein sollte. – dbenham

+0

Nein. Die erwartete Ausgabe wäre mit 'd2.txt' anstelle von' cnd2.txt' gewesen. – arkocal

Antwort

0

if /?

Performs conditional processing in batch programs. 

IF [NOT] ERRORLEVEL number command 
IF [NOT] string1==string2 command 
IF [NOT] EXIST filename command 

    NOT    Specifies that Windows should carry out 
        the command only if the condition is false. 

    ERRORLEVEL number Specifies a true condition if the last program run 
        returned an exit code equal to or greater than the number 
        specified. 

    string1==string2 Specifies a true condition if the specified text strings 
        match. 

    EXIST filename Specifies a true condition if the specified filename 
        exists. 

    command   Specifies the command to carry out if the condition is 
        met. Command can be followed by ELSE command which 
        will execute the command after the ELSE keyword if the 
        specified condition is FALSE 

The ELSE clause must occur on the same line as the command after the IF. 

Hinweis Beachten Sie, dass () kein gültiger Befehl lautet:

==> if 1==2() 
) was unexpected at this time. 

==> if 1==1() 
) was unexpected at this time. 

==>() 
) was unexpected at this time. 

==> 
+0

Es könnte sein, dass WINE es etwas anders interpretiert, ich kann nicht unter Windows testen, aber es gibt keinen (unerwarteten) Unterschied in der Ausgabe wenn ich es mit einer ECHO- oder PAUSE-Aussage fülle. Die Frage war eher, wo würden die zusätzlichen 'cn' am Anfang von Dateinamen herkommen? – arkocal

+0

Bitte fügen Sie ECHO ON kurz vor 'FOR %% I ...' line; kontaktieren Sie WINE's [Bugzilla Bug Tracker] (https://bugs.winehq.org/). Bitte fügen Sie Ihrer Frage über den Link [edit] (http://stackoverflow.com/posts/36516508/edit) das Wort 'wine' hinzu. Allerdings würde '(echo (> NUL)' Anweisung _ nichts als syntaktisch falsch '') tun. – JosefZ