티스토리 뷰

차기작 : R을 배우자

Naming files by date

quantlab 2015. 11. 11. 16:36

최근에 화일 이름을 날짜순(화일을 변경한 날짜 순)으로 바꿀 필요가 있었다. 예를 들면 Hello.txt와 Goodbye.txt가 변경 날짜가 1월 1일, 12월 31일이라면 01_Hello.txt와 02_Goodbye.txt로 바꾸는 것이다. WD는 폴더의 이름, ext는 확장자의 이름이며, 번호의 자리수(01 또는 001)는 22번줄의 # space required for numbering에 3을 원하는 숫자로 바꿔서 변경할 수 있다.


Recently I came across the situation where I had to change the filename by date(modification date). For example if I modified the file "Hello.txt" in 1st Jan. and I modified "Goodbye.txt" in 31st Dec, I have to change them to "01_Hello.txt" and "02_Goodbye.txt". WD is the folder name, ext is the extension, and on 22nd line with "# space required for numbering", you can finde "%03d", and if you modify 3 to 2, the space for numbering changes to 2 like "02".


# read in directories and name them according to the dates

# dir()
# file.rename(from, to)

WD = "D:/MP4s/"; # Directory or folder name
ext = ".mp4"


filenames <- dir(WD)

l.filenames <- nchar(filenames)

ext.filenames <- substring(filenames, l.filenames-3, l.filenames)

file.sel <- filenames[ext.filenames == ext]

mt.sel <- file.mtime(paste(WD,file.sel,sep=""))


######### TARGET MARKED
mt.sel.ordered <- file.sel[order(mt.sel)]

num <- 1:length(mt.sel.ordered)

numbering <- sprintf("%03d",num) # space required for numbering

file.rename(paste(WD,mt.sel.ordered,sep=""), paste(WD,numbering,"_",mt.sel.ordered,sep=""))



'차기작 : R을 배우자' 카테고리의 다른 글

fitting RSM  (0) 2016.01.30
I have a table!  (0) 2015.11.11
Dealing with multidimensional array  (0) 2015.11.10
8 dimensions  (0) 2015.11.02
another log-sum-exp blogging : implementation  (0) 2015.10.23
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함