这个问题被标记为ASP经典,但算法解决方案还可以。
我有以下一组顺序编号的文件:
1.jpg,2.jpg,3.jpg,4.jpg ...... X.jpg
我需要一个函数,它将输入两个文件名,fromFile和toFile参数,并且需要重命名所有需要的文件,使得from文件在toFile之前的序列中移动,文件介于两者之间重新编号。
示例:
将1.jpg移动到4.jpg应该执行以下操作:
将4.jpg移动到2.jpg应该执行以下操作:
作为输入,我有一个包含文件名和两个文件名的字符串数组。
您能告诉我文件重命名的最佳方法是什么?
答案 0 :(得分:1)
这是一个简短的方法,考虑到你的所有文件都将被命名为numeric.jpg,你将不得不建立自己的函数:
FileExists(Filename)
RenameFile(OriginalFilename,NewFilename)
<%
Input1 = Request.Form("file1")
Input2 = Request.Form("file2")
'gets digits only
Input1Digit = Left(Input1,Instr(Input1,"."))
Input2Digit = Left(Input2,Instr(Input2,"."))
'is file1 less than file2?
If Input1Digit < Input2Digit Then
'loop through the digits frontwards 1 to 5
For x = Input1Digit to Input2Digit
'if the first loop?
If cStr(x) = cStr(Input1Digit) Then
'see if file exists here
If FileExists(Input1) Then
FileRename(Input1, Input1 & ".temp") 'Rename the file here [From, To]
OriginalFileExists = True
Else
FileRename(Input1, Input1Digit & ".jpg"
OriginalFileExists = False
End If
'if not on the first loop?
Else
'did the original file exist '.temp'
If OriginalFileExists Then
NewFileName = cInt(x) - 1
Else
NewFileName = cInt(x)
End If
'rename each file here
RenameFile(x & ".jpg", NewFileName & ".jpg")
End If
Next
Else
'loop through the digits more to less 5 to 1
For x = Input1Digit to Input2Digit STEP -1
'if the first loop?
If cStr(x) = cStr(Input1Digit) Then
'see if file exists here
If FileExists(Input1) Then
FileRename(Input1, Input1 & ".temp") 'Rename the file here [From, To]
OriginalFileExists = True
Else
FileRename(Input1, Input1Digit & ".jpg"
OriginalFileExists = False
End If
'if not on the first loop?
Else
'did the original file exist '.temp'
If OriginalFileExists Then
NewFileName = cInt(x) + 1
Else
NewFileName = cInt(x)
End If
'rename each file here
RenameFile(x & ".jpg", NewFileName & ".jpg")
End If
Next
End If
%>