# Need you help to debug this code

Hi guys

The script listed below is to check if any files or folders contain special characters , zip them and rename the zip files with “" replacing the character. I test it but if there are more than two files in same folder, this program will neither zip them all nor rename them with "” all. Can anyone help me with this code?

Another thing is I dont understand the logic of function “to swich text…” this code was copyed from a post and I doubt that maybe it was the cause.


set shareUser to "administrator" --area to change
set sharePass to "password" --area to change
set shareLoc to "10.11.22.115/share" --area to change
set mountDir to "share"
--
set volumesDir to ":users:administrator:desktop:" & mountDir
set cmdDir to "/users/administrator/desktop/" & mountDir
--
set jobFolder to (("Users:administrator:Desktop:") & "job:") -- path to desktop points to desktop folder of the current user
--

---
to switchText from t to r instead of s
local d
set d to text item delimiters
try
set text item delimiters to s
set t to t's text items
-- The text items will be of the same class (string/unicode text) as the original string.
set text item delimiters to r
-- Using the first text item (beginning) as the first part of the concatentation means we preserve the class of the original string in the edited string.
tell t to set t to beginning & ({""} & rest)
set text item delimiters to d
on error m number n from o partial result r to t
set text item delimiters to d
error m number n from o partial result r to t
end try
t
end switchText

---
set badChars to characters of "*\\?\"|"
-- The plural name of "text item delimiters" would tend to indicate that you could use a list many such delimiters. In practice (on my machine) only the first one is effective.
-- In other words, when I tried "switchText from str to replacementStr instead of badChars" the only character that was replaced was the first: asterisk.
set str to switchText from str to replacementStr instead of contents of ch
end repeat
str

--//check if the connection to the remote location is available or not
set outp to (do shell script "ping -c 1 10.11.22.115 | grep '0% packet loss'") --area to change

if (outp = "1 packets transmitted, 1 packets received, 0% packet loss") then

-- // check if the mapped drive is available or not
try
alias (volumesDir) -- // do nothing if it exists
on error
-- // create the file if it does not exist
do shell script ("mkdir -p " & cmdDir)
end try

try
do shell script ("mount_smbfs //" & shareUser & ":" & sharePass & "@" & shareLoc & " " & cmdDir)
--display dialog cmdDir & " is mounted."

end try

-- //aumatically move folders
set sFolders to paragraphs of (do shell script "find " & quoted form of POSIX path of jobFolder & " -type d -mtime +360 -maxdepth 1")
if (sFolders is not equal to {""}) then

repeat with oneFolder in sFolders
set illegal_syntax to paragraphs of (do shell script "find " & quoted form of POSIX path of oneFolder & " -name '*[*\\\\?\"|]*'")

if (illegal_syntax is not equal to {""}) then

repeat with each_record in illegal_syntax

do shell script "/usr/bin/ditto -c -k -rsrc --keepParent " & quoted form of each_record & space & quoted form of (each_record & ".zip")

set zipped_no_syntax to replacebadchars((each_record & ".zip"), "_")

do shell script ("cp " & quoted form of (each_record & ".zip") & space & quoted form of zipped_no_syntax & space & "| rm " & quoted form of (each_record & ".zip"))

end repeat

end if

else
error number -128
end if



I haven’t run your code, but in the repeat loop starting: “repeat with each_record in illegal_syntax”, recall that “each_record” is a reference to the item, not the item itself so where you say “set zipped_no_syntax to replacebadchars((each_record & “.zip”), ““)” it might be better to say "set zipped_no_syntax to replacebadchars((contents of each_record & “.zip”), "”)”

After fixing some syntax problems (I had to insert a couple of end statements). The parts that I can run on my system (mostly the stuff inside the repeat with oneFolder in sFolders loop) seem to work OK to me. I point it at a folder where I have files with these POSIX filenames:

And when I run the applicable parts of your script, I end up with files with these POSIX filenames:

There were more than two files, and the only zip files I ended up with have the modified filenames. Since all of the files that have names like “foobar” will result in “foo_bar” after the replacement, I only got one zip file for those (whichever is the last one processed, “foo\bar” in my case). But for all the other filenames that are still unique after replacement, I get a zip file with the replaced filename.

So, as much as I can easily test, it works for me (as well as can be expected, given the many-to-one filename mapping that is being done).

The “cp old new | rm old combination seems a little odd to me. I would usually use “mv old new. There are other variations that might work, but I don’t think you want a pipe here (technically the rm command could delete the source file before the cp command has even gotten started copying it; it is unlikely, but possible given the semantics of the pipe construct). If mv is not acceptable, you might try the previous-command-was-successful conditional command separator (double ampersand &&) or the normal unconditional command separator (semicolon instead of the pipe (vertical bar |).

Model: iBook G4 933
AppleScript: 1.10.7
Browser: Safari 3.0.4 (523.12)
Operating System: Mac OS X (10.4)

Here’s one way (due to Nigel Garvey) of replacing files with the same names:

tell application "Finder"
set source_name to name of this_item
set target_path to (DoneFolder as Unicode text) & source_name
try
target_path as alias -- This will error if the name's not already taken.

-- No error? A file with that name already exists in the DoneFolder.
-- Find a new name for that file.
set suffix to 1
repeat -- until error
try
target_path & "." & suffix as alias
set suffix to suffix + 1
on error
-- When the coercion finally fails, the name tried isn't in use.
-- Rename the existing file and exit the repeat.
set name of file target_path to source_name & "." & suffix
exit repeat
end try
end repeat
end try
-- Now move the new file into the folder.
set source_file to (move this_item to DoneFolder)
end tell



thanks guys, I will test all your suggestios at once right after i get back to office. wish you all have nice weekend.

I’ve changed that “cp | rm” into “mv” and the problem is solved. thanks alot for your help