Using locals to construct a path in Stata - stata

Suppose I have a file called hello.txt in a directory c:\test\, then the following runs without problems in Stata:
local folder "c:\test"
confirm file "`folder'\hello.txt"
but the following won't
local folder "c:\test"
local file "hello.txt"
confirm file "`folder'\`file'"
How do I combine more than one local into a path like c:\test\hello.txt that can be used in e.g. the confirm file command?
Thanks in advance.

The problem is that \ can act both as escape character and as part of a Windows file path.
When you type something in Stata that contains a local macro, then Stata's first action is to evaluate that macro, i.e. look at its content and replace the macro with that content. Sometimes (rarely, but it can happen) you want to prevent that. That is what an escape character is for.
So what Stata sees in your second example is
c:\test`file'
which is not a valid path.
The easiest solution is to use a / instead of a \, which results in a valid path as far as Stata is concerned and that won't act as an escape character.
For more see:
Nicholas J. Cox (2008) Stata tip 65: Beware the backstabbing backslash. The Stata Journal, 8(3): 446--447.
http://www.stata-journal.com/article.html?article=pr0042

Related

Batch rename files in Adobe Bridge

I want to Batch Rename a few thousand files. I have one part of the regular expression figured out just not the first part.
The filename is the employeeID_employeeName-sequence number. I would like to only keep the employeeID_employee name.
For example the file 123456_John Smith-00001.pdf should become 123456_John Smith.pdf
I was able to use Batch Rename, then string substitution then, find [-\d?] but that only changes the filename to _John Smith.pdf
Please see batch rename example image
The expression that worked was by Wiktor Stribiżew in the comment above. In the string substitution field I placed -\d+(.\w+)$ and left the replace with field empty.

Iterating over directory with specified path in Bash

pathToBins=$1
bins="${pathToBins}contigs.fa.metabat-bins-*"
for fileName in $bins
do
echo $fileName
done
My goal is to attach a path to my file name. I can iterate over a folder and get the file name when I don't attach the path. My challenge is when I add the path echo fileName my regular expression no longer works and I get "/home/erikrasmussen/Desktop/Script/realLargeMetaBatBinscontigs.fa.metabat-bins-*" where the regular expression '*' is treated like a string. How can I get the path and also the full file name while iterating over a folder of files?
Although I don't really know how your files are arranged on your hard drive, a casual glance at "/home/erikrasmussen/Desktop/Script/realLargeMetaBatBinscontigs.fa.metabat-bins-*" suggests that it is missing a / before contigs. If that is the case, then you should change your definition of bins to:
bins="${pathToBins}/contigs.fa.metabat-bins-*"
However, it is much more robust to use bash arrays instead of relying on filenames to not include whitespace and metacharacters. So I would suggest:
bins=(${pathToBins}/contigs.fa.metabat-bins-*)
for fileName in "${bins[#]}"
do
echo "$fileName"
done
Bash normally does not expand a pattern which doesn't match any file, so in that case you will see the original pattern. If you use the array formulation above, you could set the bash option nullglob, which will cause the unmatched pattern to vanish instead, leaving an empty array.

removing m_ prefix from variable names

Initially we planned to have old convention of having m_ prefix for class variables. But now requirement has come to replace all the m_VaribaleName to this.variableName, i.e. remove the m_ and make the first character after m_ lowercase.
I can search and replace m_ with this. but this doesn't rename the variable's first character to lowercase. After search and replace if I use re-factoring tool to rename VariableName to variableName this also renames the property already exists with VariableName.
I am wondering is there any regex, tool, macro to make this task automated.
Resharper will highlight all such errors in a solution, and fix them individually, but I don't think it can fix all of them with a single command. Still, it's easy enough to navigate between errors that it finds.
You can do this in emacs.
Open your file (C-x C-f) and do M-x replace-regexp.
Let's say the name of the variable is Variable.
Your regexp query would replace \(V\)ariable for \,(downcase \1)ariable.
The \, tells emacs the following statement is a lisp expression.
Additionally, if you wanted to replace the m_ at the same time you could do replace m_\(V\)ariable for \,(downcase \1)ariable.
This will take care of all instances in a file at the same time and emacs does remember your last replace-regexp query so you do not have to retype it for multiple files. Furthermore, there is a way using DIRED mode to do the replace-regexp on multiple files at the same time.
Open up a directory on DIRED mode (C-x C-f DIR_NAME), mark the files you want by going over them (you can navigate using n and p) by pressing m. Once the files you want to process are marked press Q (capital-case). You will get the same regexp prompt as if you did a single file, enter the regexp, RET, the replacement, and RET. It will open up each file at a time, press ! for every file to replace all reg-exps in that file.
After that, you still have to save the files. Which you can do with C-s and then !.

Filename extraction with regex

I need to be able to only extract the filename (info.txt) from a line like:
07/01/2010 07:25p 953 info.txt
I've tried using this: /d+\s+\d+\s+\d+\s+(?.?)/, but it doesn't seem to work ...
How about
/\S+$/
I.e. the longest possible string of non-whitespace at the end of the line.
(Hard to know for sure without more info about the possible inputs.)
As #J V pointed out, filenames with spaces in them (like his username) will not be parsed properly by the above regexp. We don't know from the question whether that's possible.
But I have a suspicion that we're looking at the output of Windows DIR command, or something very similar. In that case, the most reliable approach might be just to hack off the first 39 characters and keep the rest:
/^.{39}(.+)$/
Then $1 will contain the filename.
Better option:
But if you are using Windows DIR (as per your new comment), and you can control the DIR command, try
DIR /b
which removes the unneeded cruft (assuming you don't need the date, size etc.) and gives you one filename per line.
OK, you're using a Unix dir (per newer comment). The CentOS dir I have outputs one file per line, nothing else, when you give it no command line options. Chances are very good that whichever dir you're using can be persuaded to output filenames like that... then you wouldn't have to worry about using a regex that may or may not be correct for every possible input. Try man dir or dir --help to find out what command-line options to use.
\d\d:\d\d\w\s+\d+\s+(.*?)$
$1 will be the file name
The problem with your original regex is that it forgets the special characters :, /, and (?.?) means nothing...
Assuming that the files have extension as .txt you can try.
(?<=(\s)*)\w*.txt
Why not just use the following regex:
\w+\.\w+

Opening a file on unix using c++

I am trying to open a file in c++ and the server the progam in running on is based on tux.
string filename = "../dir/input.txt"; works but
string filename = "~jal/dir1/dir/input.txt"; fails
Is there any way to open a file in c++ when the filename provided is in the second format?
The ~jal expansion is performed by the shell (bash/csh/whatever), not by the system itself, so your program is trying to look into the folder named ~jal/, not /home/jal/.
I'm not a C coder, but getpwent() may be what you need.
You could scan the string, replacing ~user by the appropriate directory.
The POSIX function wordexp does that, and a few other things
variable substitution, like you can use $HOME
optional command substitution, like $(echo foo) (can be disabled)
arithmetic expansion, like $((3+4))
word splitting, like splitting ~/a ~/b into two words
wildcard expansion, like *.cpp
and quoting, like "~/a ~/b" remains that
Here is a ready piece of code, that performs this task:
How do I expand `~' in a filename like the shell does?