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
Related
This is a linux problem
So I need to make a folder called "Notes", and inside that folder I need to make 6 files. The files are named with ID number, as follows:
a00144998.txt
a00154667.txt
a00130933.txt
a00143561.txt
a00157888.txt
The first 3 letter are always "a00", and the 4th & 5th letter are the year. So I now need to copy the ID number/title of file without the '.txt' extension, and paste it on a new file outside the folder "Notes"
Example file a00144998, it shows year 14(from the 4th & 5th letter), so I will copy a00144998 to a new file named "year14.txt" and sort it. Same as a00154667, it shows year 15, so I will copy a00154667 to a new file named "year15.txt". So at the end, file "year14.txt" will have :
a00143561
a00144998
I have found the code, and it works if the files are not in the folder. But once I create files inside folder, this code doesn't work, it keeps copying the txt extension. Any idea? Thanks!
ls ~/Notes/???14????.txt|sed 's/.\[4\]$//'|sort>year14.txt
No need for sed, you can do it all in a bash oneliner:
for f in a0014*.txt; do echo ${f:5:4}; done | sort > year14.txt
This will loop over every file matching the glob a0014*.txt and put each string in an f variable, echo out 4 characters starting after the 5th character.
TLDP has a great guide on string manipulation in bash.
You should also avoid parsing ls. It's meant to be human and not machine readable.
I have a code which enables templates to be saved in a fixed directory with custom but suggested filenames. However, I also want to apply this to a different template in a directory whith a space in its name.
I read in a different question an answer about spaces making it more complex. (GetSaveAsFilename default folder) an answer:
this works:
x = Application.GetSaveAsFilename(InitialFileName:="C:\mydocuments\music\", _
fileFilter:="Text Files (*.*), *.*")
However, if you have spaces in the filespec it gets a little trickier. For example, this:
x = Application.GetSaveAsFilename(InitialFileName:="%USERPROFILE%\My Documents\My Music", _
fileFilter:="Text Files (*.*), *.*")
Only gets as far as My Documents and thinks that My Music is the filename. Hope this helps.)
So, what to do to have the right directory with a space in its name?
Enclose your path in quotes:
"""%USERPROFILE%\My Documents\My Music"""
to get spaces recognized as part of the path instead of separator.
I've got an AppleScript file to replace filenames which are supplied through a CSV file. While I've got the script to work, it has issues with encoding of the strings/filenames.
The finding of files works perfectly. Renaming it to something like Malmö results in a very weird encoded string.
The CSV originates from Microsoft Excel, and I suspect is not properly UTF-8 encoded. And now I'm stuck in how to handle the encoding properly. (or how to convert the encoding). As far as I know it has the default Excel encoding ISO 8859-1.
set theFile to (choose file with prompt "Select the CSV file")
set thePath to (choose folder with prompt "Select directory") as string
set theCSVData to paragraphs of ((read theFile))
set {oldTID, my text item delimiters} to {my text item delimiters, ";"}
repeat with thisLine in theCSVData
set {oldFileName, newFileName} to text items of thisLine
if length of oldFileName > 0 then
set oldFile to thePath & oldFileName
set newFile to newFileName
tell application "System Events"
if exists file oldFile then
set name of file oldFile to newFile
end if
end tell
end if
end repeat
So my question is, how do I fix the encoding issue, either by reading it properly or by encoding the file first (through applescript)
I really think the filenames are encoded using utf16LE, but I can be wrong. Anyhow, there is a utility you can access via Terminal, that is named iconv (man iconv), that you can experiment with, and maybe re-encode the filename with. If you receive the output from a do shell script as text, or unicode text, then you get back utf16, so it won't be corrupted after the conversion. (Before you use it as a filename).
Use:
set theCSVData to paragraphs of ((read theFile as «class utf8»))
I'm relatively new in trying things out in the command prompt.
In the end I'd like to write a batch file that can do the following things.
Look for files whose name does not contain "foo" or "bar" with the extension "txt"
in the current directory, including all sub directories,
and search for lines starting with "create file" or "create table,"
so that all corresponding matching lines would be put into a new file named "y"
In the form of name of file in which the line was found, location of the file, and the actual line containing the string.
Start by looking at these commands.
Commands
for,
find,
findstr
Use the /? argument for help.
Batch References
SS64,
DosTips,
Rob van der Woude,
Technet
here is the trouble ... i'm dynamically building (rather changing)a string which contains numerals(numbers) (like to have filename out01.txt ,out02.txt etc ..)
my program works fine (i'm using the last updated value string to name a file and edit that file) ... but in the same directory with "ls" command, i can see that file created and through file browser i can acess it but from command line using vim , gedit i can't open it new file of same name is opening... moreover i can't remove that file from command line (rm out010.txt'
no such file or directory) here is the code , i might not have been able to explain my problem but code will speak for itself ...
program strtest
implicit none
character(len=1024)::filen,format_str
integer::i
format_str="(a5,i0.3,'.txt')"
do i=1,10
write(filen,format_str)'out',i
end do
write(*,*)trim(filen)
open(23,file=trim(filen))
write(23,*)"what a mess!"
close(23)
stop
end program strtest
note: i have the same problem even without using trim() function in file opening statement
please explain my situation!!
regards ...
Your filenames are coming out with 2 spaces in front of them, so if you put rm " out01.txt" (2 spaces,out01.txt) you will be able to delete them. It's the a5 that's throwing off the format string.
As #jonsca pointed out already, the problem is with the extra whitespace. The simplest way to get rid of it is to use adjustl, like this:
open(23,file=trim(adjustl(filen)))