18 November 2011

Dos Command for Batch Processing

When you need to process multiple files in a batch file use forfiles.exe

For example, to recursively delete all files in the directory U:\Downloads older than 7 days:

  1. Open Notepad
  2. Type the following:
    forfiles.exe /p "U:\Downloads" /m *.* /s /D -7 /c "cmd /c del @path /q"
    Where:
    /p [path]= path
    /m = File select mask. In this case *.* i.e. all files.
    /s = recursive
    /D [date/+dd/-dd]= select files with last modified date. In this case less than or equal to current date minus 7 days.
    /c [command]= command to run for each file surrounded by quotes. Switches can be used in the command string (See more information).
  3. Save the file as deletedownloads.bat (change file type to all files)
  4. Run it by double clicking on it.

More Information

07 November 2011

Application Cache Manifests: Browser Workflow and Common Errors

Whilst it took a while to get my head round exactly how application cache manifest files work I have found them to be very useful. Here are my notes on the subject.
Workflow
I found the workflow easier once I’d written it down. This is my approximation of it:
  1. Request for file made, browser sees manifest reference
    <html manifest=”site.appcache">
  2. Server responds with text file served as mime type “text/cache-manifest”
  3. Browser checks Cache File
    1. Never seen before:
      1. Download files: Fire progress events for each
        1. No Error: Fire cached event
        2. Error: Throw error event
          [End]
    2. Seen file before
      1. Manifest changed? NB: not ‘any file listed changed’ i.e. the manifest file’s content itself has to have been altered, not just the date/time stamp (see below for more information).
        1. Yes
          1. Download files: Fire progress events
            1. No Error: Fire cached event
            2. Error: Throw error event
              [End]
        2. No: Fire noupdate event
          [End]
NB: Once successfully cached, a browser will serve all cached files from it’s cache, not from the server. Theoretically this should be the case even if the cache is updated until window.appCache.swapCache() is fired, although I’ve found some browsers handle this situation differently. It can make for frustrating testing.

Possible Errors
An error is possible at any stage in this process. I’ve listed some of the common one’s below:
  • Manifest file not found
  • Manifest served with wrong type (typically just as text).
    In Apache Web Server you can alter this by adding the following to the config file:
    AddType text/cache-manifest .appcache

    In IIS you can alter the manifest mime type by adding the following to web.config in the application root:
    <configuration>
      <system.webServer>
        <staticContent>
          <remove fileExtension=".appcache" />
          <mimeMap fileExtension=".appcache" mimeType="text/cache-manifest" />
        </staticContent>
      </system.webServer>
    </configuration>
  • ANY of the files listed in the manifest do not download correctly.
    Theoretically this should also refer to ALL dependant files (e.g. images) too but  I’ve noticed this is browser and platform dependant.
  • Browser stubbornly refuses to show updated file despite it being updated on the server.
    Once successfully cached, the browser will continue to serve a file from it’s cache not from the server, except:
    • If it is specifically excluded from the cache by being listed in the “NETWORK:” section.
    • The manifest file is altered (see below).
    • It might still be served from the cache even if the manifest is re read, until swap cache is called.
  • Manifest file isn’t re-read:
    A common reason for this is that the file’s content hasn’t changed, but the date time stamp has been. A common solution to this is to add a comment with a date/time stamp e.g. #05/11/2011 12:34

In Conclusion
Working with manifests can be a very frustrating business BUT it is also very rewarding when the application runs successfully offline. My advice would be to turn off the caching until you’re ready to test it, then get ready to pull your hair out!
I found a very useful script which helped me debug de mystify  the app cache browser behaviour posted by Ben Nadel. Thanks Ben!
References
https://www.w3.org/TR/2011/WD-html5-20110525/offline.html
http://www.html5rocks.com/en/tutorials/appcache/beginner/
http://diveintohtml5.info/offline.html

05 November 2011

Android Boot Loop: SD Card Related Fixes

Even though I haven’t done anything other than use my Android Samsung Galaxy SII normally it sometimes gets into a boot loop. This happens whenever I un-mount the USB storage or when having just installed an app onto the SD card.
The system boots up as per normal and then just after it displays a message "media scanner running (SD card)" it just re-boots. If I leave it alone it will just continue doing this ad infinitum. If I take out the SD card and re-boot it will operate without any problems. However, most of my apps are installed on the SD so that’s not much use.
I have found two fixes for this very annoying problem: re-formatting the SD card; and inserting the SD after boot.


Reformat the SD card
Even though I use an SD to micro SD adapter for this purpose it still takes ages but on the positive side it does make me create a backup of my SD card.
    1. Take out the SD card (you can turn the phone on without it, but none of the apps installed on the SD will work).
    2. Copy the files to another computer and re-format it (as fat 32).
    3. Re-copy the files back again
    4. Reinsert it into the phone.
Insert SD card after boot
This is possible on my phone (Samsung Galaxy SII) but may not be on yours. Useful when you’re on the move and re-formatting isn’t an option.
    1. Place the SD in the card slot but do not push it home.
    2. Carefully put the battery in place and hold the battery so the contacts are just touching the phone’s (the battery will not be fully in). If you’re doing it correctly the SD card shouldn’t be fully in it’s slot. Its fiddly and might take a few attempts but it works for me on my Galaxy SII
    3. Then turn the phone on and wait for it to completely finish booting.
    4. When it has, push the battery home which will push the SD card up into the slot. Media scanner should run and then it should work as normal.
I haven’t yet found the actual cause so for the time being these fixes will have to do. Unfortunately my search results are over crowded with those unrelated to SD. I find it very annoying that the system can’t just overcome whatever problem it’s having with my SD, particularly because I feel if inserting it after boot works there can’t be any major problem with the SD.


Remove Offending Apps
The previous solutions are ok as a temporary solution. My phone started by doing it only every so often e.g. when I disconnected USB file transfer or installed some apps. Then it started doing it on any app install and every time I connected USB storage. ENOUGH!


I had already figured out it was something to do with the SD card. Reading other peoples fixes they found a resolution when they uninstalled all apps from the SD. This isn't an option for me as I have too many to move them to the phone and anyway this is the whole point: not to use up valuable phone memory with installed apps.


I had a hunch that it is caused when apps installed on SD are run before the SD is ready so the system flops. These were the steps I took to find the culprit:
  1. Establish possible causes: Install Advanced Task Killer (other task managers are available) on the phone and after a reboot (using the “Insert SD after boot” method above) load it up to see what apps were running all by themselves. Write these down and note which are running as services, and which are installed on the SD.
  2. Backup those Apps: Install Astro File Manager or another utility that allows App backups. Use Astro to backup the Apps you wrote down in step 1 (Menu, Tools, Application Backup). This is so you don't have to re-install them from the market.
  3. Uninstall: Remove any that you just don’t want running randomly by themselves, don’t use or that you think would try to access the SD card. For me these included: Flickr (probably accessing my camera pictures located on SD), Facebook (which has an Uploader service, again which will probably need my SD) and Contapps which I haven’t used of late.
After taking these steps try connecting USB storage and transfer some files across, disconnect it and install a (non service) app on the SD.
    This seemed to fix the problem for me, although of course to really find the culprit I should have done them one by one but I didn’t have time.

    [Update1] After a month or so of trouble free operation after taking these steps I am going to try installing the apps again one by one and will post results here.



    [Update2] This has definitely solved my problem. It's a few months now and I haven't seen the issue re-surface.


    [Update3] I think it's Facebook


    In Conclusion
    Only install one app at a time and test operations like connecting and disconnecting USB storage in between.


    I am using a Samsung Galaxy SII with Android 2.2 aka Froyo. NB I have not rooted it.

    04 November 2011

    Start and Stop IIS7 Websites on the Command Line

    Sometimes you need to script starting or stopping IIS7 websites e.g. when building projects. I use the following in a command prompt with admin privileges:
    %systemroot%\system32\inetsrv\appcmd.exe start site [site name]
    and
    %systemroot%\system32\inetsrv\appcmd.exe stop site [site name]

    01 November 2011

    Disable TortoiseSVN Cache

    The TortoiseSVN Cache (TSVNCache.exe) can hog system processes and for me it doesn’t really add much value so I just turn it off: Under Tortoise SVN Settings, Icon Overlays set Status cache to None.

    How to delete .svn folders

    To delete Subversion folders in a directory structure, create a batch file in the root folder and enter the following:

    for /f "tokens=* delims=" %%i in ('dir /s /b /a:d *svn') do (
      rd /s /q "%%i"
    )

    To create the file: in Explorer right click, select New, Text Document then rename from .txt to .bat

    Then save and run it.