Page 1 of 3 123 LastLast
Results 1 to 10 of 22

Thread: Random file copying script

  1. #1
    Join Date
    Jun 2005
    Location
    the belly of a snake
    Beans
    Hidden!

    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.

    Variables

    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)

    Code:
    #!/bin/bash
    # randomcopy 0.6
    # a general-purpose script for copying random files
    
    bInMB=1048576
    window_title="Randomcopy"
    window_icon="/usr/share/icons/gnome/16x16/devices/gnome-dev-ipod.png"
    searchPattern=".*\.\(aac\|aa\|mp3\)"
    
    minFS=$(($bInMB/2))
    maxFS=$(($bInMB*40))
    
    ######################################################################
    
    O=$IFS IFS=$'\n' tgtPaths=(`echo "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS"`) IFS=$O
    
    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)))		
    		done
    		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))
    		fi
    		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"`))
    		initSrcB=$remSrcB
    
    		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[@]}" )
    			done
    			initSrcF="${#remSrcF[@]}"
    			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!
    						srcCpB=$(($srcCpB+$srcFS))
    					fi
    					unset remSrcF[$random] # make element empty
    				fi
    				echo $((100*($initSrcB-$remSrcB)/$initSrcB))
    			done
    			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
    
    done
    # endof for tgtPath
    exit
    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

  2. #2
    Join Date
    Oct 2005
    Location
    Sao Paulo, SP, Brazil
    Beans
    133
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Random file copying script

    This was very usefull for me. I've been looking for something like this for some weeks. Every ipod supporting app seems to completely forget about ipod shuffle

  3. #3
    Join Date
    Jun 2005
    Location
    the belly of a snake
    Beans
    Hidden!

    Exclamation A more specific modification for the shuffle.

    I mentioned earlier how the script could be used with the ipod shuffle. It has since occurred to me that 99.9% of people that use the shuffle on linux would use the iPod shuffle Database Builder script.
    I also dislike waiting for prompts, so i have included a variable (useall) that allows the script to automatically use all free space, rather than prompt you to enter a number.

    Most significant is that this script removes any leading non-alphabetic characters from the filenames. This is so that the ipod shuffle db builder can group files together better. For example:
    Code:
    01.mp3
    02 - 2802.3478.mp3
    02. elvis - jailhouse rock.mp3
    03 - gnr - don't cry.mp3
    07_-_talking heads_-_girlfriend is better.mp3
    09.aphex twin - digeridoo.mp3
    12.vbr.mp3
    15, elvis, in the ghetto.mp3
    aphex twin - come to daddy - 05 - bucephalus.mp3
    becomes
    Code:
    01.mp3
    02 - 2802.3478.mp3
    aphex twin - come to daddy - 05 - bucephalus.mp3
    aphex twin - digeridoo.mp3
    elvis - jailhouse rock.mp3
    elvis, in the ghetto.mp3
    gnr - don't cry.mp3
    talking heads_-_girlfriend is better.mp3
    vbr.mp3
    obviously it will benefit you more if you have a consistent mp3 naming scheme


    installation and usage:

    1) Save the iPod shuffle Database Builder script to the root of the shuffle.

    2) Save this script also to the root of the shuffle and make executable (as mentioned in the first post)

    3) Edit the script so the variables such as rebuild_db to point to different locations if you wish. I make use of gnome's habit of hiding files with a period at the start, so I have ".rebuild_db.py" and ".randomcopy" at the pod root. In order to run the script, I keep a launcher in the ipod root with the command bash /media/ipod/.randomcopy



    4) Make whatever changes you wish to the other variables so the script operates the way you want it, for example commenting out useall if you like

    5) run it, select the folders you want to search


    and wait for the script to find and copy your files.


    Once done, the script will pass you the results of the ipod shuffle database builder.



    6) Unmount and Enjoy!

    Code:
    #!/bin/bash
    # randomcopy 0.6b
    # a general-purpose script for copying random files 
    # modified specifically for the ipod shuffle and the ipod shuffle database builder script
    
    bInMB=1048576
    
    window_title="Randomcopy 0.6b"
    window_icon="/usr/share/icons/gnome/16x16/devices/gnome-dev-ipod.png"
    tgtPath="/media/ipod/music" # where you wish to copy your music files to. if it doesn't exist the script will create it
    dbPath="/media/ipod/iPod_Control" # where the itunes db is kept
    rebuild_db="/media/ipod/.rebuild_db.py" # where the ipod shuffle database builder script is kept: this should always be on the root of the ipod
    
    redundancy=$(($bInMB/2)) # 1/2 MB is about enough space for the ipod shuffle database, overall
    searchPattern=".*\.\(aac\|aa\|mp3\)" # file extensions to copy
    minFS=$(($bInMB/2)) # smallest filesize to copy, in bytes
    maxFS=$(($bInMB*40)) # largest filesize to copy, in bytes
    
    useall=yes # comment-out this variable if you wish to be prompted for how many MB of files to copy, otherwise it will automatically use whatever space is left
    
    ######################################################################
    
    if ! [ -e "$tgtPath" ] ; then mkdir "$tgtPath" ; fi
    
    dbFS=`du "$dbPath" -b -s | grep -o '^[0-9]*'`
    if [ $redundancy -gt $dbFS ] 
    	then redundancy=$(($redundancy-$dbFS))
    	else redundancy=$dbFS
    fi 
    
    tgtBFree=$(((`stat -f -c %f "$tgtPath"`*`stat -f -c %s "$tgtPath"`)-$redundancy))
    
    O=$IFS IFS=$'\n' srcPaths=(`zenity --file-selection --directory --multiple --separator=$'\n' --title="Select source directories" --window-icon="$window_icon"`) IFS=$O
    
    (
    
    echo 10
    for srcPath in "${srcPaths[@]}" ; do
    	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)))		
    done
    echo 100
    
    if [ $useall ]
    	then 
    		if [ $srcB -lt $tgtBFree ]
    			then remSrcB=$srcB
    			else remSrcB=$tgtBFree
    		fi
    	else 
    		if [ $srcB -lt $tgtBFree ]
    			then dialog_entry_text=$(($srcB/$bInMB))
    			else dialog_entry_text=$(($tgtBFree/$bInMB))
    		fi
    		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"`))
    fi
    			
    initSrcB=$remSrcB
    
    if [ $srcB -gt 0 ] ; then # startof if not 0 Bytes found on quick scan
    
    	(
    
    	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[@]}" )
    	done
    	initSrcF="${#remSrcF[@]}"
    	echo 100
    
    	(
    
    	while [ ${#remSrcF[@]} -gt 0 ] && [ $remSrcB -ge $minFS ] ; do 
    		random=$(($RANDOM % $initSrcF)) 
    		if [ -n "${remSrcF[$random]}" ] ; then # if element is not empty
    			tgtFN_np=$(echo "${remSrcF[$random]}" | sed 's/^.*\///') # strips leading path
    			if [[ $tgtFN_np =~ '[a-zA-Z].*\.[^.]*$' ]] ; then tgtFN_np=$(echo "$tgtFN_np" | sed 's/^[^a-zA-Z]*//') ; fi # strip all nonalpha char before last extn
    			srcFS=$((`stat -c %s "${remSrcF[$random]}"`))
    			if [ $srcFS -le $remSrcB ] && [ $((`expr length "$(echo "$tgtPath" | sed 's/^.*\///')/$tgtFN_np"`)) -le 255 ] && ! [ -e "$tgtPath/$tgtFN_np" ] ; then # first condtn checks for space to copy, second condtn checks path not too long for ipod db rebuilder (255), third condition prevents overwrite
    				cp -i --reply=no "${remSrcF[$random]}" "$tgtPath/$tgtFN_np"
    				remSrcB=$(($remSrcB-$srcFS)) # post-copy!
    				srcCpB=$(($srcCpB+$srcFS))
    			fi
    			unset remSrcF[$random] # make element empty
    			echo $((100*($initSrcB-$remSrcB)/$initSrcB))
    		fi
    	done
    	echo 100
    
    	python "$rebuild_db" -l | zenity --text-info --title="iPod shuffle Database Builder" --window-icon="$window_icon"	
    
    	) | zenity --progress --auto-close --percentage=0 --text="copying $(($initSrcB/$bInMB))MB to $(echo "$tgtPath" | sed 's/^.*\///').." --title="$window_title" --window-icon="$window_icon"
    
    	) | zenity --progress --pulsate --auto-close --percentage=0 --text="deepscanning selected folders.." --title="$window_title" --window-icon="$window_icon"
    
    fi
    
    ) | zenity --progress --pulsate --auto-close --percentage=0 --text="quickscanning selected folders.." --title="$window_title" --window-icon="$window_icon"
    
    exit
    again if there are any problems or changes I should look into please post about them
    Last edited by lapsey; June 10th, 2006 at 10:24 AM. Reason: fixed bug

  4. #4
    Join Date
    Jul 2006
    Location
    ONTARIO, CANADA
    Beans
    68
    Distro
    Ubuntu 6.10 Edgy

    Re: Random file copying script

    Dude, you rock!

  5. #5
    Join Date
    May 2005
    Location
    US
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: Random file copying script

    This sounds like a very cool script. If I get too lazy to manually drag songs to my Sandisk player, I may give this a go.

    Thanks for creating this!

  6. #6
    Join Date
    Apr 2006
    Beans
    86

    Re: Random file copying script

    Quote Originally Posted by aysiu View Post
    This sounds like a very cool script. If I get too lazy to manually drag songs to my Sandisk player, I may give this a go.

    Thanks for creating this!
    Will it work on an Iaudio (mounted in UMTS) please?

    Obviously, I won't need the db builder :d

  7. #7
    Join Date
    Jun 2005
    Location
    the belly of a snake
    Beans
    Hidden!

    Re: Random file copying script

    Quote Originally Posted by Ago12 View Post
    Will it work on an Iaudio (mounted in UMTS) please?

    Obviously, I won't need the db builder :d
    if the device's storage can be mounted to a location on the file system, (like /media/ipod/), then the ipodified script (0.6b) should work fine

    to stop the db_builder from being run, remove the line

    Code:
    python "$rebuild_db" -l | zenity --text-info --title="iPod shuffle Database Builder" --window-icon="$window_icon"
    from randomcopy 0.6b

    the variables you may need to change (tgtPath, redundancy) are explained in the script itself.
    Last edited by lapsey; November 20th, 2006 at 07:13 PM.

  8. #8
    Join Date
    Apr 2006
    Location
    Fairmont, WV
    Beans
    110

    Re: Random file copying script

    Dude. Awesome. Wanted something like this for a long time. Thanks a bunch.

  9. #9
    Join Date
    Feb 2006
    Beans
    5

    Re: Random file copying script

    This script is exactly what I wanted!
    Before I tried it I wanted to make sure it works on the new V2 "clip" Shuffles. Has anyone tried it yet?

  10. #10
    Join Date
    Jul 2007
    Location
    Augusta, GA
    Beans
    216
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Random file copying script

    I'm obviously way late to the party here. Is there a way to make this copy over a specific number of random files? As dumb as it sounds, I am 113 songs away from hitting 8000 over on last.fm and I wanted to be able to drag over 113 songs into my iPod and toss it on shuffle to be able to hit the milestone.

Page 1 of 3 123 LastLast

Bookmarks

Posting Permissions

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