• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 295
  • Last Modified:

proble renaming multiple files using asp

I am able to rename (or move) a file in windows machine, from  abc_1_file1.txt to abc_2_file1.txt using asp.

Below is my code

<%
dim fs,f
infromfile = request.querystring("in_from_file")
intofile = request.querystring("in_to_file")

IF infromfile <> "" AND intofile <> "" then
    set fs=Server.CreateObject("Scripting.FileSystemObject")
    Set f=fs.GetFile(infromfile)
    if fs.FileExists(intofile) then
       fs.DeleteFile(intofile)
    end if
    f.Move(intofile)
    response.write " The files, from_file and to_file is moved " & infromfile & " -- to -- " & intofile
    set f=nothing
    set fs=nothing
ELSE
    response.write " The files, from_file and to_file are required" & "<br>"
End If
%>
-------------------
But now I have a need to rename (or move) a bunch of files with a particular name pattern like
abc_1_file1.txt
abc_1_file2.txt
...

to

abc_2_file1.txt
abc_2_file2.txt

How can do this using the above code?
0
aarch1
Asked:
aarch1
  • 2
  • 2
1 Solution
 
aarch1Author Commented:
I made it to work (see code below).
But it goes through all the files in that folder to locate my specific pattern on each execution.

Is there a way I can only loop through my patter files like abc_*.*



----------
<%
dim fs,f
infromfile = request.querystring("in_from_file")
intofile = request.querystring("in_to_file")
inretpath = request.querystring("in_ret_path")


IF infromfile <> "" AND intofile <> "" then
    response.write " The parameters " & infromfile & "-- " & intofile & "-- " & inretpath

      DIM SOURCE_PATH
      Dim fso, objSourceFolder, objFile, tmpFile
      SOURCE_PATH = inretpath
      ' Create Objects
      Set fso = CreateObject("Scripting.FileSystemObject")

      ' Get the source folder
      Set objSourceFolder = fso.GetFolder( SOURCE_PATH )

      ' Process each File in the Folder
      For Each objFile In objSourceFolder.Files
       if InStr(objFile.Name, "abc_") > 0 then
        tmpFile=Replace(objFile.Name,infromfile,intofile)
response.write "-------------- The files are " & objFile.Name & "===" &tmpFile & "++" & "<br>"

        objFile.Move( inretpath&tmpFile )

       end if
      Next

    response.write " The files, from_file and to_file is moved " & infromfile & " -- to -- " & intofile
ELSE
    response.write " The files, from_file and to_file are required" & "<br>"
End If
%>
0
 
L00MCommented:
So basically, you are just wanting to increment that integer by one before moving it if I understand correctly. If you know the filename will always come in with that format,you can change:

...
For Each objFile In objSourceFolder.Files
  if InStr(objFile.Name, "abc_") > 0 then
    tmpFile=Replace(objFile.Name,infromfile,intofile)
...

To:

...
For Each objFile In objSourceFolder.Files
  if InStr(objFile.Name, "abc_") > 0 then
    tmpFile=Replace(objFile.Name,infromfile,intofile)
    arrFileName = Split(tmpFile, "_")
    tmpFile = arrFileName(0) & "_" & Int(arrFileName(1)+1) & "_" & arrFileName(2)
...

I think that should do what you need. Let me know if not.
0
 
aarch1Author Commented:
The question is
The below statement goes through all the files in that DIRECTORY which  could be time consuming.
    For Each objFile In objSourceFolder.Files
-----------
Instead is there a statement which can only go throught files with a CERTAIN name pattern.
(for example, in this case all files starting with "abc_")

0
 
L00MCommented:
FSO does not have a native method to filter. Therefore, you must continue to use if
'InStr(objFile.Name, "abc_") > 0 then'
and enumerate every file within a given directory. If you switch to .NET, you would have the ability to use:
'System.IO.Directory.GetFiles(<path>, <pattern>)'. But unfortunately, that's just not available for FSO.
0
 
Computer101Commented:
Forced accept.

Computer101
EE Admin
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now