Searching Script for file's with a space

Hi guys.

We have a problem on our SMB share that files saved with a space at the end of the name dont get backed up along with the rest of the share. Files with spaces inbetween words are not a problem, “file 1” it only when the space is at the end it doesnt work "file1 "

Is there a way of searching this share, including sub folders for files with spaces at the end??

Theres about 260 different users on this share so going through one by one will take forever!!

Cheers

You could write a tree crawler (lots of examples available here) that checks the last letter of each file name and adds it to a list if it’s a space. It would be easier to go to finder, hit command-f, and look for names end with . Or do shell script… http://www.hmug.org/man/1/find.php. You can use the dir command on a windows machine too. Or get the backup system’s prefs set so it doesn’t skip these files in the first place. Good luck! :slight_smile:

command + f doesnt work fully, it ony finds afew of the MANY files that have spaces ta the end of them.

Ive tryied using the find command in terminal but i can only get it to list files with spaces in the entire name, not just files with space at the end of the name.

Any more ideas??

ok, ive found this code works in the terminal;


find /volumes/art -name "* .*" -type f -print > /private/var/root/Desktop/files.txt

i cant get it to work in apple script tho with “do shell script”

hi dg,

i wish you’d post your attempts! :smiley:

try this:


set normalCommand to "/usr/bin/find /volumes/art -name \"* .*\" -type f -print > /private/var/root/Desktop/files.txt"

do shell script normalCommand with administrator privileges

remember that you always want to give a full path to executables in your shell scripts. use the ‘which’ command to find the path easily. like this: “which find”. since a lot of people have this problem, here is an AppleScript to help:


set theQ to "What shell command are you looking for?"

set theAns to text returned of (display dialog theQ default answer "")

set thePath to (do shell script "/usr/bin/which " & theAns)

display dialog "The command you are looking for is at: " & return & return & thePath

note that you could put multiple command line executables in this if you just separate them by a space. i hope this helps.

do shell script "find /Users/myName/Desktop/MyFolder -name \"* .*\" -type f -print > /Users/myName/Desktop/files.txt"

works for me.

In more friendly form:

set tFolder to POSIX path of (choose folder)
set tFilter to quoted form of "*t.*"
set tOutput to POSIX path of (path to desktop as text) & "Filtered_Files.txt"

do shell script "find " & tFolder & " -name " & tFilter & " -type f -print > " & tOutput

Returns a file with every name that ends in “t”.

Thanks Waltr!

Ill try that at work tomorrow!

sorry about not posting my atempts aswell, i will next time, give you all a good laugh =P

theres another part to this script that im going to have ago at tomorrow aswell;

now i have a list of all the files that end in a space or more than one space at the end of there name(80 ish), i would like to remove that space using a script.

something like…

Save the filename into a variable,
tell applescript to delete the ascii code for space (32???) from the end untill the code changes.

ill give it a shot tomorrow but any other ideas of how to do it are welcome =]

hi dg,

it’s not to laugh at you, but rather if i can see where you are going wrong i can help you learn. that’s what it’s all about. :slight_smile:

i’ll check into your other stuff later. if you try first and have problems i’ll be checking.

thanks for all the replys,

ive looked into the find command more and cant seem to find a way of pauseing it on each file it finds.

from there its just a case of creating an IF routine to find if the last character is a space and deleting it untill there last character is any other character other than a space.

then moving to the next file on the list.

Heres what i have so far;


	set vFolder to POSIX path of (choose folder)
	set vFilter to quoted form of "* .*"
	set vOutput to POSIX path of (path to desktop as text) & "Filtered_Files.txt"
	
	do shell script "find " & vFolder & " -name " & vFilter & " -type f -print > " & vOutput
	
        set vFile to first text item of vOutput
	
	if last character of vFile is " .*" then
		display dialog "y"
	else
		display dialog "n"
	end if

cheers

hi dg,

regardless of how you generate the file, you’ll have to open the file to get the contents, something like this:


set accessFile to open for access vOutput without invisibles
set theInfo to read accessFile
set theLine to every paragraph of theInfo as list

then you use the lines to find the individual files and worry about changing the name.

sorry, i’m so slammed today or i’d add more.

I’ve been following this thread, and even wrote some script to remove the space found in a name, and rename the file. I stopped, however, when it occured to me that I had no idea whether it was possible to change the name of a file on an SMB share using an AppleScript on another machine. The script is in pieces, several of which could be combined to shorten and optimize the script, but I didn’t do that. Here’s what I have:

set vFolder to text items 1 thru -2 of POSIX path of (choose folder) as Unicode text -- remove the slash because it's a folder or you'll get a double slash later
set vFilter to quoted form of "* .*" -- space before period
set badFiles to paragraphs of (do shell script "find " & vFolder & " -name " & vFilter) -- a list of POSIX paths.

-- get the "bad" names. This could easily be combined with fixing them, but didn't get there before I stopped.
set badNames to {}
repeat with aFile in badFiles
	set end of badNames to baseName(aFile)
end repeat
-- fix the "bad" names
set fixedNames to {}
repeat with aName in badNames
	set end of fixedNames to fixName(aName)
end repeat
-- Coerce POSIX paths to AppleScript paths
set ASFPaths to {}
repeat with aFile in badFiles
	set end of ASFPaths to POSIX file aFile as string
end repeat

-- At this point you have the bad file paths (as colon-delimited AppleScript paths) and the repaired names, so it is fairly straight forward to correct the names of the files. I stopped because I was not at all certain that the Finder could change the name of an SMB share (and I don't have one to test).

-- Extracts the base name from a POSIX path, jj's script
to baseName(thePath) -- Requires POSIX path
	if thePath ends with "/" then
		set nameIndex to -2
	else
		set nameIndex to -1
	end if
	set ASTID to AppleScript's text item delimiters
	set AppleScript's text item delimiters to "/"
	set thePath to text item nameIndex of thePath
	set AppleScript's text item delimiters to ASTID
	return thePath
end baseName

-- Assuming no blank names or double extensions, get rid of the space
to fixName(N) -- assumes no blank names
	set temp to reverse of characters of N -- could have done this with reverse counting - didn't get there.
	set c to count temp
	repeat with k from 1 to c
		if item k of temp is "." and item (k + 1) of temp is " " then
			set idx to k + 1
			exit repeat
		end if
	end repeat
	set item idx of temp to missing value
	set newName to (reverse of temp) as string -- ignores the missing value (the space before)
	return newName
end fixName

thanks for that script, ive tried it but it doesnt seem to work, it displays the last file on the smb share with a space at the end of the name but doesnt seem to remove the space. unless im doing somthing wrong, ive just copied your script in and ran it.

ill have a play around with yours and my own to see what i can come up with

cheers again

Oh, too bad. As I said above, I don’t have access to a share to test, so the script doesn’t change the name of the file itself, it just changes the name and stores it in a list.

If you add this line just above the comment that says “At this point you have…”, that is, just ahead of the handlers, you’ll see the badNames list, the fixedName list, and the paths to those files (or at least I do for a folder on my own machine)

{badNames, fixedNames, ASFPaths}

Try this with your share and see if it does it correctly (no changes to the share, just finding the badNames, fixing them, and saving the AppleScript form of the path to each file).

To actually change the name, you use the Finder. Try this version which works for me, and note that it could definitely use a ton of optimizing and combining functions to make it faster.


set vFolder to quoted form of (text items 1 thru -2 of POSIX path of (choose folder) as Unicode text) -- remove the slash because it's a folder or you'll get a double slash later
set vFilter to quoted form of "* .*" -- space before period
set badFiles to paragraphs of (do shell script "find " & vFolder & " -name " & vFilter) -- a list of POSIX paths.

-- get the "bad" names
set badNames to {}
repeat with aFile in badFiles
	set end of badNames to baseName(aFile)
end repeat
-- fix the "bad" names
set fixedNames to {}
repeat with aName in badNames
	set end of fixedNames to fixName(aName)
end repeat
-- get the paths
set ASFPaths to {}
repeat with aFile in badFiles
	set end of ASFPaths to POSIX file aFile as string as alias
end repeat
-- change the names
set c to count badNames
repeat with k from 1 to c
	tell application "Finder" to set name of item k of ASFPaths to item k of fixedNames
end repeat

-- Extracts the base name from a POSIX path
to baseName(thePath) -- Requires POSIX path
	if thePath ends with "/" then
		set nameIndex to -2
	else
		set nameIndex to -1
	end if
	set ASTID to AppleScript's text item delimiters
	set AppleScript's text item delimiters to "/"
	set thePath to text item nameIndex of thePath
	set AppleScript's text item delimiters to ASTID
	return thePath
end baseName

-- Assuming no blank names or double extensions, get rid of the space
to fixName(N) -- assumes no blank names
	set temp to reverse of characters of N
	set c to count temp
	repeat with k from 1 to c
		if item k of temp is "." and item (k + 1) of temp is " " then
			set idx to k + 1
			exit repeat
		end if
	end repeat
	set item idx of temp to missing value
	set newName to (reverse of temp) as string
	return newName
end fixName

That works, altho it crashes after renameing about 1/3 of the files on this line


set name of item k of ASFPaths to item k of fixedNames

The error say it cant get the alias of the file it crashes on, but if you run it again it works fine on that file and crashes after the next chunk of files is done.

Because you are getting the data required from a server over your LAN, it may be “crashing” because something is timing out. It’s not really a crash - the Finder tries to set the name and the server doesn’t respond as expected. I have no way of testing for that. The “standard” timeout for an AppleScript is 60 seconds. You might try to increase that:

with timeout of 120 seconds
	repeat with k from 1 to c
		tell application "Finder" to set name of item k of ASFPaths to item k of fixedNames
	end repeat
end timeout

If that doesn’t work, I’m at a loss from a distance.

my mistake!

it was carshing because the subfolder had a space at the end of it.

but the script works perfect!!

thanks =]

dave