[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

proble renaming multiple files using asp

Posted on 2007-07-26
6
Medium Priority
?
278 Views
Last Modified: 2010-04-16
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
Comment
Question by:aarch1
  • 2
  • 2
5 Comments
 

Author Comment

by:aarch1
ID: 19576074
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
 
LVL 11

Expert Comment

by:L00M
ID: 19579053
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
 

Author Comment

by:aarch1
ID: 19609081
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
 
LVL 11

Accepted Solution

by:
L00M earned 2000 total points
ID: 19609684
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
 
LVL 1

Expert Comment

by:Computer101
ID: 20209487
Forced accept.

Computer101
EE Admin
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Simple Linear Regression
Progress

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question