Saved Bookmarks
| 1. |
Solve : Extract info from Dir? |
|
Answer» Win XP H. echo offwhich works quite well but the last two lines of the listing contain the FILES and directory info which I don't want. The info extracted from those lines is Quote 195 File(s) Is there any way of getting the for loop to ignore the final two lines. Obviously the contents of the lines will change when another folder is listed. ThanksUse the /b SWITCH with dir , the bare formatSince you are only setting one variable you dont need to use an array.. and with /b switch you dont need to skip any lines for /f "tokens=*" %%a in ('dir c:\*') do set line=%%aThe idea is to be able to extract information from the Dir listing. That information could be any combination of the information produced by Dir, last access date, created date, last written date, file size, filename etc. using the /b switch suppresses everything but the file/folder name so how could I extract say the file/folder name and creation date if /b is used??? Quote from: Woodman on December 10, 2008, 12:06:27 AM The idea is to be able to extract information from the Dir listing. That information could be any combination of the information produced by Dir, last access date, created date, last written date, file size, filename etc. using the /b switch suppresses everything but the file/folder name so how could I extract say the file/folder name and creation date if /b is used??? You can still get the file info with dir /b. Type for /? at the prompt and read the part about variable modifiers. Quote from: diablo416 on December 09, 2008, 11:52:11 PM Since you are only setting one variable you dont need to use an array.. What array? Since when did cmd have arrays? Quote from: Dias de verano on December 10, 2008, 12:15:17 AM Quote from: diablo416 on December 09, 2008, 11:52:11 PMSince you are only setting one variable you dont need to use an array.. don't be so sure, maybe diablo416 knows something you don't. heh. yeah.... When did we start helping Megaman 2 Robot masters anyway? Quote from: Dias de verano on December 10, 2008, 12:13:42 AM You can still get the file info with dir /b. Type for /? at the prompt and read the part about variable modifiers. Right on the mark thank you. I found everything I need except a syntax to return file ownership details. Do you know of one? I tried o and q without success. This would be a case of knowing your data up close and personal. You can send the directory list through the pipe, excluding lines which have either an open parenthesis or a close parenthesis. Other characters could be used and you could include characters as well. Code: [Select]echo off cls setlocal enabledelayedexpansion for /f "skip=4 tokens=1-6" %%a in ('dir /q /a:-d c:\*.* ^| find /i /v "(" ^| find /i /v ")"') do ( echo Owner: %%e File: %%f ) To access the other columns in the dir list, you'll need to reference the other variables defined by the tokens parameter. Quote Quote from: Dias de verano on Today at 02:15:17 AM Personally I have always thought of batch language as a trip down the rabbit hole, so who better to consult than the White Knight about arrays in batch code: Code: [Select]set month=12 for /f "tokens=%month%" %%i in ("January February March April May June July August September October November December") do ( set month=%%i ) echo %month% Hmmmm, his logic could be backwards, but since he is talking backwards, he got it right! Quote from: Sidewinder on December 10, 2008, 05:00:52 AM Personally I have always thought of batch language as a trip down the rabbit hole, so who better to consult than the White Knight about arrays in batch code: That's a pseudo-array. Quote from: Dias de verano on December 10, 2008, 11:42:13 AM Quote from: Sidewinder on December 10, 2008, 05:00:52 AMPersonally I have always thought of batch language as a trip down the rabbit hole, so who better to consult than the White Knight about arrays in batch code: Exactly; I see no subscripts anywhere.Good grief! Tough crowd. Quote That's a pseudo-array. What is a pseudo-array? Quote Exactly; I see no subscripts anywhere. I was under the impression that the month variable acted as an index into the array. However the White Knight suggested the following snippet for all the index lovers out there: Code: [Select]echo off setlocal enabledelayedexpansion : Make Array for /l %%y in (1, 3, 68) do ( call set /a idx=%%idx%%+1 call set array.%%idx%%=%%y ) : Read Array for /l %%i in (1,2,11) do ( for /f %%x in ("!array.%%i!") do ( echo Array Index: %%i Value: %%x ) ) Thank you Sidewinder - the code in your reply #8 is a great learning experience and of course works splendidly. The other comments in various replies about arrays, pseudo-arrays, White Knight et al are way above my level of knowledge. I cannot find that an array as shown in your reply #11 has anything to do with extracting Dir details but I'll keep LOOKING (or did my thread just get hijacked?). Thanks to all for the input, much appreciated. Quote from: Sidewinder on December 10, 2008, 06:28:23 PM
any attempt to use arrays in batch is a pseudo array. the definition of an array holds that they all have the same name, and are accessed via noncongruous subscripts. In your examples, each variable has a name that is simply formed from the index it refers to, there is no way to reference the array as a whole. for example, if you were to list environment variables after creating a "array" as you've described it, it will show all the "elements" of the "array". Although I must contend that it is a good workaround that offers array-like access methods. A similar thing occured with javascript, java doesn't have intrinsic support for arrays. The original solutions were pretty similar to that which you've created. Anyway, the entire problem dissapeared when JavaScript became an ECMAscript, and the standard (i believe) imposed the creation of an "Array" object. Arrays are something that can be simulated, but unless the language specification actually mentions it, you can assume that you can't create anything that can be called an array in formal terms. (VB docs mention array a lot... same with C and a lot of other languages (javascript notwithstanding, lol), but the docs for command line extensions and so forth? Don't think so.) What you've done is analogous to creating a file that contains other files, and calling it a file system. While you can access the files, you cannot do so using standard file system methods, just as you can do array-like things with your creation here, but cannot access it using standard array-access methods as supported by numerous array-featuring languages (javascript notwithstanding). And finally, I must mention that it is a clever way of adding the ability to work with sets of data, assuming environment space isn't at a premium, of course. |
|