Solved VB Script syntax problem

Nosmas

New member
Local time
6:04 PM
Messages
46
Location
Chipstead, Surrey, UK
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 Computer

Computer type
PC/Desktop
Computer Manufacturer/Model Number
Dell Optiplex 9030
OS
Microsoft Windows 10 Pro 64-bit Multiprocessor
CPU
Intel(R) Core(TM) i3-4160 CPU @3.60 GHz x 4
Memory
4.00 GB
Monitor(s) Displays
23-inch full-HD WLED
Screen Resolution
1920 x 1080 @ 60 Hz
Hard Drives
1 x 2.5-inch SATA hard drive
PSU
185 Watt
Browser
MS Edge and IE 11
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 Computer

Computer type
Laptop
Computer Manufacturer/Model Number
HP Elitebook 8540p
OS
Windows 7 Pro 32
CPU
Intel(R) Core(TM) i5 CPU M 540 @ 2.53GHz
Motherboard
Hewlett-Packard 1521
Memory
4,00 GB (Usable 2,98)
Graphics Card(s)
NVIDIA NVS 5100M
Sound Card
NVIDIA High Definition Audio
Screen Resolution
1600x900
Hard Drives
INTEL SSDSA2CW120G3
Antivirus
F-Secure Internet Security
Browser
IE, Firefox, Opera
Other Info
Sandboxie,
SRP (Software Restriction Policy),
EMET (Enhanced Mitigation Experience Toolkit),
WFC (Windows Firewall Control by BiniSoft),
Malwarebytes Premium
Please echo this: ("c:\Users\Peter\Desktop\" & strLogFileName & ".txt")
and post the result
 

My Computer

Computer type
Laptop
Computer Manufacturer/Model Number
ACER ASPIRE 5742G
OS
Microsoft Windows 7 Home Premium 64-bits 7601 Multiprocessor Free Service Pack 1
CPU
Intel(R) Core(TM) i3 CPU M 370 @ 2.40GHz
Motherboard
Acer Aspire 5742G
Memory
4,00 GB
Graphics Card(s)
ATI Mobility Radeon HD 5400 Series
Sound Card
(1) AMD High Definition Audio Device (2) Realtek High Defi
Screen Resolution
1366 x 768 x 32 bits (4294967296 colors) @ 60 Hz
Hard Drives
WDC WD5000BEVT-22ZAT0
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 Computer

Computer type
PC/Desktop
Computer Manufacturer/Model Number
Dell Optiplex 9030
OS
Microsoft Windows 10 Pro 64-bit Multiprocessor
CPU
Intel(R) Core(TM) i3-4160 CPU @3.60 GHz x 4
Memory
4.00 GB
Monitor(s) Displays
23-inch full-HD WLED
Screen Resolution
1920 x 1080 @ 60 Hz
Hard Drives
1 x 2.5-inch SATA hard drive
PSU
185 Watt
Browser
MS Edge and IE 11
Then script it to YYMMDD ;)

Or set a hard coded value for the today variable just to test: 14-09-11
 

My Computer

Computer type
Laptop
Computer Manufacturer/Model Number
HP Elitebook 8540p
OS
Windows 7 Pro 32
CPU
Intel(R) Core(TM) i5 CPU M 540 @ 2.53GHz
Motherboard
Hewlett-Packard 1521
Memory
4,00 GB (Usable 2,98)
Graphics Card(s)
NVIDIA NVS 5100M
Sound Card
NVIDIA High Definition Audio
Screen Resolution
1600x900
Hard Drives
INTEL SSDSA2CW120G3
Antivirus
F-Secure Internet Security
Browser
IE, Firefox, Opera
Other Info
Sandboxie,
SRP (Software Restriction Policy),
EMET (Enhanced Mitigation Experience Toolkit),
WFC (Windows Firewall Control by BiniSoft),
Malwarebytes Premium
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.
 
Last edited:

My Computer

Computer type
PC/Desktop
Computer Manufacturer/Model Number
Dell Optiplex 9030
OS
Microsoft Windows 10 Pro 64-bit Multiprocessor
CPU
Intel(R) Core(TM) i3-4160 CPU @3.60 GHz x 4
Memory
4.00 GB
Monitor(s) Displays
23-inch full-HD WLED
Screen Resolution
1920 x 1080 @ 60 Hz
Hard Drives
1 x 2.5-inch SATA hard drive
PSU
185 Watt
Browser
MS Edge and IE 11
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 Computer

Computer type
Laptop
Computer Manufacturer/Model Number
ACER ASPIRE 5742G
OS
Microsoft Windows 7 Home Premium 64-bits 7601 Multiprocessor Free Service Pack 1
CPU
Intel(R) Core(TM) i3 CPU M 370 @ 2.40GHz
Motherboard
Acer Aspire 5742G
Memory
4,00 GB
Graphics Card(s)
ATI Mobility Radeon HD 5400 Series
Sound Card
(1) AMD High Definition Audio Device (2) Realtek High Defi
Screen Resolution
1366 x 768 x 32 bits (4294967296 colors) @ 60 Hz
Hard Drives
WDC WD5000BEVT-22ZAT0
Back
Top