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
---
to replacebadchars(str, replacementStr)
local badChars
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.
repeat with ch in badChars
set str to switchText from str to replacementStr instead of contents of ch
end repeat
str
end replacebadchars
--//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