尝试使用excel重命名PDF以供R中的引用时不匹配
我需要通过引用一个15000行的excel工作表来重命名15000个PDF,该工作表在两个相应的列中包含旧名称和必需的新名称 我的excel工作表如下所示: 我将文件保存在一个名为-SDP Response的文件夹中。然后运行以下脚本:尝试使用excel重命名PDF以供R中的引用时不匹配,r,paste,R,Paste,我需要通过引用一个15000行的excel工作表来重命名15000个PDF,该工作表在两个相应的列中包含旧名称和必需的新名称 我的excel工作表如下所示: 我将文件保存在一个名为-SDP Response的文件夹中。然后运行以下脚本: #Use Readxl and dplyr package library(readxl) library(dplyr) #Set Working Directory as Desktop setwd("/Users/shwetachopra/Desktop
#Use Readxl and dplyr package
library(readxl)
library(dplyr)
#Set Working Directory as Desktop
setwd("/Users/shwetachopra/Desktop")
#Set path to folder that contains our PDFs as Folder
folder <- "/Users/shwetachopra/Desktop/SDP Responses"
#Create vector of list of PDF names, from folder
oldnames <- as.vector(list.files(folder))
#Export excel of matching newnames in same order as above
sdpdata <- read_excel("NewSDPNames.xlsx")
#Change working directory to folder containing the pdfs
setwd("/Users/shwetachopra/Desktop/SDP Responses")
#Separately extract the column which contain the matching old names
oldies <- sdpdata[[2]]
#filter rows that match old file names
subset <- sdpdata[grep(paste(oldnames, collapse="|"),oldies),]
#extract new file names
new_names <- subset[[3]]
#rename old file names with new ones
file.rename(oldnames,new_names)
#使用Readxl和dplyr包
图书馆(readxl)
图书馆(dplyr)
#将工作目录设置为桌面
setwd(“/Users/shwetachopra/Desktop”)
#将包含PDF的文件夹的路径设置为文件夹
文件夹尝试以下操作:
library(readxl)
#Set Working Directory as Desktop
setwd("/Users/shwetachopra/Desktop/SDP Responses")
#Read the list of PDFs
oldnames = list.files(".", pattern = ".pdf$")
# Read the excel with 'Old PDF Names' and 'New Names' columns
sdpdata <- read_excel("NewSDPNames.xlsx")
# Only select from sdpdata those files that are in oldnames
subset = sdpdata $`Old PDF Names` %in% oldnames
# Rename
file.rename(from = sdpdata $`Old PDF Names`[subset],
to = sdpdata $`New Names`[subset])
比较旧名称
与新名称
的长度。如果它们不匹配,则excel中可能没有文件夹中一个或多个文件的条目
file.rename(oldnames,new_names)