Name function is stripping characters

M

mcnewsxp

i am using VBA to rename a .PS file.
the renaming causes certain formatting characters to be removed.
the "[" is removed in certain places causing the file to be usuable.
is there some other way to rename other than the Name oldfile as newfile?

i'll try the copy file function if i can figure out the syntax.
 
W

Wayne Morgan

I just tried the following in the Immediate window and it worked correctly.

Name "c:\test\this is a [test].txt" As "c:\test\[This] is a test.txt"
 
M

mcnewsxp

I just tried the following in the Immediate window and it worked correctly.
Name "c:\test\this is a [test].txt" As "c:\test\[This] is a test.txt"

it works but removes some formatting from .ps files.
so do all the forms of filecopy and copyfile.
next will be to try shell.
i think i have to use environ(comspec) or something...
 
W

Wayne Morgan

To use Shell, I had to use Cmd.exe instead of Command.com if there were
spaces in the file name. Command.com would take the space as a parameter
delimiter, even if the space was inside quotes, but Cmd.exe would accept the
space(s) without a problem.

Example:
Public Function TestShell()
Dim strOldName As String, strNewName As String, strCommandLine As String
strOldName = """c:\test\[This] is a test.txt"""
strNewName = """This is a [test].txt"""
strCommandLine = "cmd.exe /c ren " & strOldName & " " & strNewName
Debug.Print strCommandLine
Shell strCommandLine
End Function

--
Wayne Morgan
MS Access MVP


mcnewsxp said:
I just tried the following in the Immediate window and it worked
correctly.

Name "c:\test\this is a [test].txt" As "c:\test\[This] is a test.txt"

it works but removes some formatting from .ps files.
so do all the forms of filecopy and copyfile.
next will be to try shell.
i think i have to use environ(comspec) or something...
 
W

Wayne Morgan

PS,

Just curious. What do you mean it removes formatting from .ps files? Are
these Post Script print files?
 
M

mcnewsxp

PS,

Just curious. What do you mean it removes formatting from .ps files? Are
these Post Script print files?

some kind of adobe format.
i'm doing a line input search and replace.
users need to pass through the file several times.
on all passes after the first i do a rename and kill of the temp file.
i may need to fihure out a way to do all the formatting in one pass.
the rename removes the "[" character but only in certain blocks of the file.

it removes the [ from the beginning of the first line below:

[5.03938 4.97831 5.03938 5.03938 2.87958 6.46717 2.16146 5.76022 2.87958
5.76022 6.47835
5.70001 2.87958 6.46717 6.48179 6.48179 2.87958 5.03938 5.21829 2.87958
4.97831 5.03938
5.03938 2.16146 5.03938 5.03938 5.03938 5.03938 5.03938 5.03938 4.97831
5.03938 5.03938
5.03938 2.16146 6.46717 5.03938 5.03938 5.03938 2.10125 2.16146 5.03938
5.03938 2.87958
4.32033 5.03938 5.03938 2.87958 5.03938 5.03938 2.16146 2.10125 5.76022
5.03852 5.99417
] pdfxs
 
G

Guest

No, the VBA.Name function does not do that.

If that is happening, it is not the Name function.

You need to examine your code and data files again.

(david)

mcnewsxp said:
PS,

Just curious. What do you mean it removes formatting from .ps files? Are
these Post Script print files?

some kind of adobe format.
i'm doing a line input search and replace.
users need to pass through the file several times.
on all passes after the first i do a rename and kill of the temp file.
i may need to fihure out a way to do all the formatting in one pass.
the rename removes the "[" character but only in certain blocks of the file.

it removes the [ from the beginning of the first line below:

[5.03938 4.97831 5.03938 5.03938 2.87958 6.46717 2.16146 5.76022 2.87958
5.76022 6.47835
5.70001 2.87958 6.46717 6.48179 6.48179 2.87958 5.03938 5.21829 2.87958
4.97831 5.03938
5.03938 2.16146 5.03938 5.03938 5.03938 5.03938 5.03938 5.03938 4.97831
5.03938 5.03938
5.03938 2.16146 6.46717 5.03938 5.03938 5.03938 2.10125 2.16146 5.03938
5.03938 2.87958
4.32033 5.03938 5.03938 2.87958 5.03938 5.03938 2.16146 2.10125 5.76022
5.03852 5.99417
] pdfxs
 
W

Wayne Morgan

I would have to agree with David until someone proves me wrong. Just
renaming the file should have NO effect what so ever on the contents of the
file. You have something else going on.

--
Wayne Morgan
MS Access MVP


mcnewsxp said:
PS,

Just curious. What do you mean it removes formatting from .ps files? Are
these Post Script print files?

some kind of adobe format.
i'm doing a line input search and replace.
users need to pass through the file several times.
on all passes after the first i do a rename and kill of the temp file.
i may need to fihure out a way to do all the formatting in one pass.
the rename removes the "[" character but only in certain blocks of the
file.

it removes the [ from the beginning of the first line below:

[5.03938 4.97831 5.03938 5.03938 2.87958 6.46717 2.16146 5.76022 2.87958
5.76022 6.47835
5.70001 2.87958 6.46717 6.48179 6.48179 2.87958 5.03938 5.21829 2.87958
4.97831 5.03938
5.03938 2.16146 5.03938 5.03938 5.03938 5.03938 5.03938 5.03938 4.97831
5.03938 5.03938
5.03938 2.16146 6.46717 5.03938 5.03938 5.03938 2.10125 2.16146 5.03938
5.03938 2.87958
4.32033 5.03938 5.03938 2.87958 5.03938 5.03938 2.16146 2.10125 5.76022
5.03852 5.99417
] pdfxs
 
M

mcnewsxp

I would have to agree with David until someone proves me wrong. Just
renaming the file should have NO effect what so ever on the contents of the
file. You have something else going on.

nothing much is going on except reading a line of input, checking for some
string character pattern and replacing if found, then write the line to a
temp file, and finally close the files
no corruption occurs until the rename processes occurs on the second pass.
 
M

mcnewsxp

I would have to agree with David until someone proves me wrong. Just
renaming the file should have NO effect what so ever on the contents of the
file. You have something else going on.

i can tell you the same thing occurs if you open the file in word and doa
'save as' but does not ocur if you simply 'save.'
 
D

Douglas J. Steele

mcnewsxp said:
i can tell you the same thing occurs if you open the file in word and doa
'save as' but does not ocur if you simply 'save.'

Opening a file in Word and use Save As is a completely different thing than
using FileCopy or Name in VBA.

For example, opening an MDB file in Word will corrupt it beyond repair.
 
W

Wayne Morgan

How does the "rename process" occur on the "second pass"? You shouldn't be
renaming a file while you're writing to it. Please post the code you're
using.
 
M

mcnewsxp

Private Sub ImportPSFile(sTxtInFile As String)

'''label vars
Dim strPSFile As String
'''marker vars
Dim fso As New FileSystemObject, fil1
Dim strReplaceText As String
Dim txtFile

Dim strCmd As String

Screen.MousePointer = 11
Label3.ForeColor = vbBlack
Label3.Caption = "Formatting..."
DoEvents

On Error GoTo ImportError

stringDataPath = Application.CurrentProject.Path & "\"
If Dir(stringDataPath & "\NEW" & sTxtInFile) <> "" Then
Kill stringDataPath & "\NEW" & sTxtInFile
End If

If Dir(stringDataPath & "\TEMP" & sTxtInFile) <> "" Then
Open stringDataPath & "\TEMP" & sTxtInFile For Input As #1
Set txtFile = fsoOut.CreateTextFile(stringDataPath & "TEMP2" &
sTxtInFile, True)
Else
Open stringDataPath & "\" & sTxtInFile For Input As #1
Set txtFile = fsoOut.CreateTextFile(stringDataPath & "TEMP" & sTxtInFile,
True)
End If

Select Case Me.cbxReplaceText.Value
Case Is = "Black"
strReplaceText = "1 1 1 setrgbcolor " & vbCrLf &
Me.cbxSearchText.Value & "-"
Case Is = "Blue"
strReplaceText = "0 0 1 setrgbcolor" & vbCrLf & Me.cbxSearchText.Value
& "-"
Case Is = "Dark Gray"
strReplaceText = "0.7 0.7 0.7 setrgbcolor " & vbCrLf &
Me.cbxSearchText.Value & "-"
Case Is = "Gray"
strReplaceText = "0.5 0.5 0.5 setrgbcolor " & vbCrLf &
Me.cbxSearchText.Value & "-"
Case Is = "Red"
strReplaceText = "1 0 0 setrgbcolor " & vbCrLf &
Me.cbxSearchText.Value & "-"
Case Is = "Violet"
strReplaceText = "1 0 1 setrgbcolor " & vbCrLf &
Me.cbxSearchText.Value & "-"
End Select

Do Until EOF(1)
'Grab one row's worth of data
Line Input #1, strPSFile

If InStr(strPSFile, "[5") > 0 Then
boolStop = True
End If

If InStr(strPSFile, Trim(Me.cbxSearchText.Value) & "-") > 0 Then
strPSFile = Replace(strPSFile, Trim(Me.cbxSearchText.Value & "-"),
strReplaceText)

txtFile.WriteLine (strPSFile)
Line Input #1, strPSFile
Else

txtFile.WriteLine (strPSFile)
End If

Loop

Close #1
Set txtFile = Nothing

If Dir(stringDataPath & "\TEMP2" & sTxtInFile) <> "" Then
Kill stringDataPath & "\TEMP" & sTxtInFile
Name stringDataPath & "\TEMP2" & sTxtInFile As stringDataPath &
"\TEMP" & sTxtInFile
End If

Label3.Caption = "Done!"

Done:
Screen.MousePointer = 0
Exit Sub

ImportError:
MsgBox Err.Description
Label3.ForeColor = vbRed
Label3.Caption = "Error!"
Close #1
GoTo Done

End Sub
 
M

mcnewsxp

I would have to agree with David until someone proves me wrong. Just
Opening a file in Word and use Save As is a completely different thing
than using FileCopy or Name in VBA.

For example, opening an MDB file in Word will corrupt it beyond repair.

again, save does no harm.
the file appears to be nothing but text.
 
G

Guest

'Name' maps to a windows api which changes the value in
the 'directory', which also contains a pointer to the 'file'

The windows 'Name' API does not change the file contents.

It's a computer. It only does what you tell it, and it is very
literal. 'Name' does not change the file contents.

Understanding what the computer does is a starting point
for working out what you have done.

'Name' does not change the file contents. You need to
re-think your explanation of the behaviour you are seeing.

(david)
 
W

Wayne Morgan

Do Until EOF(1)
'Grab one row's worth of data
Line Input #1, strPSFile

If InStr(strPSFile, "[5") > 0 Then
boolStop = True
End If

If InStr(strPSFile, Trim(Me.cbxSearchText.Value) & "-") > 0 Then
strPSFile = Replace(strPSFile, Trim(Me.cbxSearchText.Value & "-"),
strReplaceText)

In your code listed above, you start your loop, get the first line from the
input file, check to see if "[5" is in the string and if it is set boolStop
to True, then you replace the text that matches the combo box selection with
the replacement text you've specified. I'm guessing that if "[5" is in the
string you're wanting to not do the replacement (strictly judging by the
name of your variable boolStop); however, while you set the value of this
variable to True, you then never do anything with it. It is not used in any
If statement or anything else in this procedure that would cause your code
to "stop" doing what it's doing. I also noticed that boolStop was NOT Dim'ed
in this procedure, so you may be using the value of it elsewhere, but I
can't tell that by what you have here.

I would recommend commenting out the Name line in the code and checking the
contents of the file TEMP2*. This will let you see what its contents are
before it gets renamed.
ImportError:
MsgBox Err.Description
Label3.ForeColor = vbRed
Label3.Caption = "Error!"
Close #1
GoTo Done

Unless you're trying to keep Err dirty for some reason, the last line should
be

Resume Done

instead of
GoTo Done

although, the error should clear when you exit the procedure anyway. So it
may just be a technicality.

--
Wayne Morgan
MS Access MVP


mcnewsxp said:
Private Sub ImportPSFile(sTxtInFile As String)

'''label vars
Dim strPSFile As String
'''marker vars
Dim fso As New FileSystemObject, fil1
Dim strReplaceText As String
Dim txtFile

Dim strCmd As String

Screen.MousePointer = 11
Label3.ForeColor = vbBlack
Label3.Caption = "Formatting..."
DoEvents

On Error GoTo ImportError

stringDataPath = Application.CurrentProject.Path & "\"
If Dir(stringDataPath & "\NEW" & sTxtInFile) <> "" Then
Kill stringDataPath & "\NEW" & sTxtInFile
End If

If Dir(stringDataPath & "\TEMP" & sTxtInFile) <> "" Then
Open stringDataPath & "\TEMP" & sTxtInFile For Input As #1
Set txtFile = fsoOut.CreateTextFile(stringDataPath & "TEMP2" &
sTxtInFile, True)
Else
Open stringDataPath & "\" & sTxtInFile For Input As #1
Set txtFile = fsoOut.CreateTextFile(stringDataPath & "TEMP" &
sTxtInFile, True)
End If

Select Case Me.cbxReplaceText.Value
Case Is = "Black"
strReplaceText = "1 1 1 setrgbcolor " & vbCrLf &
Me.cbxSearchText.Value & "-"
Case Is = "Blue"
strReplaceText = "0 0 1 setrgbcolor" & vbCrLf &
Me.cbxSearchText.Value & "-"
Case Is = "Dark Gray"
strReplaceText = "0.7 0.7 0.7 setrgbcolor " & vbCrLf &
Me.cbxSearchText.Value & "-"
Case Is = "Gray"
strReplaceText = "0.5 0.5 0.5 setrgbcolor " & vbCrLf &
Me.cbxSearchText.Value & "-"
Case Is = "Red"
strReplaceText = "1 0 0 setrgbcolor " & vbCrLf &
Me.cbxSearchText.Value & "-"
Case Is = "Violet"
strReplaceText = "1 0 1 setrgbcolor " & vbCrLf &
Me.cbxSearchText.Value & "-"
End Select

Do Until EOF(1)
'Grab one row's worth of data
Line Input #1, strPSFile

If InStr(strPSFile, "[5") > 0 Then
boolStop = True
End If

If InStr(strPSFile, Trim(Me.cbxSearchText.Value) & "-") > 0 Then
strPSFile = Replace(strPSFile, Trim(Me.cbxSearchText.Value & "-"),
strReplaceText)

txtFile.WriteLine (strPSFile)
Line Input #1, strPSFile
Else

txtFile.WriteLine (strPSFile)
End If

Loop

Close #1
Set txtFile = Nothing

If Dir(stringDataPath & "\TEMP2" & sTxtInFile) <> "" Then
Kill stringDataPath & "\TEMP" & sTxtInFile
Name stringDataPath & "\TEMP2" & sTxtInFile As stringDataPath &
"\TEMP" & sTxtInFile
End If

Label3.Caption = "Done!"

Done:
Screen.MousePointer = 0
Exit Sub

ImportError:
MsgBox Err.Description
Label3.ForeColor = vbRed
Label3.Caption = "Error!"
Close #1
GoTo Done

End Sub
 
M

mcnews

i meant to remove all of the boolStop stuff.
i was using it for debugging to see if it ( [5 ) was getting stripped
before it was written.
it wasn't.
sorry for confusion.
 
M

mcnews

I would recommend commenting out the Name line in the code and checking the
contents of the file TEMP2*. This will let you see what its contents are
before it gets renamed.

i tried this.
that is how i know it is happening after the rename.
 
W

Wayne Morgan

Well, there should be nothing in the Rename to cause this. Have you tried
DOS commands to see if this still happens? This sounds more like a Windows
problem or, more likely, there is something you're antivirus is picking up
in the file and removing as you rename it. Something about the string may
look like part of a virus. This is just a guess, but if every file that got
renamed had data changed/missing then your computer wouldn't run because you
wouldn't be able to install any programs successfully. Try disabling your
antivirus temporarily and see what happens.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top