AppleSctipt Finding File with partial filename - get

I am an applescript noob and i need help getting a certain log file that has a long string of numbers in its title. The number changes and the filename before the number string is unique to the folder it is in. Is there some way i can use the "starts with" to retrieve the file with only using part of the filename and make a variable to work with it?

This is best done with shell script. Use "do shell script" if you must do it in AppleScript. You can concatenate the string part of do shell script with whatever you want, for example:
set file_start to "file_start"
set file_ext to ".txt"
do shell script "mdfind -name " & quoted form of file_start & "|grep " & quoted form of (file_start & "[0-9]*" & file_ext & "$")
replace file_start in quotations with your file's non-changing beginning, and replace the extension with whatever you're using.

do shell command "find ~/some/directory -iname *somefile*"
This actually ended up working a little better.

Related

PowerShell unable to remove space between text and String

In PowerShell I am trying to find a way to remove the text from an output of text and a String.
Write-Host 'File located at C:\'$Fileline.FilePath -
I get an output of
c:\ program files\path
The space between c:\ and "Program files" is what I want to remove. Do I have to convert the text to a string, and then output it as two strings and then remove the spaces?
This is happening because you are passing multiple strings to Write-Host, which it is then joining with spaces. This behaviour is somewhat unique to Write-Host.
You can meet your need by sending a single double quoted string to Write-Host, which you can then put your variable inside and it will be expanded. However because you are accessing a property of your variable, you need to wrap it in a sub-expression: $():
Write-Host "file located at C:\$($Fileline.FilePath) -"
Try using the PowerShell -f formatting operator:
Write-Host ("File located at C:\{0} -" -f $FileLine.FilePath)
There's good info on -f at SS64 and at TechNet

Search and replace files (Linux)

I'm quite new to Linux. I'm using Linux Mint and I've just found a situation where I have a file which exists multiple times inside the tree/folders of a folder. I want to replace all occurrences of this file with a new version of it.
So instead of looking for that file once and again and replacing it with the new one, I wonder if there is any kind of search & replace command for files.
I've already searched for a similar question in stackoverflow, but I was only able to find commands to search & replace TEXT in files, not the file itself.
Can anyone please point me to the right direction?
Thank you.
you can always do it in parts, like:
Get a list of items matching your search.
Replace every match (using mv for example) with your file.
something like:
foreach dir ( `ls | egrep '^(i686\|amd64)\.'` )
mv yourfile $dir
end

Find space escape

Writing a small script in bash (MacOS in fact) and I want to use find, with multiple sources. Not normally a problem, but the list of source directories to search is held as a string in a variable. Again, not normally a problem, but some of them contain spaces in their name.
I can construct the full command string and if entered directly at the command prompt (copy and paste in fact) it works as required and expected. But when I try and run it within the script, it flunks out on the spaces in the name and I have been unable to get around this.
I cannot quote the entire source string as that is then just seen as one single item which of course does not exist. I escape each space with a backslash within the string held in the variable and it is simply lost. If I use double backslash, they both remain in place and again it fails. Any method of quoting I have tried is basically ignored, the quotes are seen as normal characters and splitting is done at each space.
I have so far only been able to use eval on the whole command string to get it to work but I felt there ought to be a better solution than this.
Ironically, if I use AppleScript I CAN create a suitable command string and run it perfectly with doShellScript (ok, that's using JXA, but it's the same with actual AppleScript). However, I have so far been unable to find the correct escape mechanism just in a bash script, without resorting to eval.
Anyone suggest a solution to this?
If possible, don't store all paths in one string. An array is safer and more convenient:
paths=("first path" "second path" "and so on")
find "${paths[#]}"
The find command will expand to
find "first path" "second path" "and so on"
If you have to use the string and don't want to use eval, split the string into an array:
string="first\ path second\ path and\ so\ on"
read -a paths <<< "$string"
find "${paths[#]}"
Paths inside string should use \ to escape spaces; wraping paths inside"" or '' will not work. eval might be the better option here.

How to add characters to a string between specific characters in bash?

Right now I'm working on creating a script in linux bash shell that adds the word "-BACKUP" to a file name between certain points. For example, if I had a file/string called file1.txt I would want to add the "-BACKUP" between "file1" and ".txt" to make "file1-BACKUP.txt". How would I go about doing that? Would I use the basename command anywhere? In this situation, the extension and stem could be anything, not just what I gave as an example. All help is appreciated!
Use the substring processing parameter expansion operator % to remove the suffix from the string, then append the new text. The variable must be enclosed in braces for substring processing parameter expansion to work.
var="file1.txt"
echo "${var%.txt}.BACKUP.txt"

How i write file into home(In linux system)

I am trying to write file in my home folder(I am using Linux operating system) while i am writing the file into temp it's working
put shell("echo $HOME") into last1
The above code for getting home folder and I am place the path into variable last1
put the text of field "bash1" into URL "file:last1/dic.sh"
Here bash1 is an text field and it's contains some shell script i want to write into home directory
The below code is Works
put the text of field "bash1" into URL "file:/tmp/dic.sh"
How i rewrite my code
As your last1 variable is enclosed in quotes, it's getting treated as a literal string rather than a variable. The following would work:
put field "bash1" into URL ("file:" & last1 & "/dic.sh")
Note that you do not have to refer explicitly to the text property when putting texts from fields - you can do the above. Furthermore, if you're on Linux, you can just use the ~ shortcut to refer to the user's home directory:
put field "bash1" into URL "file:~/dic.sh"
You have to "open" the file, write your stuff and "close" the file again.
try something like
put last1 & "/dic.sh" into myFile
open file myFile for write
write the text of field "bash1" to file myFile
close file myFile

Resources