Nov 21, 2009
Pages: 1, 2

Copy A File And Rename It With Increment If Exist - in VBScript

free web hosting
Open Discussion > MODERATED AREA > Computers > Programming Languages > Others

Copy A File And Rename It With Increment If Exist - in VBScript

oxida
Hi all.

I want to make a VBScript wich does the following:

Copy a file like test.txt
if test.txt already exists then name it test1.txt and so on until i have xxx number of files.

If someone could help me or post a script I would be happy xD

Thanks,

Comment/Reply (w/o sign-up)

pasten
If you mind some software that would do the work for you, then I would recommend Renamer. I have used this utility in many similar situtations as yours. And this software is very useful to rename files in large quantities. Also it offers a variety of features like regular expressions and so on. Just check it to be sure if it works for you. Believe me, I myself didn't get the time to completely explore its features.

Comment/Reply (w/o sign-up)

oxida
QUOTE(pasten @ Sep 8 2008, 02:43 PM) *
If you mind some software that would do the work for you, then I would recommend Renamer. I have used this utility in many similar situtations as yours. And this software is very useful to rename files in large quantities. Also it offers a variety of features like regular expressions and so on. Just check it to be sure if it works for you. Believe me, I myself didn't get the time to completely explore its features.


Thanks for your reply, but its not what i am looking for. I just want a vbscript wich does only kopie a specified file xx times and name it to:
[any name]1, [any name]2, [any name]3 etc etc.

I already know how to copy a file one time but thats it.

Comment/Reply (w/o sign-up)

travstatesmen
So, let me see if I have this straight. You want to copy a file from one pathname, and then find out if it exists in any form in a second pathname, and then paste it to the second path, with an increment on the resulting filename, am I right?

An illustration of the concept would be for a backup of your mySQL database, for instance. Lets say you have a CRON job that will create an automated backup of your mySQL database, that puts the resulting database in the same place all the time, overwriting the previous backup with the same name, to save on storage space. But you want to keep multiple versions of the backups on your local computer, so you want a script that will copy the source file from the original backup on the server at, say, ftp://ftp.mysite.trap17.com/backups/latestbackup.zip and then put it on your local computer, say, at c:\my documents\backups\*.zip but name it incrementally one higher than the previous existing backup. Does that sound like the type of thing that you are after?

Let me see if I can put it into a logical flow for you. The code below is not written in any particular programming language and will not compile in its current form, this is just for the sake of clarification of the logic flow....

CODE
'**** Declare Variables ****
DIM sourcepath$ AS String                                            'The path to the source file, includes backslash
DIM sourcefile$ AS String                                            'The name of the source file without a loop number
DIM destpath$ AS String                                              'The path to the destination file, includes backslash
DIM max AS Integer                                                   'The maximum number of copies to keep
DIM a AS Integer                                                     'A loop counter

'**** Main Loop ****
FOR a = max-1 TO 1                                                   'Create a decreasing loop counter
  IF EXIST destpath$ & sourcefile$ & a THEN                          'If the filename with current counter's value exists.....
    COPY (sourcepath$ & sourcefile$, destpath$ & sourcefile$ & a+1)  'Copy the file adding the loop counter + 1
    END NEXT                                                         'Break out of the FOR-NEXT loop now
  END IF
NEXT a                                                               'Otherwise, decrement the loop counter by one and try again.

Sure, there will need to be much more to it in a final script, such as checking if the maximum has already been reached, and what to do from there, and also error handling for Input/Output errors, etc, but I think that is basically what you are trying to achieve, is it?. Not forgetting that I have omitted any reference to the file extension (*.zip, *.txt, etc). I guess you would want to pass the variables listed above (except for the loop counter) when you call the script, so that it can be used for a variety of purposes, rather than just for one specific project that you have in mind.

I'm not a real programmer, but I can understand logic reasonably well. I'm sure that others here can improve on the start that I have made, and maybe put it into a real programming language for you. Hope it helps somehow.

 

 

 


Comment/Reply (w/o sign-up)

oxida
Travstatesmen, that's exactly what I want to do.
Thank you for the info you gave me, the language i wan't it to be written in is: VBscript.

I have some really basic knowlage about VBscript and I can simply copy files/folders to the place I want and rename it.

But that's where I got stuck.

So if someone could help me then I would be so happy.
When I'm home I will post the VBscript I have for copy and rename.

Thanks again.

Comment/Reply (w/o sign-up)

oxida
Well I found something simmiliar.
It makes a file with a temp name then it changes the .temp extension to .txt.
This will be repeated 100 times.

CODE

Set objFSO = CreateObject("Makefile.FileSystemObject")

For i = 1 to 100 ' For Next loop that runs from 1 to 100

strFileName = objFSO.GetTempName 'generates random files names

strFileName = Replace(strFileName, ".tmp", ".txt") 'changes .temp to .text

strPath = "c:\script\temp\" & strFileName 'path to the location where the files needed to be placed.
'if this script is placed in a folder like: c:\script
'and your strPath is c:\script\temp it
'wil not work it needs a "\" at the end.

objFSO.CreateTextFile(strPath) 'creates the file
Next

Comment/Reply (w/o sign-up)

sonice
Hi in my situation I have for example 32132131346879.zip the file generated in C:/temp by my PC and I need to find the way for this file to be renamed to text.zip and this 32132131346879 randomly changed each time...

How can I do it with VBS script?

Thanks.

Comment/Reply (w/o sign-up)

(G)Tom McElvy
Similar need
Copy A File And Rename It With Increment If Exist

I have a similar, but simpler need, and please understand I am NOT a programmer :)  !  All I need to be able to do is to RANDOMLY copy a file from a source directory to a destination directory.  Trick is, it must be random!  The command line would be:

RANDOM.EXE <SOURCE PATH> <DESTINATION PATH>

I had something someone had for the BBSs years ago, but I'll be damned if I can find it!  Anyone able to help??  Many thanks!

-reply by Tom McElvy

Comment/Reply (w/o sign-up)

PatrickMc
You want to randomly copy a file from <SOURCE PATH> to <DESTINATION PATH>.

Here is a script.

CODE
# Script randomcopy.txt
# Input argument source and dest paths.
var str source_path, dest_path

# Collect a list of files in source path.
var str list; lf -n "*" $source_path > $list
# Get a count of files in source path.
var int count; set $count = { len $list }
# Get number of seconds in current time.

var str seconds; chex "12[" gettime() > $seconds
# Generate a random number n between 1 and $count
var real n
set $n = ( makereal(str($seconds)) / 60 * $count ) + 1
# Get the $n'th file from our file list.
var str file; lex makestr(real($n)) $list > $file

# Copy $file to dest_path. Enclose both in double quotes as
# they may contain spaces, etc.
system copy ("\""+$file+"\"") ("\""+$dest_path+"\"")



The script is in biterscripting ( http://www.biterscripting.com ) . Save the script as C:\Scripts\randomcopy.txt. Start biterscripting. Enter the following command. (Enter the whole command on just one line. The source_path and dest_path are arguments passed to the randomcopy.txt script.)

CODE
script randomcopy.txt source_path("<SOURCE PATH>") dest_path("<DESTINATION_PATH>")


Change the <SOURCE PATH> and <DESTINATION PATH> to appropriate paths and make sure to enclose the paths in double quotes. The script can also be called directly from MS DOS, batch, perl, php, vbs or task scheduler. See the help page on batch.

Patrick

Comment/Reply (w/o sign-up)

(G)Jayarama
how to check a file is exist or not
Copy A File And Rename It With Increment If Exist

Hi All

I have a doubt, I want to check a file which is in Web. I want to check the file is exist or not. I have written a code which is giving me "False" . Where ifTyped the same URl in the browser I can see the File. I have given the sample code here please any body knows the solution please tell me.

Set fso = CreateObject("Scripting.FileSystemObject")

Msgbox  fso.FileExists("http://Bangmsrira1 :8080/SampleProject/Book1.Xls")

It is showing false. 

Thanks

-question by Jayarama


Comment/Reply (w/o sign-up)



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)

Pages: 1, 2
Similar Topics

Keywords : copy, file, rename, increment, exist, vbscript

  1. Search For Video Files And Display Them
    Using a batch file in windows (0)
  2. Timer Command In Dos
    batch file help (6)
    I wish to execute a command at regular intervals .....precisely every half an hour....Is there any
    Dos command which can do it I havent found any so far...and i've heard there are no timer
    commands in DOS. So how should i go about it...i want the batch file to be executed at regular
    intervals....any external process or application that will invoke it?????....
  3. Open Any Unknown File Type With Notepad
    Make Notepad as default file opener (7)
    Open Any Unknown File with Notepad
    ************************************************************************ DISCLAIMER: The
    following contains registry edits. If done improperly can cause strange behaviour, and at worst,
    could even entirely corrupt your Windows Installation, requiring you to reinstall Windows. I cannot
    be held for any damage you may cause to your system as a result of taking any action suggested in
    the following article. ************************************************************************
    Target OS: Windows XP Introduction....
  4. Autoplay.inf And .bat File Question
    can you check my code and information? (2)
    Okay, so here's the situation. For one of my school projects I have decided to do a simple
    cartoon animation. Because of the colour loss that you get when saving in .gif format, I have
    decided to save it as a .wmv file. However our school does not have a media player installed so I
    have decided to hand the project file (.mswmm) in instead and provide instructions on how to play
    the movie from there. Therefore when the cd is inserted I need two files to open (movie.mswmm AND
    instructions.txt). I have googled about autoplay and I have found out that you can only get ....

    1. Looking for copy, file, rename, increment, exist, vbscript

Searching Video's for copy, file, rename, increment, exist, vbscript
See Also,
advertisement


Copy A File And Rename It With Increment If Exist - in VBScript

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com