Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Append file names in folder

Status
Not open for further replies.

Nu2Java

Technical User
Jun 5, 2012
166
US
I have directories filled with job names that are just in .txt format and I want to be able to append the file name with a revision control.

job file name: "87-100-A120"

Each time I create a text file in the directory, how can I append to the file name? And create: "87-100-A120_rev1" ... "_rev2" etc etc.

Thanks for any help!
 
Are the job names sequencial? What do the numbers represent?

87-100-A120
87-100-A121
87-100-A122

You could produce a script thats reads the directory every 15 seconds for new files that do not end with a ##-###-X### mask. Once found, append the job name.

-Geates
 
Geates,

The job numbers will not be sequencial, these are just job numbers. The best part is, each job file name will have its own sub-folder.

Code:
 C:\Jobs\87-100-A120\87-100-A120.txt

I need to then create a new text file and turn the existing into "87-100-A120revA" or "rev1".
So if I was to create a text file using vbscript, how can I rename the file that will already be in the subfolder of the same name and give it the ".revA" or ".rev1" extension?

Thanks
 
All you have to do is set the [red].Name[/red] attribute.
Code:
set objFSO = CreateObject("Scripting.FileSystemObject")
set objFile = objFSO.GetFile("somefile.txt")
[red]objFile.Name = "whatever" '<-- this is how your rename a file [/red]
msgbox objFile.Name

If the file to rename is that same as the parent folder name (as in your example) [blue]we can get the folder name to create a new filename.[/blue]
Code:
set objFSO = CreateObject("Scripting.FileSystemObject")
set objFile = objFSO.GetFile("somefile.txt")

strExt = ".revA"
strFileName = objFile.Name
[blue]
strFolderName = objFSO.GetParentFolderName(objFile.Path)
strFileName = right(strFolderName, inStrRev(strFolderName, "\") + 1)
[/blue]
objFile.Name = strFileName & strExt


-Geates
 
Geates,

I have been playing around with this and I'm not sure this is the result I need. Here is an example of what I am testing out. If I just create a text file and then create the same name to the same directory, how can I continue to append to that file name?

I want to keep the originals and then add an extension to the end of each one after that so the directory will then look like this:

File1.txt
File1_rev1.txt
File1_rev2.txt
etc etc

Code:
Dim objFile, strDir, strExt, fso

strDir = ("C:\Temp2\")

Const ForAppending = 8
Const ForReading = 1

set objFSO = CreateObject("Scripting.FileSystemObject")
strExt = InputBox("Enter a file name to create: ")

set objFile = objFSO.OpenTextFile(strDir & strExt, ForAppending, True)

If objFSO.FileExists(strDir & strExt) Then
MsgBox "File already exists!"
set objFile = objFSO.OpenTextFile(strDir & strExt & "rev1", ForAppending, True)
End If
 
Here is some code that will look for an existing file/revision and create a text file with the next revision number. My simple code looks for a text file named abc123.txt in the C:\Temp folder; revisions are assumed to be of the format "<filename>_rev<digit(s)>". To try it out, create a C:\Temp folder (if it doesn't already exist, or point the script to your test folder) and make a few dummy text files named abc123.txt or abc123_rev(digits).txt and run the code. Hopefully it will be useful for your needs.

Code:
[COLOR=green]'tek-tips thread329-1696432[/color]
[COLOR=green]'get current "revision" of given file[/color]
[COLOR=green]'create new file with next rev number[/color]

[COLOR=blue]Option[/color] [COLOR=blue]Explicit[/color]  

[COLOR=blue]Dim[/color] objFSO  
[COLOR=blue]Set[/color] objFSO [COLOR=blue]=[/color] CreateObject("Scripting.FileSystemObject")  

[COLOR=blue]Dim[/color] fileToCreate, myFileFolder, myFileBase, myFileExt, newFile, nextRev  

fileToCreate [COLOR=blue]=[/color] "C:\Temp\abc123.txt"  

[COLOR=blue]if[/color] [COLOR=blue]not[/color] objFSO.FolderExists(objFSO.GetParentFolderName(fileToCreate)) [COLOR=blue]then[/color]  
	msgbox "Error, folder does not exist", "Error", vbokonly + vbcritical  
	wscript.quit  
end [COLOR=blue]if[/color]  

myFileFolder [COLOR=blue]=[/color] objFSO.GetParentFolderName(fileToCreate)  
[COLOR=green]'wscript.echo(objFSO.GetFileName(fileToCreate))[/color]
myFileBase [COLOR=blue]=[/color] objFSO.GetBaseName(fileToCreate)  
myFileExt [COLOR=blue]=[/color] objFSO.GetExtensionName(fileToCreate)  
nextRev [COLOR=blue]=[/color] GetCurrentRev(fileToCreate) [COLOR=blue]+[/color] 1  
[COLOR=green]'wscript.echo(GetCurrentRev(fileToCreate))[/color]
newFile [COLOR=blue]=[/color] objFSO.BuildPath(myFileFolder, myFileBase [COLOR=blue]&[/color] "_rev" [COLOR=blue]&[/color] nextRev [COLOR=blue]&[/color] "." [COLOR=blue]&[/color] myFileExt)  
[COLOR=blue]Set[/color] newFile [COLOR=blue]=[/color] objFSO.CreateTextFile(newFile, [COLOR=blue]True[/color])  


[COLOR=blue]Function[/color] GetCurrentRev(filePath)  
		  
		Dim folder, files, file, re, pattern, currentRev, fileRev  
		currentRev [COLOR=blue]=[/color] 0  
		  
		If [COLOR=blue]Not[/color] objFSO.FolderExists(objFSO.GetParentFolderName(filePath)) [COLOR=blue]Then[/color]  
			GetCurrentRev [COLOR=blue]=[/color] -1  
			Exit [COLOR=blue]Function[/color]  
		End [COLOR=blue]If[/color]  
		  
		set re [COLOR=blue]=[/color] [COLOR=blue]new[/color] regexp  
		re.IgnoreCase [COLOR=blue]=[/color] [COLOR=blue]true[/color]  
		re.Pattern [COLOR=blue]=[/color] objFSO.GetBaseName(filePath) [COLOR=blue]&[/color] "_rev(\d+)"  
		  
		Set folder [COLOR=blue]=[/color] objFSO.GetFolder(objFSO.GetParentFolderName(filePath))  
		Set files [COLOR=blue]=[/color] folder.Files  

		For [COLOR=blue]each[/color] file [COLOR=blue]In[/color] files  
			If re.Test(file.Name) [COLOR=blue]Then[/color]  
				fileRev [COLOR=blue]=[/color] re.Execute(file.Name)(0).SubMatches(0)  
				if fileRev > currentRev [COLOR=blue]then[/color]  
					currentRev [COLOR=blue]=[/color] fileRev  
				end [COLOR=blue]if[/color]  
			End [COLOR=blue]If[/color]  
		Next  
		  
		GetCurrentRev [COLOR=blue]=[/color] currentRev  
		  
End [COLOR=blue]Function[/color]
 
jges ... this works great! Thanks for the code! I should be able to work this in to what I need.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top