Posts Tagged ‘ script

View string including special characters like newlines in Python

After spending a frustrating hour or two trying to format a string, i stumbled on ’s repr() funcion. repr() allows you to inspect a string object when you’re troubleshooting your code. For example:

I was trying to understand a string that was shown in the terminal like this:

print mystring
Node - 192.168.1.104
ERROR:
Description = Generic failure

Seems pretty simple to remove the extra right? no. doing a string.replace(“\n”, ” “) did not fix it, but actually mangled the string. So here comes repr() to the rescue…

print repr(mystring)
'Node - 192.168.1.104\r\r\nERROR:\r\r\nDescription = Generic failure\r\r\n'

\r\r\n… i’ve never heard of it before, but knowing that it’s there, i could easily change my replace to:

print mystring.replace("\r\r\n", " ")
Node - 192.168.1.104 ERROR: Description = Generic failure 

Script to run handbrake on an entire folder

UPDATED 8-10-10:
This has been superceeded by its newer version at http://www.surlyjake.com/2010/08/script-to-run-handbrake-recursively-through-a-folder-tree/. The new version features all of the previous features, but can also traverse recursively through a folder structure.

#!/bin/bash
if [ -z "$1" ] ; then
	TRANSCODEDIR="."
else
TRANSCODEDIR="$1"
fi
for file in "$TRANSCODEDIR"/*
do
	HandBrakeCLI -i "${file}" -o "${file}.mp4" --preset="iPhone & iPod Touch"""
done

Save that into a .sh file like “handbrakefolder.sh”

Uninstall ALL Versions of WinZip Batch Script

I have been struggling with this for quite a while, and ran into a lot of issues with all of the different versions on our network. There are people with 100 line .vbs to do this, and I think this is much simpler:

@echo off
REM
REM
REM    Detects winzip and silently uninstalls
REM    There is a un-stoppable pop-up window. so this will kill
REM    all instances of  IE after the UN-installation.
REM
REM
REM
 /F /IM wzqkpick.exe
 product where "name like '%%winzip%%'" call Uninstall
REM
REM This section is for WinZip <12
REM
If Exist "%programfiles(x86)%\Winzip\winzip32.exe" GOTO 64
If Exist "%programfiles%\Winzip\winzip32.exe" "%programfiles%\WinZip\Winzip32.exe" /Uninstallx
taskkill /F /IM iexplore.exe
GOTO :END
:64
"%programfiles(x86)%\WinZip\Winzip32.exe" /Uninstallx
taskkill /F /IM iexplore.exe
:END

For getting rid of WinZip 12, the magic happens in the 'wmic' line. It searches all of the installed product's names for "winzip" and will uninstall anything it finds. This only works for products that use MSI's. If you ran this outside of a batch file, you will have to use a single % percentage sign instead of the %% double percentage sign... scripting sucks, and that's just the way it is. Same thing that happens in "FOR" loops.

I never knew anything about the control (wmic) until now, but I will be sure to exploit it's features.

script to update wordpress

Why? Because i’m lazy. I’ll this to see if it works
#!/bin/sh
#your current site will be backed up to your home folder with the date in the name
#change this to whatever you want
BACKUPNAME="_site_b4_update"
BACKUPDATE=`date +%y%m%d`
#this is the path to your wordpress site's root
SITEROOT="/usr/local/www/"
#here she goez...
cd ~
tar -pczf "$BACKUPNAME""$BACKUPDATE".tar.gz "$SITEROOT"
cd /usr/local
fetch http://wordpress.org/latest.tar.gz
tar -xzf latest.tar.gz
rm latest.tar.gz
cp -R wordpress/* "$SITEROOT"/
rm -R wordpress
chown -R www "$SITEROOT"