steppedup Posted March 6, 2010 Share Posted March 6, 2010 (edited) (all in windows)<---addition to forum subject line.I wasted a couple of hours figuring this out, before going in a different design direction.But it was fun - and it may help someone-else out so here it goes.I wanted to access uTorrent's resume.dat in order to find where files are stored. But - this is very useful for anyone who wants to process binary files or read binary files in Windows in a programmatic way. Ironically, you have to do it through a "Linux-like environment" which is cygwin. 1. After confirming you have AutoIt3 working (this was done on Windows Vista, x64).2. Install cygwin - make sure you install vim,vi, or emacs as well. The default cygwin installation does not include them. This was Cygwin 1.7.1.Browse to C:\Cygwin, and edit Cygwin bat to look like this:@echo offC:chdir C:\cygwin\binbash --login -i %13. Here's your AutoIT one-liners that will run the files:Run(@ComSpec & " /c " &'c:\cygwin\bin\bash --login "triggerTorrentPaths.sh"', "", @SW_HIDE)Run(@ComSpec & " /c " &'c:\cygwin\bin\bash --login "triggerTorrentCaptionsNames.sh"', "", @SW_HIDE)Then you can use the standard _FileReadToArray, _Array functions to read through the contents.Below are the sole contents of triggerTorrentPaths.sh and triggerTorrentCaptionNames.sh. Make sure you use an editor like eEditor to enter them because Windows based platforms have white-space issues as opposed to Unix environments.triggerTorrentPaths.sh: (do not copy and paste...actually go get that or vim from cygwin, and type it in. Trust me!)#!/bin/shcat /cygdrive/c/users/lance/AppData/Roaming/uTorrent/resume.dat |sed 's/:/:\n/g'|sed -n '/path[0-9]*/{n;n;p;}'|sed 's/[0-9]:$//'|sed '/\\/!d' > /cygdrive/c/TheGoodSeed/uTorrentResumePathCache.txttriggerTorrentCaptionsNames.sh#!/bin/shcat /cygdrive/c/users/yippeeskippee/AppData/Roaming/uTorrent/resume.dat |sed 's/:/:\n/g'|sed -n '/path[0-9]*/{n;n;p;}'|sed 's/[0-9]:$//'|sed '/\\/!d' > /cygdrive/c/TheGoodSeed/uTorrentResumePathCache.txtOnce you've got them entered in,namethe file triggerTorents.shIn cygwin, chmod 755 triggerTorrentPaths.sh (and for the other file). This makes them executable.Test out the files by typing in "./ triggerTorrentPaths.sh" without the quotes while in cygwin.You know if they work if they produce your files in your desired folders.Finallyl, run the AutoIT3 lines as listed above.Below are my notes copied and pasted...I was working from something I found on the uTorrent forums, so was reverse-engineering.Please do not ridicule me too much, as I know I'm exposing my lack of in-depth knowledge here. But here's how I ended up figuring out what's sed's doing here.Run in cygwin, but direct it to text:Command:cat /cygdrive/c/users/yippeeskippee/AppData/Roaming/uTorrent/resume.dat |sed 's/:/:\n/g'|sed -n '/path[0-9]*/{n;n;p;}'|sed 's/[0-9]:$//'|sed '/\\/!d'Directed:cat /cygdrive/c/users/yippeeskippee/AppData/Roaming/uTorrent/resume.dat |sed 's/:/:\n/g'|sed -n '/path[0-9]*/{n;n;p;}'|sed 's/[0-9]:$//'|sed '/\\/!d' > full command.txtThen, drop off all the sed comments tocat /cygdrive/c/users/yippeeskippee/AppData/Roaming/uTorrent/resume.dat > 1st.txtcat /cygdrive/c/users/yippeeskippee/AppData/Roaming/uTorrent/resume.dat |sed 's/:/:\n/g' >2nd.txtEtc, etc - you can see how the command works easily by comparing each consecutive version.And below are my notes on breaking the above commands down. <====points to each section of code as it was broken down. Hope it helps!!!!-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------catDisplay the contents of a file (concatenate)Syntax cat [Options] [File]...\Display a file$ cat myfile.txtConcatenate two files: $ cat file1.txt file2.txt > union.txt\sedSED is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an editor which permits scripted edits, SED works by making only one pass over the input(s), and is consequently more efficient. But it is SED's ability to filter text in a pipeline which particularly distinguishes it from other types of editors.cat /cygdrive/c/users/yippeeskippee/AppData/Roaming/uTorrent/resume.dat |sed 's/:/:\n/g'|sed -n '/path[0-9]*/{n;n;p;}'|sed 's/[0-9]:$//'|sed '/\\/!d'So...display resume.datcat /cygdrive/c/users/yippeeskippee/AppData/Roaming/uTorrent/resume.dat<===============================pipe to sed |`/REGEXP/' This will select any line which matches the regular expression REGEXP. `g' Apply the replacement to *all* matches to the REGEXP, not just the first.inserts a new line at every : - actually substitutes :\n for :sed 's/:/:\n/g'`-n'`--quiet'`--silent' By default, SED will print out the pattern space at then end of each cycle through the script. These options disable this automatic printing, and SED will only produce output when explicitly told to via the `p' command.sed 'n;n;n;n;G;'# add a blank line every 5 lines (after lines 5, 10, 15, 20, etc.)# print the line immediately before a regexp, but not the line # containing the regexp sed -n '/regexp/{g;1!p;};h'<================================================================= # print the line immediately after a regexp, but not the line # containing the regexp sed -n '/regexp/{n;p;}'so...print the 2nd line after a regexp, but not the line itself?lines=$'line1\nline2\nline3\nline4\nline5\n'# print n lines after match if there are n lines, but do not print the matched line itselfecho -n "${lines}" | sed -n '/line2/{n;p;}'echo -n "${lines}" | sed -n '/line2/{n;{N;p;};}'echo -n "${lines}" | sed -n '/line2/{n;{N;N;p;};}'echo -n "${lines}" | sed -n '/line2/{n;{N;N;N;p;};}'# print the matching line and n lines after the match if there are n linesecho -n "${lines}" | sed -n '/line2/{N;p;}'echo -n "${lines}" | sed -n '/line2/{N;N;p;}'echo -n "${lines}" | sed -n '/line2/{N;N;N;p;}'echo -n "${lines}" | sed -n '/line2/{N;N;N;N;p;}'# print the matching line and n lines after the match (using grep)echo -n "${lines}" | grep -A 1 'line2'echo -n "${lines}" | grep -A 2 'line2'echo -n "${lines}" | grep -A 3 'line2'echo -n "${lines}" | grep -A 4 'line2'echo -n "${lines}" | grep -A 10 'line2'sed -n '/path[0-9]*/{n;n;p;}'<=============================================================# print only lines which match regular expression (emulates "grep") sed -n '/regexp/p' # method 1 sed '/regexp/!d' # method 2The empty regular expression `//' repeats the last regular expression match (the same holds if the empty regular expression is passed to the s command). Note that modifiers to regular expressions are evaluated when the regular expression is compiled, thus it is invalid to specify them together with the empty regular expression. sed '/\\/!d'<============================================================= Edited March 6, 2010 by steppedup Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now