| 1. |
Solve : Opening files from list of files? |
|
Answer» Gudday all The file is 20090619153310.SPL No other files are parsed. I have 'remed' out the seemingly offending lines in the code above. Also is it possible to pass a variable, in this case %%a, from the inner loop to the outer loop? I wanted the progarm to tell me when it finished each file. The line ' left loop after 1st line of the file' is printed but not the current file name. You can use set /p to get the first line of a file. SET /P variable= It works because by default SET with the /p switch waits for a CR terminated string from STDIN (the keyboard) however you can use the < redirection symbol to make it take its input from a file. The crlf at the end of the first line of a text file ensures that the command terminates. so this should work... Code: [Select]@echo off setlocal enabledelayedexpansion dir D:\Archives\E20_120_AU\ArchiveCreator\Errors\*.spl /b > SPLfiles.txt for /f %%a in (SPLfiles.txt) do ( set file=%%a echo the file is !file! set /p firstline=<"%%a" for /f "tokens=1,2 delims= " %%i in ("!firstline!") do ( echo variable i = %%i echo variable j = %%j ) ) notes... (1) labels, GOTO etc In many ways, NT command script language is a kludge, and it doesn't always work the way you would expect if you are used to "proper" programming or scripting languages and tools. One thing that is peculiar is the way it processes loops and other parenthetical structures. A loop is processed as if it was one logical line. Thus this for [whatever] do ( some stuff ) is equivalent logically to for [whatever] do (some stuff) (In fact if you only have one command in the loop you can use that format.) Now you cannot have a label in the middle of a line, it has to be on a line by itself, so labels in loops break the code. Since your outer and inner loop is processed as one HUGE line, that is why it halted. Jumping out of loops is very tricky. While I am on the topic, you may come across example scripts where the writer has used a double (or SINGLE) colon instead of REM to start a comment line :: This is a comment This is a carryover from MS-DOS where the practice did no harm (I think). It works because labels are disregarded (mostly!) and the line is a (broken) label. However such a line inside parentheses BREAKS THE CODE. It is an undocumented and unsupported "feature". The behaviour it CAUSES can give much puzzlement to coders, however it is often vociferously defended by people who ought to know better. My advice is, don't do it! (2) Considering the FOR command, for /f %%V in (dataset) do With the /f switch, dataset (unquoted) is processed as a filename, whereas "dataset" is a string, which is why I used ("!firstline!") above. (3) In the other thread you asked about reference material for NT command scripting. One good site I have found is http://ss64.com/nt/ You may have the compiled help file ntcmds.chm on your system; it is not included on Vista & 7 I believe. type hh ntcmds.chm at the prompt to find out. If you don't have it, you can get if off an XP disk or installation, or download it from Microsoft (it is included in the Server 2003 Adminpak) http://support.microsoft.com/kb/304718 Dear ST Thank you once again. After some fiddling the code works at last! At least the boss is happy. |
|