VBScript convert date to string
Asked Answered
H

3

6

So I have a possibly simple question that I can not find the answer to.

I am writing a VBScript that will move a subfolder from one folder to another. When moving I want to append the date onto the subfolders name. I have everything working, except I can not figure out how to convert the date to a string so that it can be added to the folder name.

curDate =  Month(Date) + "_" + Day(Date) + "_" + Year(Date) + "_" + Time

If  fs.FolderExists(rsltFldrPath) Then

'Grab folder and Subfolders
Set fldr = fs.GetFolder(rsltFldrPath)
Set subFldr = fldr.SubFolders

For each folder in subFldr
    moveTo = archFldrPath + "\" +folder.name + curDate
    fs.MoveFolder folder, moveTo
Next
End If

Any help is appreciated. Thanks!

Helminthic answered 22/7, 2011 at 15:14 Comment(0)
T
7

The string concatenation operator in VBScript is "&", so a quick fix would be:

>> curDate =  Month(Date) & "_" & Day(Date) & "_" & Year(Date) & "_" & Time
>> WScript.Echo curDate
>>
7_22_2011_5:32:55 PM

If you specify the exact desired result, I'm willing to propose a better way to achieve your goal.

Tipperary answered 22/7, 2011 at 15:36 Comment(2)
Hm...well my main goal is to have a folder called "Results" To end up being called "Results_Month_Day_Year_Time" So that when I move it there is no other possible files that it will overwrite.Helminthic
What about the space after "Results"? Do you want the PM/AM? Should the day and month be zero padded?Tipperary
D
3

Ekkehard.Horners answer won't work as Time needs to be reformatted to remove the colons (:) Folders cannot have colons.

Try;

curDate =  Month(Date) & "_" & Day(Date) & "_" & Year(Date) & "_" & Time

Replace(CurDate,":","-")

I'm sure there's a more succinct answer but that will work also padding shouldn't be an issue as the underscores will negate duplicate possibilities.

I wanted to comment but don't have enough rep.

Dorindadorine answered 30/12, 2014 at 9:46 Comment(1)
Thanks that was really accurate!Stereograph
T
-1

hi in my test I supported the days of the date on variables, then I converted the variables into strings and it worked.

Terracotta answered 5/9 at 15:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.