Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Rename an external Windows file with a VFP6 string from ttoc(date) string

Status
Not open for further replies.

tomsr

Programmer
Aug 7, 2003
2
0
0
US
Renaming Windows file from VFP string
 
I don't know what you mean by an "external Window file". And what does TTOC() have to do with it?

In general, you can rename any file from within VFP by using - guess what? - the RENAME command. For example:

[tt]RENAME oldname.txt TO newname.txt[/tt]

As far as I know, the only limitation is that the file must not be open at the time.

If this does not answer your question, perhaps you would take a moment to explain a bit more clearly what you want to know.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
As Mike mentioned, this is one of the commands that accepts literal text right in the command line. Other variations:

RENAME "oldname.txt" TO "newname.txt"

RENAME ("oldname.txt") TO ("newname.txt")

oldname = "currentname.txt"
newname = "changedname.txt"
RENAME (oldname) TO (newname)

Which you choose largely depends on where your command is in code, whether the values are in variables/fields/arrays or hardcoded and which style fits best.
 
Perhaps this is what you're trying to do?
Example, you have created a backup Zip file, and its called BACKUP.ZIP
You now say newfile=ttoc(date())+".zip"
Now you can rename the file as suggested by others.
(Sytze)
 
Looking closer, I think you mean you want TTOC() included as part of the file name. Here's my explanation in VFP 9 but most of these commands should also be in VFP 6.

First these examples all display with SET CENTURY ON. If you want only the 2 digit year then SET CENTURY OFF. If you want the date in different order then use SET DATE TO DMY or SET DATE TO YMD or SET DATE TO MDY or other selections.

? TTOC(DATE())
05/23/2014 12:00:00 AM

? TTOC(DATE(), 1)
20140523000000

? TTOC(DATE(), 2)
12:00:00 AM

? TTOC(DATE(), 3)
2014-05-23T00:00:00

Probably one of those formats is what you want to use. But some will need one more modification. Windows won't allows certain characters in filenames: [highlight #FCE94F]\/:*?"<>|[/highlight] So what you need to do is block invalid characters from the filename string:

MyFileName = CHRTRAN(TTOC(DATE()), [\/:*?"<>|], [])

And if you don't want spaces either:

MyFileName = CHRTRAN(TTOC(DATE()), [\/:*?"<>| ], [])

It might look more readable coded this way:

ExclChrs = [\/:*?"<>| ]
MyFileName = CHRTRAN(TTOC(DATE()), ExclChrs, [])

Note that strings can be encapsulated by matching double quotes, single quotes or square brackets That way, any one pair can include the others inside the string. Somethings things get complicated and it's nice to have multiple options.


 
dbMark has addressed the two important things to bear in mind:

dbMark said:
oldname = "currentname.txt"
newname = "changedname.txt"
RENAME (oldname) TO (newname)
Put a variable name in brackets, otherwise VFP will take the variable name itself as file name. That's the consequence of VFPs concept of commands in difference to functions with parameters. The RENAME command gives you the ability to specify file names, which have no space in them, without quotes, so every name, no matter if variable name or field name, will be taken 1:1 as file name. Putting it in brackets forces VFP to interpret the code as expression to evaluate, so the value of a variable is taken as file name.

dbMark said:
Windows won't allows certain characters in filenames: \/:*?"<>|
This is the second part of the problem, even if you have overcome the first, Windows won't allow to put in a date with neither backslashes nor slashes, even though only a backslash is displayed as separator of directory and file names, Windows for example also understands C:/Windows/notepad.exe as a name, and therefore slashes are forbidden in file names, too.

So star dbMarks posts.

Bye, Olaf.
 
Guys, shouldn't that be TTOC(Datetime())? Otherwise, you're always getting 12:00:00 so why use it at all.

Rather, use DTOS(Date()) which returns YYYYMMDD without worrying about SET DATE or SET CENTURY.

Of course we have no idea if that's what the OP was after. <shrug>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top