Results 1 to 10 of 22

Thread: Random file copying script

Threaded View

  1. #1
    Join Date
    Jun 2005
    the belly of a snake

    Lightbulb Random file copying script (great for ipod shuffle!)

    edit: I made a minor change to how the script searches for files, which should speed things up a bit. Also renamed the script to a more general-purpose name (randomcopy)
    A more ipod shuffle specific modification is in the next post. The one here can be used with any old device.


    This will show you how to copy a number of random files from selected directories into the folder of your choice. You can use this to fill cd-roms, dvd-roms, ipod shuffles, palm pilots, etc etc.
    Currently it is set up to only look for files with the extensions mp3, aac or aa. With a tiny bit of modification (see the 'variables' section in this post) you can make the script randomly copy any type of file -pending file extension- of practically any size. Providing the target device doesn't require you to reconvert files (like in a psp) or use any pointlessly complicated transfer software like itunes, there are many uses for this script.

    *Remember, it's totally random so it won't remember your favourite tracks or anything like that. I don't recommend this as a solution to software such as rhythmbox or gtkpod, but until those work properly with my ipod shuffle this seems to be the best alternative.
    Personally I recommend using the iPod shuffle Database Builder python script in order to point your ipod shuffle to all the files on it. It's really really easy to run.

    And now for some informative stuff stolen from that gnome nut, ardchoille:
    Nautilus script explanation
    A nautilus script is just an executable shell script (usually bash) that is placed in a special scripts directory so that the Nautilus graphical shell can find it. This is a really neat function of Nautilus, because it allows you to extend the functionality the the file browser to do just about anything. Scripts are invoked by selecting a file or group of files, and right-clicking with the mouse, to bring up a 'Context' menu. One of the options of this menu is the 'Scripts' submenu (this option only appears once you have at least one script in the scripts directory), which allows you to select a script to invoke on the selected files.

    For a script to appear on the script menu, it must:
    1. be placed in your scripts directory (~/.gnome2/nautilus-scripts), and
    2. be executable (with chmod a+x filename)

    If you place an executable script in your scripts directory, its name will not necessarily appear on the scripts menu immediately. You first must visit the scripts directory with Nautilus - which can be done using the last option in the scripts menu. Once the directory is visited, Nautilus will know about which scripts you have, and you will be able to use them.
    installation and use

    1) copy and paste the code below into a file called "randomcopy" (or whatever you like!) under ~/.gnome2/nautilus-scripts/ and chmod it as detailed above
    2) right-click any folder (the folder icon, not when you are in the folder itself) and select "scripts > randomcopy" from the menu.
    3) when prompted, select any folders with files of the extensions as listed in the script. Maybe one day I will make this more interactive.
    4) the script will then search quickly to see how many megabytes of appropriate files there are. it will then ask you how much you want to copy (you are limited only by how much space there is on the device you are copying to).
    5) let it run! It only tends to take as long as it does to transfer files from one place to another. Of course, as with all programs, your mileage may vary.


    The variables in the script may interest you as well:

    minFS is the smallest and maxFS is the largest you want a file to be when copying.
    In this case i have them at 0.5MB and 40MB respectively. Anything larger or smaller will be ignored during deepscan.

    If you want the script to handle more file types than this, you can add the appropriate extensions to the searchPattern variable (7th line down)

    # randomcopy 0.6
    # a general-purpose script for copying random files
    for tgtPath in "${tgtPaths[@]}" ; do
    	if [ -d "$tgtPath" ] ; then # startof if target directory
    		# Select....
    		O=$IFS IFS=$'\n' srcPaths=(`zenity --file-selection --directory --multiple --separator=$'\n' --title="Select source directories" --window-icon="$window_icon"`) IFS=$O
    		# Searching.. quickscan
    		echo "10"
    		for srcPath in "${srcPaths[@]}" ; do
    		#	=$(($srcB+$(echo `dosomething` | bc)))
    			srcB=$(($srcB+$(echo `find "$srcPath" -type f -size +$(($minFS-1))c -size -+$(($maxFS+1))c -iregex $searchPattern -print0 | xargs -0 stat -c %s+ ; echo 0` | bc)))		
    		echo "100"
    		# How many?....
    		tgtBFree=$((`stat -f -c %f "$tgtPath"`*`stat -f -c %s "$tgtPath"`)) 
    	 	if [ $srcB -lt $tgtBFree ]; 
    			then dialog_entry_text=$(($srcB/$bInMB))
    			else dialog_entry_text=$(($tgtBFree/$bInMB))
    		remSrcB=$(($bInMB*`zenity --entry --text="$(($srcB/$bInMB))MB of files $(($minFS/$bInMB))-$(($maxFS/$bInMB))MB found. $(($tgtBFree/$bInMB))MB free on device of /$(echo "$tgtPath" | sed 's/^.*\///'). How much to copy?" --entry-text="$dialog_entry_text" --title="$window_title" --window-icon="$window_icon"`))
    		if [ $srcB -gt 0 ]; then # startof if not 0 Bytes
    			# Searching.... deepscan
    			echo "10"
    			for srcPath in "${srcPaths[@]}" ; do
    				O=$IFS IFS=$'\n' tempSrcF=( `find "$srcPath" -type f -size +$(($minFS-1))c -size -+$(($maxFS+1))c -iregex $searchPattern` ) IFS=$O
    				remSrcF=( "${remSrcF[@]}" "${tempSrcF[@]}" )
    			echo "100"
    			# Copying....
    			while [ ${#remSrcF[@]} -gt 0 ] && [ $remSrcB -ge $minFS ] ; do 
    				random=$(($RANDOM % $initSrcF)) 
    				if [ -n "${remSrcF[$random]}" ] ; then # if element is not empty
    					srcFS=$((`stat -c %s "${remSrcF[$random]}"`))
    					if [ $srcFS -le $remSrcB ] && ! [ -e "$tgtPath/$(echo "${remSrcF[$random]}" | sed 's/^.*\///')" ] ; then # last condition prevents overwrite
    						cp -i --reply=no "${remSrcF[$random]}" "$tgtPath"
    						remSrcB=$(($remSrcB-$srcFS)) # post-copy!
    					unset remSrcF[$random] # make element empty
    				echo $((100*($initSrcB-$remSrcB)/$initSrcB))
    			echo "100"
    			zenity --info --text="$(($srcCpB/$bInMB))MB successfully copied to $tgtPath" --title="$window_title" --window-icon="$window_icon"
    			) | zenity --progress --auto-close --percentage="$((100*($initSrcB-$remSrcB)/$initSrcB))" --text="copying $(($initSrcB/$bInMB))MB to $(echo "$tgtPath" | sed 's/^.*\///').." --title="$window_title" --window-icon="$window_icon"
    			# endof copying pipe
    			) | zenity --progress --pulsate --auto-close --percentage=0 --text="deepscanning selected folders.." --title="$window_title" --window-icon="$window_icon"
    			# endof deepscan find pipe
    		fi # endof if not 0 B
    		) | zenity --progress --pulsate --auto-close --percentage=0 --text="quickscanning selected folders.." --title="$window_title" --window-icon="$window_icon"
    		# endof quickscan find pipe
    	fi # endof if target directory
    # endof for tgtPath
    if you encounter any problems please let me know.
    Last edited by lapsey; May 10th, 2006 at 10:11 PM. Reason: slight change to script


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts