Windows 7 Forums

Welcome to Windows 7 Forums. Our forum is dedicated to helping you find support and solutions for any problems regarding your Windows 7 PC be it Dell, HP, Acer, Asus or a custom build. We also provide an extensive Windows 7 tutorial section that covers a wide range of tips and tricks.


Windows 7: VB Script syntax problem

11 Sep 2014   #1
Nosmas

Microsoft Windows 7 Home Premium 32-bit 7601 Multiprocessor Free Service Pack 1
 
 
VB Script syntax problem

Although I am fairly new to VBS I have successfully modified a Macrium Reflect generated script for backups of my system, but now wish to enhance the script and to write (log) entries to a text file. Each time the backup is run I want to create a new text file and to differentiate between files by incorporating the date (and possibly the time) in the file title. The following is an extract which is giving me a problem: -

1 OPTION EXPLICIT
2 Dim objFSO, objLog, today, strLogFileName
3
4 today = Date()
5 wscript.echo today
6 strLogFileName = "Backup-log " & today
7 wscript.echo strLogFileName
8
9 Set ObjFSO = CreateObject("Scripting.FileSystemObject")
10 Set objLog = objFSO.CreateTextFile("c:\Users\Peter\Desktop\" & strLogFileName & ".txt")

The line numbers are of course NOT part of the script. The echo's at lines 5 and 7 are purely to ensure that the correct parameters are being constructed and will not be included when the script finally runs, and Desktop will eventually be replaced by a different folder. Line 10 is giving a runtime error of 'Path not found'. Can someone please advise me what I have done wrong in this script?


My System SpecsSystem Spec
.
11 Sep 2014   #2
Tookeri

Windows 7 Pro 32
 
 

Nothing wrong with your code. I tested it and it created a file called Backup-log 2014-09-11.txt

But I assume maybe you have a different date format? Make sure you don't have any / or \ in the file name before creating the text file.
My System SpecsSystem Spec
11 Sep 2014   #3
Kaktussoft

Microsoft Community Contributor Award Recipient

Microsoft Windows 7 Home Premium 64-bits 7601 Multiprocessor Free Service Pack 1
 
 

Please echo this: ("c:\Users\Peter\Desktop\" & strLogFileName & ".txt")
and post the result
My System SpecsSystem Spec
.

11 Sep 2014   #4
Nosmas

Microsoft Windows 7 Home Premium 32-bit 7601 Multiprocessor Free Service Pack 1
 
 

Tookeri and Kaktussoft thank you for your replies. I inserted a line to echo the string following CreateTextFile INCLUDING the opening and closing brackets and when I ran the script it echoed the string WITHOUT the brackets but with the correct date 11/09/14 (as per my system's date format, although I could script this to YYMMDD if I wanted to) BUT then displayed a Runtime error of 'Path not found' and of course did not create the text file.
My System SpecsSystem Spec
11 Sep 2014   #5
Tookeri

Windows 7 Pro 32
 
 

Then script it to YYMMDD

Or set a hard coded value for the today variable just to test: 14-09-11
My System SpecsSystem Spec
11 Sep 2014   #6
Nosmas

Microsoft Windows 7 Home Premium 32-bit 7601 Multiprocessor Free Service Pack 1
 
 

Changed Dim 'today' to 'thisday' and added function: -

' ### This function gets the date in format yymmdd ###
Function Today_Date()
thisday=Right(Year(Date),2) & Right("0" & Month(Date),2) & Right("0" & Day(Date),2)
End Function

Result
echo thisday = 140911
echo strLogFileName = Backup-log 140911
echo of path = c:\Users\Peter\Desktop\Backup-log 140911.txt
and created file with correct title.

Eureka!!! I must admit I don't really understand exactly how the function works but IT DOES!! It seems that two digits of each element of the date are specified but exactly what does the 'Right' accomplish and what are the "0"'s for? Anyway thanks for the suggestion to alter the format. Perhaps the / in the date are the cause of the problem "Path not found"?

One thing I have discovered is that if the script is run a second or more times on the same day it does not result in an error message saying the file already exists, but just does not attempt to create another file of the same name. Obviously if there is a likelihood of the script being run more than once a day then it would be necessary to include the time in the name.
My System SpecsSystem Spec
11 Sep 2014   #7
Kaktussoft

Microsoft Community Contributor Award Recipient

Microsoft Windows 7 Home Premium 64-bits 7601 Multiprocessor Free Service Pack 1
 
 

Quote   Quote: Originally Posted by Nosmas View Post
Changed Dim 'today' to 'thisday' and added function: -

' ### This function gets the date in format yymmdd ###
Function Today_Date()
thisday=Right(Year(Date),2) & Right("0" & Month(Date),2) & Right("0" & Day(Date),2)
End Function

Result
echo thisday = 140911
echo strLogFileName = Backup-log 140611
echo of path = c:\Users\Peter\Desktop\Backup-log 140611.txt
and created file with correct title.

Eureka!!! I must admit I don't really understand exactly how the function works but IT DOES!! It seems that two digits of each element of the date are specified but exactly what does the 'Right' accomplish and what are the "0"'s for? Anyway thanks for the suggestion to alter the format. Perhaps the / in the date are the cause of the problem "Path not found"?

One thing I have discovered is that if the script is run a second or more times on the same day it does not result in an error message saying the file already exists, but just does not attempt to create another file of the same name. Obviously if there is a likelihood of the script being run more than once a day then it would be necessary to include the time in the name.
It adds "0" in front of the month number for example. After that it takes only the last 2 characters.

month 3-> month 03
month 11-> month 011 -> (keep last 2 only) 11


/ is not allowed in a filename!
My System SpecsSystem Spec
Reply

 VB Script syntax problem




Thread Tools Search this Thread
Search this Thread:

Advanced Search




Similar help and support threads
Thread Forum
Problem with VBS Syntax
I have been experimenting with running Scheduled backups with Macrium Reflect using a generated VB Script which I have successfully modified. I now wish to enhance the VBS to perform the backups only if an external drive exists (i.e. it is switched on), and if there is sufficient space for the...
Software
Problem with a script that should remove and add shortcuts
Hi. Im going to install windows 7 on 20+ machines. So I have taken an old auto-install script I had for windows XP and trying to make it work for windows 7. WHat I want it to do is to remove the shortcut "spark.exe.ink" for all users from the desktop and I want "spark.exe.ink" to be added...
Installation & Setup
problem at installing greasemonkey's script
I have a problem at installing greasemonkey's script at firefox on windows 7 x64. After installing greasemonkey, I select a script I wanto install, and press install button. Then the browser shows the codes of the script. Please let me know how to resolve this problem. Thanks in advance.
Browsers & Mail
Problem with echo in batch script
Morning i have a problem with batch script in Windows 7. this is my code: @echo off set mypath=c:\tools if not exist "%mypath%\architet.txt" goto check3264 find /I "x64-based PC" "%mypath%\architet.txt" >nul 2>&1 if %errorlevel% EQU 0 (set architet=64) ELSE (set architet=32) goto uacok...
General Discussion
excel syntax problem for formula... difference in days
if I have 2 columns of dates and I want to find the number of days between the days of D2 and C2, what is the syntax for the formula so that the result in column E is x number of days between the two dates
Microsoft Office
Restart time script problem
I was using the restart script from the above stickied reboot time post. It was working fine now all of a sudden it says Can't Fine script engine "VBScript" for script I havnt done anything but uninstall mcafee and install comodo firewall with avast anti virus. Under associate programs it says...
Performance & Maintenance


Our Sites

Site Links

About Us

Find Us

Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

Designer Media Ltd

All times are GMT -5. The time now is 16:21.

Twitter Facebook Google+



Windows 7 Forums

Seven Forums Android App Seven Forums IOS App