File delete using FOR loop in makefile - build

I got struck in writing FOR loop to delete object files in clean target. I tried the below code however I'm getting error message. Could any one please help me on this?
TES_FILE := D:/Technique/Testmake/1.obj
TES_FILE += D:/Technique/Testmake/2.obj
clean:
$(foreach objFile,$(TES_FILE),if exist $(objFile) DEL /F "$(subst /,\,$(objFile))")
error message:
Error Message: if exist D:/Technique/Testmake/1.obj DEL /F "D:\Technique\Testmake\1.obj" if exist D:/Technique/Testmake/2.obj DEL /F "D:\Technique\Testmake\2.obj" Invalid switch - "Technique". gmake: * [clean] Error 1
Thanks,
Anand
I'm reposting this question as I have not received any answers.

Make's foreach function is a textual substitution. It doesn't actually invoke a different shell or command for each "body" of the foreach. So the result of make's foreach is simply the result of appending all those instances of the body together. For example:
$(foreach A,W X Y Z,$(A) $(A))
yields the single string W W X X Y Y Z Z. In your case, it yields a single string containing the concatenation of all the if statements. Which of course won't work.
If you mentioned why you felt you needed a foreach in the first place it would be easier to help. If it were me I'd just write:
clean:
DEL /F $(subst /,\,$(TES_FILE))

Related

re-check in every iteration

I want to loop through files as long as there's files left (Where I edit and remove files inside the loop but glob.glob seems to only initialize at the beginning of the loop and not in every iteration. How can I get this achieved?
Thanks,
Ron
edit 1
I tried:
for MyBuff in glob.glob(DATA_FOLDER+"*.bin"):
tarcmd = TARBIN+" -cjf "+DATA_FOLDER+str(int(time.time()*100))+".tar.bz2 $(find "+DATA_FOLDER+" -name \"*.bin\"| head -500)"
#...tarcmd operations and removal of the 500 inserted bin files
Now, the problem is that I tar up to 500 files into the tarball but what if there's more than 500? I want to loop until no bin files are left...
Assuming that they are text files, Here is a way but I'm not sure if this is the best one.
import os, re
filepath = '/path/to/file/directory/'
result = [f for f in os.listdir(filepath) if re.search(r'[A-Za-z0-9]+\.txt$', f)]
for f in result:
print f

Windows Batch Iterating CSV list and selecting from second list

I have a list of projects and a list of project types. I need to iterate over the list of project types and every time the selected project type equals a certain value I need to get the Ith entry from the project names. For example:
Project Names: Project1,Project2,Project3
Project Types: web,ssis,ssis
I need all the "ssis" projects so the resulting list should be "Project2,Project3".
I've used for /f a lot but I don't know how to get the "i" in the "for" iteration so I can select the related entry from the second list. I've searched on this and gone down into the weeds on "for" but can't seem to come up with an answer.
If there's a method that works better than "for" I'm game. If there's a way to use "for" on the first list and pull the related entries out of the second list that would be great too.
Any help is greatly appreciated.
Based off a now-deleted comment:
#ECHO OFF
SET projlist=Project1,Project2,Project3
SET projtypes=web,ssis,ssrs
SET /a ptr=1
SETLOCAL ENABLEDELAYEDEXPANSION
FOR %%a IN (%projtypes%) DO (
ECHO %%a
SET index=1
FOR %%b IN (%projlist%) DO (
IF !index!==!ptr! ECHO %%b
SET /A index+=1
)
SET /a ptr=ptr+1
ECHO ----------------
)
ENDLOCAL
Running this will give this result:
web
Project1
----------------
ssis
Project2
----------------
ssis
Project3
----------------
Of course, you'll need to modify it to fit your exact needs.
If I understand it correctly, you want to group all project names that belongs to the same project type and show the result as a list of groups. I taken the answer of this post, that do a match of the elements of two lists, and modified it in order to get the groups you want:
#echo off
setlocal EnableDelayedExpansion
set ProjectNames=Project1,Project2,Project3
set ProjectTypes=web,ssis,ssis
rem Group project names by type
set i=0
for %%A in (%ProjectTypes%) do (
set /A i+=1, j=0
for %%B in (%ProjectNames%) do (
set /A j+=1
if !i! equ !j! set NamesByType[%%A]=!NamesByType[%%A]!,%%B
)
)
rem Show result
for /F "tokens=2,3 delims=[]=" %%a in ('set NamesByType[') do (
set list=%%b
echo Type %%a: !list:~1!
)
Output:
Type ssis: Project2,Project3
Type web: Project1
You may also consult a particular element of NamesByType array, for example:
echo %NamesByType[ssis]%

Mallet in R regex error :java.lang.NoSuchMethodException: No suitable method for the given parameters

Ive been following the tutorial on how to use mallet in R to create topic models. My text file has 1 sentence per line. It looks like this and has about 50 sentences.
Thank you again and have a good day :).
This is an apple.
This is awesome!
LOL!
i need 2.
.
.
.
This is my code:
Sys.setenv(NOAWT=TRUE)
#setup the workspace
# Set working directory
dir<-"/Users/jxn"
Dir <- "~/Desktop/Chat/malletR/text" # adjust to suit
require(mallet)
documents1 <- mallet.read.dir(Dir)
View(documents1)
stoplist1<-mallet.read.dir("~/Desktop/Chat/malletR/stoplists")
View(stoplist1)
**mallet.instances <- mallet.import(documents1$id, documents1$text, "~/Desktop/Chat/malletR/stoplists/en.txt", token.regexp ="\\p{L}[\\p{L}\\p{P}]+\\p{L}")**
Everything works except for the last line of the code
**`**mallet.instances <- mallet.import(documents1$id, documents1$text, "~/Desktop/Chat/malletR/stoplists/en.txt", token.regexp ="\\p{L}[\\p{L}\\p{P}]+\\p{L}")**`**
I keep getting this error :
Error in .jcall("RJavaTools", "Ljava/lang/Object;", "invokeMethod", cl, :
java.lang.NoSuchMethodException: No suitable method for the given parameters
According to the package, this is how the function should be:
mallet.instances <- mallet.import(documents$id, documents$text, "en.txt",
token.regexp = "\\p{L}[\\p{L}\\p{P}]+\\p{L}")
I believe it has something to do with the token.regexp argument as
documents1 <- mallet.read.dir(Dir) works just fine which means that the first 3 arguments supplied to mallet.instances was correct.
This is a link to the git repo that i was following the tutorial from.
https://github.com/shawngraham/R/blob/master/topicmodel.R
Any help would be much appreciated.
Thanks,
J
I suspect the problem is with your text file. I have encountered the same error and resolved it by using the as.character() function as follows:
mallet.instances <- mallet.import(as.character(documents$id),
as.character(documents$text),
"en.txt",
FALSE,
token.regexp="\\p{L}[\\p{L}\\p{P}]+\\p{L}")
Are you sure you converted the id field also to character ? It is easy to overlook the advice and leave it as an integer.
Also there is a typo in the code sample: the backslashes have to be escaped:
token.regexp = "\\p{L}[\\p{L}\\p{P}]+\\p{L}"
This usually occurs because the html text editor eats up one backslash.

Batch File If Error

Ok so I am working on a project and I am doing this and I get to the if statement in confirmOne and it gives me "( was not expected at this time." Please help!
Many of the stray "You got to here!" messages are from me trying to debug it. I really need this soon. Please help. I also tried deleting parts and it still doesn't seem to work. If you see any other errors please tell me as I need all the help I can get. Thank you!
:grabInput
echo Please enter the username of the user you wish to access.
REM - } End Echoing Information/Main Menu | Grab Input {
set /p result=
goto correctName
REM - } End Grab Input | Process Input {
:correctName
set /p input=%result%
goto confirmOne
:confirmOne
echo Got to confirmOne
pause
if %input%==[] (
pause
cls
echo Oops! Looks like you didn't enter anything! Try Agian!
echo.
echo ................................................................................
echo.
goto grabInput
) ELSE (
goto confirmTwo
)
:confirmTwo
echo Got to ConfirmTwo
pause
if %input%==~help (
goto helpMenu
) ELSE (
goto confirmThree
)
:confirmThree
echo Got to ConfirmThree
if %input%==~info (
goto infoMenu
) ELSE (
goto swapDrive
)
Well, if you didn't enter anything for %input%, then your if statement would look like if ==[] (.
Your if statement should look like if [%input%] == [] (
I also see a lot of unnecessary code, you should take a look over your script.
Batch ALWAYS works on strings.
with the statement if %input%==[], when %input% is set to [nothing] (which is what you're trying the detect), batch substitutes [nothing] fo %input% and gets
IF ==[] (
and is confused because '(' is not a comparison operator.
[] is not some magic mantra. It's an old method of detecting the presence of parameters such that [%1] would equal [] if the parameter was absent. It doesn't work when the variable contains spaces or some other characters.
if "%var%"=="" is better
if not defined var is better still
Note that
set /p var=
will NOT set var to [nothing] in you simply press enter, It will leave var unchanged.
Hence this
set var=something
set /p var=
will leave var set to something. You should code this as
set "var="
set /p var="Some prompt "
if not defined var echo VAR is not defined
The quotes around var= ensures that var is NOT set to [some spaces] if there are trailing spaces on the line.
Other than that, the sequence
goto somelabel
:somelabel
(REM lines are irrelevant) is superfluous.
equally, in
if somecondition (goto somewhere) else (goto somewhereelse)
:somewhereelse
the else condition is superfluous
Batch only notices :label as a DESTINATION for a GOTO or a CALL. and will otherwise simply charge straight through any :label it finds as though they were remarks statements.

Makefile - loop through a list and select a specific value

I'm trying to do something like this (assuming $input is something provided by the user):
LIST = pre1 pre2 pre3 pre4 pre5 pre6 pre7 pre8 pre9 pre10
START = 0
for prefix in $(LIST); do \
if $(input) == $(prefix) then
START = 1
endif \
if $(START) == 1 then \
if [ -f $(prefix)<file_name> ]; then <do_A>; else <do_B>; fi
endif \
done
my problem is with the two if's mentioned above. i don't know how can i choose a specific string value from a list while iterating it (if $(input) == $(prefix) then) and i don't know how to check if a value is 1 or 0 (if $(START) == 1 then).
My intent with this code is to use the same makefile for different directories which have the same file name, but with a different prefix. sometimes, a directory might contain multiple versions of the file with a different prefix and i want to define a hierarchy of those prefixes (defined by LIST in my example). when the user provide a version, the idea is to start searching for the most up-to date version, starting from the version he provides (e.g. if the user provide pre4, then i need to search pre4 first and if it's not exist - i'll go on and search for pre5 and so on. but in this example, i won't search for pre1 even if it do exist in the current directory).
Anyone has an idea on how can i do that?
Thanks in advance.
If that is supposed to be a command in a Makefile, the syntax would have to be something like this:
LIST = pre1 pre2 pre3 pre4 pre5 pre6 pre7 pre8 pre9 pre10
START = 0
input = somename
file_name = whatever
some_target:
for prefix in $(LIST); do \
if test "$(input)" = $$prefix; then \
START=1; \
fi; \
if test "$(START)" = 1; then \
if test -f $$prefix$(file_name); then \
<do_A>; \
else \
<do_B>; \
fi; \
fi; \
done
But you didn't tell us what <input> and <file_name> are supposed to be, so I assumed they are other make variables. Basically the make rules look like one long shell line, with commands separated by semicolons, and lines continued with backslashes. $$ is replaced by make with a single $, which is why references to shell variables ($$prefix) need two dollars.
Your make manual (type man make has the whole story and is fun to read and understand.) Become a make guru today! Be sure to understand the difference between a make variable and a shell variable.