Page 3 of 3 FirstFirst 123
Results 21 to 22 of 22

Thread: Random file copying script

  1. #21
    Join Date
    Mar 2009
    Beans
    87

    Re: Random file copying script

    Ahahaha, it's working! I missed out a vital part - I was trying to sync to a local directory testing it out. As soon as I mounted a USB device the script ran, said it copied but it didn't. I went back to your post in 2009 and tweaked the cp -i line, it now works superbly.
    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 -L "${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
    Thanks for that!

  2. #22
    Join Date
    Mar 2005
    Location
    Lancashire, NW England UK
    Beans
    169
    Distro
    Lubuntu 13.04 Raring Ringtail

    Re: Random file copying script

    Wonderful. Glad you got it going!
    Momist. Follower of the minor Greek god Momer.

Page 3 of 3 FirstFirst 123

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
  •