Jump to content
xisto Community
oxida

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

Recommended Posts

Hi all.I want to make a VBScript wich does the following:Copy a file like test.txtif 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 xDThanks,

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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, http://forums.xisto.com/no_longer_exists/ 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....

'**** Declare Variables ****DIM sourcepath$ AS String											'The path to the source file, includes backslashDIM sourcefile$ AS String											'The name of the source file without a loop numberDIM destpath$ AS String											  'The path to the destination file, includes backslashDIM max AS Integer												   'The maximum number of copies to keepDIM 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 IFNEXT 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.
Edited by travstatesmen (see edit history)

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

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 fileNext

Edited by oxida (see edit history)

Share this post


Link to post
Share on other sites

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.

Edited by sonice (see edit history)

Share this post


Link to post
Share on other sites
Similar needCopy 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

Share this post


Link to post
Share on other sites

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

Here is a script.

# 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 $countvar real nset $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.)

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
Edited by PatrickMc (see edit history)

Share this post


Link to post
Share on other sites
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://forums.xisto.com/no_longer_exists/ :8080/SampleProject/Book1.Xls")

It is showing false. 

Thanks

-question by Jayarama

Share this post


Link to post
Share on other sites

Jayarama

The following commands in biterscripting will do just that.

cat "http : // bangmsrira1:8080 / SampleProject/Book1.Xls" > x.xlssystem start x.xls

If the file does not exist, you will get an error. If the file exists, it will be opened in Excel. (I have had to add spaces in the URL so the forum won't format it further.)

Patrick

Share this post


Link to post
Share on other sites
VB renaming questionCopy A File And Rename It With Increment If ExistI was wondering if you could help me with a quick VB script, its been so long since I have done scripting I cant even do the basics now! I have a folder, in that folder there will be 10 files, with a number in them (0-9) I want to specify a new name for each file based on the number (so 0=black, 1=white etc)I need the script to check the original file name and rename it based on the original name. How would I do this? 

-question by lisa

Share this post


Link to post
Share on other sites
File RenameCopy A File And Rename It With Increment If Exist

Hi..

I Need  to Rename a Lot of Files in the Folders and Subfolders with the One Filed of Table in SQL (C# Windows Form Project).

Please HELP ME...

-reply by Mohammad Reza

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.