Help with vbscript to create a file name from reading a text file

ryan e
ryan e used Ask the Experts™
on
I am trying to have a vbscript file read a log file that has a file name in it.  Then I would like to change the name of the file in the directory to be the new name from the file.  Any suggestions would be greatfull .  I get an error each time saying bad file name or number on line 16

Const ForReading = 1


Set objFSO = CreateObject("Scripting.FileSystemObject")


Set objFile = objFSO.OpenTextFile("T:\365\rename\testrename\Log.txt", ForReading)

strCharacters = objFile.Read(70)
msgbox strcharacters
objFile.Close


strNewName = strCharacters
msgbox strnewname
objFSO.MoveFile "T:\365\rename\testrename\agood" , strNewName

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:
"T:\365\rename\testrename\agood" is the name of an existing file?

What is the value of strNewName when line 16 executes?


»bp
ryan eIT Manager

Author

Commented:
Thanks for the response bill

When I run the message box it shows that strnewName = the name Stream_Autocomplete_0_74D9645B844A494A8545116153EEEF34.dat
 in the log file

agood is the name of an existing file.  I have tried naming it agood or a.good and same result
Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:
I think you may need the full path on the destination file, so it would need to be:

"T:\365\rename\testrename\Stream_Autocomplete_0_74D9645B844A494A8545116153EEEF34.dat"


»bp
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

ryan eIT Manager

Author

Commented:
I will not know the exact name of the file so I can't do it that way. .  Each Machine I perform this on will have a different name.  Basically I have another script that gets the name of the dat file and then puts it into the log.  This script reads the log and then changes the name of the file to be what is in the log.
Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:
Based on the code you posted, you already have the path to the file hard coded in the script as "T:\365\rename\testrename\agood", so I'm a little confused...


»bp
ryan eIT Manager

Author

Commented:
Correct.  I have the path of where I will store the file.  The log file has the name Stream_Autocomplete_0_74D9645B844A494A8545116153EEEF34.dat in it (this will change)

I want to take what is in the log file (steam_autocomplete etc) and name the agood file to be Stream_Autocomplete_0_74D9645B844A494A8545116153EEEF34.dat
Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:
Then just:

objFSO.MoveFile "T:\365\rename\testrename\agood" , "T:\365\rename\testrename\" & strNewName


»bp
ryan eIT Manager

Author

Commented:
I've tried that also, but get the same result
path not found
Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:
And drive T: is mapped when you run this, you aren't trying to run it from Task Scheduler or some other automated approach that runs in an environment where the user mappings are not defined?


»bp
ryan eIT Manager

Author

Commented:
Drive T is mapped.

I even tried moving the entire 365 folder to my c: drive and running it that way and have had the same result    I am running from either double clicking the file or running from a command prompt
IT / Software Engineering Consultant
Top Expert 2016
Commented:
I didn't notice this before, but you are doing a READ() agains the file, you want to do a READLINE().  Try this adjusted code.

Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFile = objFSO.OpenTextFile("T:\365\rename\testrename\Log.txt", ForReading)

strNewName = objFile.ReadLine
MsgBox "[" & strcharacters & "]"
objFile.Close

objFSO.MoveFile "T:\365\rename\testrename\agood" , strNewName

Open in new window


»bp
ryan eIT Manager

Author

Commented:
DUDE......THAT WAS IT
Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:
Yes, I should have seen that early, I just skipped right over the Read() assuming that part was working well, and focusing on the error line, with the rename...


»bp
ryan eIT Manager

Author

Commented:
Thanks for th assistance

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial