I am trying to create a batch script in R to pre-process some data and one of the first steps I have to do is check to see if a file exists in a sub-directory and then (if it does) create a copy of it with a new name. I'm having trouble with the syntax.
我试图在R中创建一个批处理脚本来预处理一些数据,我要做的第一步是检查子目录中是否存在文件,然后(如果存在)创建副本它有一个新的名字。我的语法有问题。
This is my code:
这是我的代码:
##Define the subject directory path
sDIR = "/home/bsussman/Desktop/WORKSPACE"
#create data frame to loop through
##list of subject directories
subjects <-list.dirs(path = sDIR, full.names = TRUE, recursive = FALSE)
for (subj in 1:length(subjects)){
oldT1[[subj]] <- dir(subjects[subj], pattern=glob2rx("s*.nii"), full.names=TRUE)
T1[[subj]] <- paste(subjects[subj], pattern="/T1.nii",sep="")
if (file.exists(paste(subjects[subj], pattern="/T1.nii",sep=""))=FALSE{
file.copy(oldT1, T1)
}
}
It renames files in one subdirectory, but will not do loop through gives me these errors:
它重命名一个子目录中的文件,但不会循环通过给我这些错误:
Error: unexpected '=' in:
"
if (file.exists(paste(subjects[subj], pattern="/T1.nii",sep=""))="
> file.copy(oldT1, T1)
[1] FALSE
> }
Error: unexpected '}' in " }"
> }
Error: unexpected '}' in "}"
I am not as much worried about the [1]FALSE message. But any ideas?
我对[1] FALSE消息并不那么担心。但任何想法?
Thanks!!
谢谢!!
1 个解决方案
#1
1
It's just a problem with the syntax in the if
statement. Try replacing this:
这只是if语句中语法的问题。尝试替换这个:
if (file.exists(paste(subjects[subj], pattern="/T1.nii",sep=""))=FALSE{
file.copy(oldT1, T1)
}
with this:
有了这个:
if (!file.exists(paste(subjects[subj], pattern="/T1.nii",sep=""))){
file.copy(oldT1, T1)
}
#1
1
It's just a problem with the syntax in the if
statement. Try replacing this:
这只是if语句中语法的问题。尝试替换这个:
if (file.exists(paste(subjects[subj], pattern="/T1.nii",sep=""))=FALSE{
file.copy(oldT1, T1)
}
with this:
有了这个:
if (!file.exists(paste(subjects[subj], pattern="/T1.nii",sep=""))){
file.copy(oldT1, T1)
}