gvim on windows: file pointed to by tempname() does not exist - windows

I'm trying to use a vim script that issues commands to sas,
When i try to run the script i receive an error
"Can't open file ..path.. /AppData/Local/Temp/ .. temp file .. "
this is the path and file name returned by :tempname()
After navigating to the appdata temp files directory, the temp file does not exist.
I've tried manually changing the backup directory with set backupdir=~\tmp
(a directory I created)
but that doesn't change the return value of :tempname() nor does the tempfile actually exist.
Two questions,
1. Is there a way to ensure vim is writing the file the script seems to need
2. can i rewrite the script to avoid needing a temp file? (just pass the path to the actual file to sas?)
the relevant part of the script is
let returntxt = system("\"" .
\ shellescape("C:\\Program\ Files\\SAS\\SASFoundation\\9.2\\sas.exe") .
\ "\ -nosplash" . "\ -sysin" . "\ " .
\ shellescape(expand("%:p")) . "\"")
" Shows the return messages from the SAS commandline (may be useful
" if no log produced)
:echo "*** SAS commandline: " . returntxt

Option 'backupdir' contains a list of directories where backup files are generated, it has nothing to do with temporary directory (except that it depends on where it is located, opposite is not true). You should try using %TMP% or %TEMP% environment variable, either from vim using
let $TMP=expand('~/tmp')
or before vim is launched (don’t know how to do this on windows). Using let $TMP does work for me under wine, but only for existing temporary directories.
Note that your system() call is rather strange: shellescape() should already add all needed quotes so that surrounding "\"" are not required (if it does not you should properly configure 'shell*' options). You also don’t need to escape spaces: "\ " and " " are exactly the same strings.
About second question: you can try passing file contents as a second argument to system(), but if file may contain NULLs it is not going to work.

Related

How to get name of folder for Shell Script in Automator?

I'm using the newest version of macOS Monterey.
Sometimes I have to merge/combine all files that are in a specific folder into one txt file.
I currently do that by typing this in Terminal:
cd /Users/my_name/Desktop/test_folder ; cat * >merged.txt
This will merge/combine all files in folder test_folder into one file called merged.txt. The file merged.txt will get saved into the folder test_folder.
Every time I need this I have to open Terminal copy/paste the command and replace test_folder with the right folder name, since it's not always the same.
I want to make this easier by just make a right click on a folder, go to Quick Actions and select e.g. Merge all files to merge/combine all files inside the folder I just clicked on.
But I stuck at getting the folder name. How can I dynamically get the folder name and path I clicked on to start this Quick Action instead of the hard coded /Users/my_name/Desktop/test_folder?
Or, is there another and easier solution?
This is what I have so far:
I wouldn’t do this with AppleScript, especially if all it’s ultimately doing is calling out to a shell script.
Stick with the Run Shell Script action except change the option for passing the input as arguments rather than to stdin.
The folders selected in Finder will then be available to your script via $#, so you can do something like:
for d in "$#"; do
cat "$d"/* > "$d/merged.txt"
open -R "$d/merged.txt"
done 2>/dev/null
This loops through the selected directories and concatenates the files to merged.txt in the respective directory. The open -R line reveals the merged.txt file in Finder.
Errors are written to /dev/null, i.e. discarded, as cat will throw an error if any of the directories, themselves, contain directories.
Instead of adding a Run Shell Script to your workflow, try adding a Run AppleScript command instead. Copy this following AppleScript code to the Run AppleScript command.
on run {input, parameters}
try
do shell script "cd " & quoted form of POSIX path of input & " && cat *.txt > merged.txt"
on error
try
do shell script "cd " & quoted form of POSIX path of input & " && rm merged.txt"
end try
end try
end run

Bash Shell Script if statement issue and cp/date command issues

I am new to UNIX and am having some issues with my bash script and would like some help. My script is designed to copy files from one directory to a target directory while outputting which files are being copied, the current date each file is being copied, and an error message if not enough parameters/input from the user.
So far, I have three parameters/arguments. 1)sourcePath which is the directory that has files. 2)backupPath, the directory the user wants to copy files to from sourcePath (if backupPath is not a directory, then it will automatically be made), and 3)filePrefix which prompts the user to enter a certain letter or pattern and any file from sourcePath that starts with that letter or has that pattern will be copied. Couple of issues so far are that the files being copied are not being displayed, so the user doesn't know which files were copied until they cd into the backupPath directory and ls to see. The second problem is the current date not showing. The third problem is that I have an if statement for if not enough paramteters have been passed that will echo a certain message, but that message is echoed no matter what, even if all parameters have been given. Code is below:
read -r -p "sourcePath: " sourcePath
read -r -p "backupPath: " backupPath
read -r -p "filePrefix: " filePrefix
if [ $# -lt 3 ]; then
echo "Create backup files in a target directory given
the target directory name and a file name prefix.
Only files found in the specified source directory
whose name begins with the supplied prefix will
be copied. In addition, all copied echo files will
have a datestamp suffix added.
syntax: ./dobackup.bash sourcePath backupPath filePrefix"
fi
mkdir -p /home/public/"$backupPath"
cp -v /export/home/public/"$sourcePath/$filePrefix"* `(date +%y%m%d)`
So as said above, "cp -v" seems to not be working properly as it lists every file in the backupPath and sourcePath directories when it is to only display files being copied.
My if statement outputs whenever the script is executed, when it should only output if not all of the parameters/inputs have been met, or typed in by the user. I understand this is a lot of help to ask for but I am still new to UNIX and scripting. I know how to do all of this if I was to just input the separate commands myself but am having difficulties here. All help is appreciated.

problem with copying directory files to another directory

So I want to copy some files from one directory to another.
Essentially, I want to capture the directory path to a variable, say, "pathname" and use
"cp-r $pathname ." to copy file1 and file2 to to a new folder in which I made using mkdir and have cd'ed into (hence the "." as the second command line argument).
source_to_copy_from:
home/folder1/folder2/file1
home/folder1/folder2/file2
destination_to_copy_to:
home/newfolder1/newfolder2/file1
home/newfolder1/newfolder2/file2
I did:
pathname=$"$(pwd)"
//code to make the newfolder1 here
cp -r $"$pathname" .
But there appears to be nothing in the new pathname that was supposed to be copied in.
Also am using Mac bash
also quite beginner to bash

Locate a file anywhere on the hard drive and transfer it to a different folder

I'm trying to create an app to transfer any files with a certain name a different folder. So far, I have the following:
tell application "Finder"
move (every item of (get path to home folder) whose name is "extended image name.jpg") to ((get path to home folder) & "Pictures" as string)
end tell
While this doesn't return any errors, it doesn't do what I want it to either. I am also aware that this only searches the home folder, so if there is any way to do a broader search of the whole drive without having to enter a username, that would be great (I want this to be able to run on more computers than one without them having to edit the script.)
-Thanks
The code considers only files in the home folder but not in its subfolders, to consider all subfolders you have to add entire contents
tell application "Finder"
move (every item of entire contents of home whose name is "extended image name.jpg") to folder "Pictures" of home
end tell
But be aware that entire contents is extremely slow. The shell command find or a spotlight search with mdfind is much faster for example
set homeFolder to POSIX path of (path to home folder)
set picturesFolder to POSIX path of (path to pictures folder)
do shell script "mdfind -onlyin " & quoted form of homeFolder & " -0 'kMDItemDisplayName = \"extended image name.jpg\"' | xargs -0 -J {} mv {} " & quoted form of picturesFolder
Important Note:
As you are moving multiple files with the same name the Finder version will ask for overwriting and the shell version will overwrite all files with the same name.
When you use the Finder to search, if your folder contains only few hundreds files that's OK. But if you want to search in a folder containing thousands of files, Finder will take too long. In this case, it is better to use the shell command 'find' which is much faster.
Syntax of find command is : find directory/ -name target_file_name
Even more, you can chain that command with -exec fonction which will used result of the find to do something: in your case to copy files found in Pictures folder.
In -exec command the {} means the file found. The shell copy command is cp.
This ends with : find /Users/myUserName/ -name 'extended image name.jpg' -exec cp {} \;
(note: the \; tells the system that's the end of the -exec command)
Overall you can run this command in Applescript via do shell script :
set Source to POSIX path of (path to home folder)
set Dest to POSIX path of (path to pictures folder)
set TargetName to "extended image name.jpg"
set BackSlash to ASCII character 92
set SemiCol to ASCII character 59
try
do shell script "find " & Source & " -name " & quoted form of TargetName & " -exec cp {} " & Dest & " " & BackSlash & SemiCol
end try
It is much longer than Finder syntax, but also much faster to run !
Notes:
1) the POSIX path convert the Finder path with : to shell path with /
2) the Backslash and SemiCol are set to \ and ; . this is a work around to avoid that Applescript compiler misinterpret the \ during compilation
3) the do shell script is in try/end try block to avoid error. "Find" gives errors when you try to access to files without permission. Such errors will be ignored.
4) with this method, you can replace Source by "/". Doing so, the find will search in all directories of the main drive (it may take some time !). If you want to search for all users, set Source to "/Users/"

For loop for files in a directory

I am trying to loop through all of the files in a directory and
move them to a workspace (I need to do this to do this because the workspace doesn't have much storage).
Run a program which produces an output directory that contains all the files I will want to work with in the future
Delete the original file from the workspace (to save space in the workspace)and
Move the output directory out of the workspace and back to the storage space
I am able to do this for each file singly (i.e. each line works if I actually use the name of the files), but I can't get the for loop to work. I am quite new to this, so I probably did something simple wrong.
Can anyone see where I am going wrong?
for i in path_to_files; do
#copy to home directory (from scratch)
cp $i .
#Run IDBA
idba_ud -l $i -o '$i'_out
#remove file from work directory (limited space)
rm $i
#copy out directory back to scratch
cp -r '$i'_out path_to_files
done
I keep getting an error that says
syntax error near unexpected token `cp'.
I have also tried replacing cp with copy and i/$i with file/$file with no luck.
If this is indeed POSIX compatible shell (your code looks suspiciously like that, but you haven't specified the actually used shell), then:
You should always quote filenames, in case it contains spaces or other weird characters:
But you should not use single-quotes, as this will prevent shell from expanding your variables.
when appending text to substituted variables, use ${} notation (e.g. if $i expands to "murgel", then ${i}foo will expand to "murgelfoo", whereas $ifoo will expand to "" (an empty string) if there is no variable ifoo)
Thus try:
filepath=/path/to/files
for i in "${filepath}"/*; do
#copy to home directory (from scratch)
cp "${i}" .
#Run IDBA
idba_ud -l "${i}" -o "${i}_out"
#remove file from work directory (limited space)
rm $i
#copy out directory back to scratch
cp "${i}_out"/* "${filepath}"/
done

Resources