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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change file name (VBScript)

Status
Not open for further replies.

jordan15

Technical User
Dec 5, 2006
9
GB
Hi Guys,
Need your help once again,
I have a script that runs fine now but the files it uses I need to change the name as at the moment the user will only put in the batch number but the file name also has the question number as part of the file name. I would like to search folders based on the batch number only as several files in the folder belong to the same batch number and I would like to retrieve files from the same batch number only.

for e.g

user putS in F117
the files in the folder are
K0019F117.txt
K0019F118.txt
K0019F116.txt
K0020F117.txt
K0019F115.txt
K0021F117.txt
K0002F117.txt

So i would like my script to only retrieve all files that end with F117

My script I am using is
Option Explicit
Dim objFSO, objFolder, objShell, objTextFile, objFile, sRead
Dim strDirectory, strFile, strText,strDirectory2, strFile2, files, file, index
strDirectory = "c:\input"
strDirectory2 = "c:\fso2\test2"
strFile = "this will come from the user as f117 but currentlty is K0019F117.txt etc "
strFile2 = "\fso.txt"


' Create the File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Check that the strDirectory folder exists
If objFSO.FolderExists(strDirectory) Then
Set objFolder = objFSO.GetFolder(strDirectory)
Else

WScript.Echo " The File" & strDirectory & "does not exist"
WScript.Quit
End If


set objFile = nothing
set objFolder = nothing

Const ForAppending = 8, ForReading = 1, ForWriting = 2
set files = objFSO.GetFolder("c:\input").Files
index=1
for each file in files
if index<>5 then

Set objTextFile = objFSO.OpenTextFile _
(strDirectory & "\" THIS IS WERE I WANT THE NEW FILE NAME ie E117 TO BE, 1, True)

' Writes strText every time you run this VBScript

if index= 1 then
sRead = objTextFile.ReadAll
else
objTextFile.SkipLine

sRead = objTextFile.ReadAll

end if
objTextFile.Close

if index=1 then

Set objTextFile = objFSO.OpenTextFile _
(strDirectory2 & strFile2, 2, True)
else
Set objTextFile = objFSO.OpenTextFile _
(strDirectory2 & strFile2, 8, True)
end if
' Writes strText every time you run this VBScript
objTextFile.Write(sRead)

objTextFile.Close
index=index+1
end if
next
'objTextFile.Close
WScript.Quit


Thanks in advance
 
Jordan I am having a really hard time trying to follow your logic.

Forget your code for the moment and just provide the logic of what you want to do. For example:

1. Check if directory exists and if so bind to it.
2. Enumerate files looking for end of file name to be a specified value
3. Do "what" to the file if it matches the criteria?
4. etc.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
Sorry ignore the question above as this can not work as the files are not the same, what I would need to do is because each file has question headings as the first line i need to do a match based on the file first line and if it is a match open if it is not then move to the next file. Every time this process happens a new file name should be created in the second folder. The file names in the second folder should increment by one. So second folder should have 1.txt, 2.txt, 3.txt etc.
 
So basically
I have a folder that contains files say 100 and they all have different question asked which is in the first line so e.g
One file will have questions like what is your name|age|eye color
2nd file what is your cat name|were do you work
3rd file were do you live|what is your address
etc,etc

the questions are repeated in different files as they all have differnt answers what I want to do is merge all identical answers together and this is possible as they will have the same headings(1st line).
So the first question may be in 5 different files in the folder. I need to look at the first line of each file and if they match then merge into one file using the script above. Which at the moment merges all the files in the folder.

Thanks
 
Yes thanks, the only thing is I do not even know where the info is coming from I have jsut been given the text files and been told to sort it out. I have asked about the source of the info but the person who passed it on to me does not know.
Thanks amyway
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top