Vbscript converting numeric month to specified letter

45 pts.
Tags:
VBScript
VBScript Debugging
hi folks been reading some of the posts and am blown away by the level of coding and help on the forums.....so I am a little embarrassed by the basic question I have :) i am admittedly an absolute novice when it comes to vbscript and can get my head around basics when needed, but as i have run into a problem i thought i better try reaching out to the experts. i had a batch script written to convert a filename to yesterdays date ddmmyyyy where, if mm = 01 would be changed to A and 02 would be B and so on. Although it worked it wasnt to robust when the month changed so I wanted to convert it to vbscript. I managed to get a script together to convert the file to date name and change it from .csv to .txt but have now run into a small problem of converting mm from 01 to A. I know it should be simple but the more i look at the script the more blank my mind goes. What i need to achieve is converting the month as follows: 01 to A, 02 to B, 03 to C etc. this is the script so far: Const sWD = "c:Test" Const sFName = "c:testtest1.csv" Const sFName2 = "c:testtest2.csv" Set oFSO = CreateObject("Scripting.FileSystemObject… If Not oFSO.FolderExists(sWD) then Error "Cannot locate folder """ & sWD & """" dYesterday = DateAdd("d", -1, Now()) sYesterday = 1000000 * Day(dYesterday) + 10000 * Month(dYesterday) + Year(dYesterday) if not oFSO.FileExists(sFName) then Error "Cannot locate file """ & sFName & """" if not oFSO.FileExists(sFName2) then Error "Cannot locate file """ & sFName2 & """" oFSO.MoveFile sFName, sYesterday & ".txt" oFSO.MoveFile sFName2, sYesterday & ".txt" Sub Error(sMsg) MsgBox sMsg WScript.Quit End Sub thanks for your help Rain

Software/Hardware used:
vbscript

Answer Wiki

Thanks. We'll let you know when a new response is added.

You need to use the chr function:

sYesterday = 1000000 * Day(dYesterday) + 10000 * Chr(64 + Month(dYesterday)) + Year(dYesterday)

Chr(64 + 1) will give you A
Chr(64 + 2) will give you B

Chr converts an integer in the range of the ASCII characters and turns in into the approporate ascii character. 65 is capital A.

<a href=”http://www.asciitable.com/”>ASCII Table</a>

———————-

How about something like this ?

<pre>sYesterday = Day(dYesterday) & chr(64+Month(dYesterday))&Year(dYesterday)</pre>

-CarlosDL

Discuss This Question: 3  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • Defector
    Hi Gent01 Thanks for the reply and the info. Very clean and simple conversion to ASCII that I wasnt aware of. Very nice When looking at the code with CHR in place I would recieve an error though with it trying to multiply: 10000 * Chr(64 + Month(dYesterday))
    45 pointsBadges:
    report
  • carlosdl
    You are receiving an error because the script is trying to multiply a number and a non-numeric char (i.e 10000 * "A"). I updated the answer above with another suggestion, using Gent01's idea.
    69,240 pointsBadges:
    report
  • Defector
    fantastic!!!! figured i would recieve an error due to numeric vs. non-numeric. using the base of Gent01's changes and your suggestion it works absolutely as expected. Thanks so much for sharing the knowledge!! the final code i have for others should they require same or similar is as follows: Const sWD = "c:Test" Const sFName = "c:testtest1.csv" Const sFName2 = "c:testtest2.csv" Set oFSO = CreateObject("Scripting.FileSystemObject") If Not oFSO.FolderExists(sWD) then Error "Cannot locate folder """ & sWD & """" dYesterday = DateAdd("d", -1, Now()) sYesterday = Day(dYesterday) & chr(64 + Month(dYesterday)) & Year(dYesterday) if not oFSO.FileExists(sFName) then Error "Cannot locate file """ & sFName & """" if not oFSO.FileExists(sFName2) then Error "Cannot locate file """ & sFName2 & """" oFSO.MoveFile sFName, sYesterday & ".txt" oFSO.MoveFile sFName2, sYesterday & ".txt" Sub Error(sMsg) MsgBox sMsg WScript.Quit End Sub
    45 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following