vbgunz
9th April 2005 07:42 UTC
How to Parse a File of a Listing of Files and Directories. Distinguishing Importance
I am trying to parse a file that looks like the following...
Extracting Directory
Extracting Directory.dir
Extracting Directory.dir\File
Extracting Directory.dir\file.txt
Extracting Directory\File
Extracting Directory\file.txt
Extracting File
Extracting file.txt
into a NSH file that should look like the following...
Delete /REBOOTOK '$EXEDIR\Directory'
Delete /REBOOTOK '$EXEDIR\Directory.dir'
Delete /REBOOTOK '$EXEDIR\Directory.dir\File'
Delete /REBOOTOK '$EXEDIR\Directory.dir\file.txt'
Delete /REBOOTOK '$EXEDIR\Directory\File'
Delete /REBOOTOK '$EXEDIR\Directory\file.txt'
Delete /REBOOTOK '$EXEDIR\File'
Delete /REBOOTOK '$EXEDIR\file.txt'
BUT I am faced with two problems.
1. The two lines in red above are directories. Considering they're placed at the very top of the list, and considering I am using "Delete /REBOOTOK" they will simply not be removed. This is problem number 1.
2. I can pretty much parse the file but cannot or have not figured out a way to append the closing quote for every line. This is problem number 2.
This is as far as I've gotten
Delete /REBOOTOK '$EXEDIR\Directory
Delete /REBOOTOK '$EXEDIR\Directory.dir
Delete /REBOOTOK '$EXEDIR\Directory.dir\File
Delete /REBOOTOK '$EXEDIR\Directory.dir\file.txt
Delete /REBOOTOK '$EXEDIR\Directory\File
Delete /REBOOTOK '$EXEDIR\Directory\file.txt
Delete /REBOOTOK '$EXEDIR\File
Delete /REBOOTOK '$EXEDIR\file.txt
Now, this NSH file is useless because I cannot close the strings with the closing quotes. Even if I did close the strings I can end up with many directories in which should've been removed.
I was thinking of the following so to solve the directory problem. Create a copy of the list and simply parse and run the second list with RMDir for every item. I then thought this would probably be a waste of processing power and theres got to be a smarter method.
Please note how a directory above has an extension. Folders of this type exist. Also note how a file included in the above installation has no extension. This I believe can turn problem number 2 into a headache.
I need help in solving these two problems. Can anyone please offer me some advice, some pointers or something positive. Thanks for looking this over!
glory_man
9th April 2005 11:25 UTC
1. You must use RMDir to remove directories.
stb
9th April 2005 11:43 UTC
I think, the additional quote can be added with something like
StrCpy $0 "$0'"
I would reverse the list order. Think about a loop doing this
StrCpy $1 ""
for each $0 in list ; pseudo code
StrCpy $1 "$0$\n$1" ; \n or $\n is linebreak (?)
next
now you have a string with $\n as separator between items
Attention: this will not work well with NSIS because string length is limited to 1024 (8192 in special build). As each path may have 260 characters this will probably be an issue.
If you only require a quick&dirty solution you can make such a loop (pseudo code)
$2 = <count of list items> ; counter
for each $0 in list
WriteRegStr ... $2 $0
IntOp $2 $2 - 1
next
$2 = <count of list items>
for $1 = 1 to $2
ReadRegStr $0 ... $1
WriteToFile .nsh ...
next
Takhir
9th April 2005 11:53 UTC
If first file is installation log, good idea is to parse it from the end (to delete files first). Some discussion was in this thread http://forums.winamp.com/showthread.php?threadid=211188 .
To get dir attribute of the path use one of disk/file samples from archive http://nsis.sourceforge.net/archive/...instances=0,11 This is my variant http://nsis.sourceforge.net/archive/...ances=0,11,211 , but it is also possible using FindFirst/Next without System plug-in. Use RMDir if path defines a folder.
You can also simply add 2 lines for every path - Delete and RMDir.
Afrow UK
9th April 2005 12:25 UTC
To check if it's a folder, do IfFileExists "path\*.*"
-Stu
vbgunz
9th April 2005 19:41 UTC
Thank you to everyone! Your advice has enlightened me. Thanks again. I took another approach at this task and believe I've come up with something much easier to tackle. At least for those of you itching to spit out possible solutions :)
First, I had two ways to create the listing. the first listing found in my first post consisted of a weak verbosity. It left me in the dark as to what was a directory and what was a file. I've now decided this issue would probably be best approached with increased verbosity.
Here is the same exact listing in my first post but with more detail.
Date Time Attr Size Compressed Name
------------------- ----- ------------ ------------ ------------
2005-04-09 01:45:34 D.... 0 0 Directory
2005-04-09 01:45:36 D.... 0 0 Directory.dir
2005-04-09 01:45:15 ....A 0 0 Directory.dir\File
2005-04-09 01:45:15 ....A 0 0 Directory.dir\file.txt
2005-04-09 01:45:15 ....A 0 0 Directory\File
2005-04-09 01:45:15 ....A 0 0 Directory\file.txt
2005-04-09 01:45:15 ....A 0 0 File
2005-04-09 01:45:15 ....A 0 0 file.txt
------------------- ----- ------------ ------------ ------------
0 0 8 files
The magic begins with the 21st character. If it's a D it's a directory. If it is anything else it is a file. If it is a D, I need to replace the first 53 characters on that line to read "RMDir '$EXEDIR\". If the 21st character reads to be anything but a D, I will need to replace the first 53 characters with "Delete /REBOOTOK '". Regardless of whether it is a file OR a directory, I still need to close the string with a quote. So I'll need to also write to the end of every line.
Takhir, major thanks for the Read a File in reverse post. I've accomplished reading and writing back to the file in reverse so now directories are almost guarenteed to be removed. The only problem I have left over is understanding how to loop through every line.
I pretty much know how to StrCmp but I fail to understand how to loop through all lines once until the file is finished. Also, how do you finish enclosing the string with the final quote?
Any more help on this is absolutely appreciated! Thank you all again for your time and energy!
vbgunz
12th April 2005 19:13 UTC
I just wish to say major thanks to Instructor and Jnuw for helping me out on something that pertained to this issue. If you follow this link, with links above thanks to Takhir, you'll see how I did it!
Thanks again fellas!