473,397 Members | 2,056 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Splitting & Comparing 2 Strings

I am having a problem trying to compare a file string to see if the save as file string needs to be overwritten. The logic, I believe, is to compare the 'save as string' with the already open 'file string'. Does this make sense? When the Save File dialog comes up, I need to compare the save as filename with the already open filename, and if its the same name, then ask to be overwritten. I'm not sure where to start. I believe the file extension, '.las' for example, will need to be removed, and then a comparison made. Can anyone help? Thanks in advance
Jan 11 '10 #1

✓ answered by vb5prgrmr

When user opens a file via your code and the common dialog, the file name is retrieved for you and thus you should save that into a variable. From there if user alters the file and selects save, the file name will be the same and the file contents overwritten with the new information. However, if user selects save as, as you have pointed out, then once again the common dialog returns you a path/file name and all you need to do is to compare the two...
Expand|Select|Wrap|Line Numbers
  1. Dim Ans As Integer
  2. If OrigFileName = SaveAsFileName Then
  3.   Ans = MsgBox("Overwrite file?", vbYesNoCancel,"")
  4.   If Ans = vbYes Then
  5.     'save code here
  6.   ElseIf Ans = vbNo Then
  7.     'reshow saveas dialog here or do a recursive call to whatever function this is
  8.   ElseIf Ans = vbCancel Then
  9.     'do nothing
  10.   End If
  11. Else
  12.   'Save Code goes here
  13. End If
  14.  


Good Luck

7 1258
vb5prgrmr
305 Expert 100+
When user opens a file via your code and the common dialog, the file name is retrieved for you and thus you should save that into a variable. From there if user alters the file and selects save, the file name will be the same and the file contents overwritten with the new information. However, if user selects save as, as you have pointed out, then once again the common dialog returns you a path/file name and all you need to do is to compare the two...
Expand|Select|Wrap|Line Numbers
  1. Dim Ans As Integer
  2. If OrigFileName = SaveAsFileName Then
  3.   Ans = MsgBox("Overwrite file?", vbYesNoCancel,"")
  4.   If Ans = vbYes Then
  5.     'save code here
  6.   ElseIf Ans = vbNo Then
  7.     'reshow saveas dialog here or do a recursive call to whatever function this is
  8.   ElseIf Ans = vbCancel Then
  9.     'do nothing
  10.   End If
  11. Else
  12.   'Save Code goes here
  13. End If
  14.  


Good Luck
Jan 11 '10 #2
Thanks for the help. Just what I was looking for.
Jan 11 '10 #3
I keep getting a runtime error '70', access denied. What does this mean? Code is as follows:
Expand|Select|Wrap|Line Numbers
  1. Dim Ans As Integer
  2.             'check if originalfile matches saveasfilename
  3.         If tFile = fSave Then
  4.             Ans = MsgBox(fSave & " already exists. Do you want to overwrite file?", vbquestion+vbYesNoCancel, "Save As")
  5.                 If Ans = vbYes Then
  6.                     'save code here
  7.                     Kill tFile '<<<this is the problem>>>
  8.                     If LCase(Right(fSave, 4)) = ".las" Then
  9. '                       'copy temp file
  10.                         FileCopy "C:\Temp.dat", fSave
  11.                     Else
  12.                         FileCopy "C:\Temp.dat", fSave & ".las"
  13.                     End If
  14.                 Else If Ans = vbNo Then
  15.                     'reshow saveas dialog here 
  16.                     With CommonDialog1
  17.                         .Filter = "All Files|*.las*|"
  18.                         .InitDir = fSave
  19.                         .DialogTitle = "Save File As"
  20.                         .ShowSave
  21.                     End With
  22.                     fSave = CommonDialog1.FileName
  23.                 Else If Ans = vbCancel Then
  24.                 'do nothing
  25.                 End If
  26.         Else
  27.           'Save Code goes here
  28.           FileCopy "C:\Temp.dat", fSave & ".las"
  29.         End If
  30.  
I'm not really sure how to approach this. The savefile dialog pops up to save the file. If the user selects the same filename, it should overwrite the file, but it doesn't. If the user enters a different name, it saves properly. Any suggestions? Thanks again
Jan 11 '10 #4
vb5prgrmr
305 Expert 100+
Okay, when you first open the file, how do you do it? Open Statement?...
Expand|Select|Wrap|Line Numbers
  1. Open SomeFilePathName For Input As #1
  2.  
Do you close your connection to the opened file?
Expand|Select|Wrap|Line Numbers
  1. Close #1
  2.  
If not, there is your error as you still have the file open and therefor cannot delete it.



Good Luck
Jan 12 '10 #5
I am using the code below to reference the file:
Expand|Select|Wrap|Line Numbers
  1. Set fso = CreateObject("Scripting.FileSystemObject")
  2. Set objstream = fso.opentextfile(tFile, 1, False, 0)
  3.  
tFile is referencing a file such as "C:\Temp\Temp.dat"
Is there any specific way to close the file, if using filesystemobject? I've tried just "Close" so it would close anything opened, but it didn't work. Thanks again.
Jan 12 '10 #6
I had resolved the issue. I just needed to close the fso.objstream:
Expand|Select|Wrap|Line Numbers
  1. fso = objstream.Close
  2.  
Thanks for all your help. My software is now 100% completed, thanks to you.
Jan 12 '10 #7
vb5prgrmr
305 Expert 100+
Not a problem, glad you got it solved...
Jan 13 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: agent349 | last post by:
First off, I know arrays can't be compared directly (ie: if (arrary1 == array2)). However, I've been trying to compare two arrays using pointers with no success. Basically, I want to take three...
9
by: Dr. StrangeLove | last post by:
Greetings, Let say we want to split column 'list' in table lists into separate rows using the comma as the delimiter. Table lists id list 1 aa,bbb,c 2 e,f,gggg,hh 3 ii,kk 4 m
9
by: Steve Jorgensen | last post by:
Hi all, Frankly, this is such an off-beat thing, I don't know if anyone else here will ever have the same issue. Just in case anyone does, though... I needed to be able to search for the...
7
by: theBestFriend | last post by:
If I type in the vualue && from the standard input and store it into myInputString variable, I expected that expression if(myInputString == "&&") will evaluate to true, but it doesn't. Can you...
6
by: JSheble | last post by:
I realise .NET has all these great objects, specifically strings, but is it really necessary to create a string variable (object) just to compare two string values? For example, I'm looking at...
2
by: Trint Smith | last post by:
Ok, My program has been formating .txt files for input into sql server and ran into a problem...the .txt is an export from an accounting package and is only supposed to contain comas (,) between...
20
by: Opettaja | last post by:
I am new to c# and I am currently trying to make a program to retrieve Battlefield 2 game stats from the gamespy servers. I have got it so I can retrieve the data but I do not know how to cut up...
2
by: Pugi! | last post by:
hi, I am using this code for checking wether a value (form input) is an integer and wether it is smaller than a given maximum and greater then a given minimum value: function...
29
by: Andrea | last post by:
I want to write a program that: char * strplit(char* str1, char *str2, char * stroriginal,int split_point) that take stroriginal and split in the split_point element of the string the string...
2
by: shadow_ | last post by:
Hi i m new at C and trying to write a parser and a string class. Basicly program will read data from file and splits it into lines then lines to words. i used strtok function for splitting data to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.