[RESOLVED] Overwrite existing folder
Hello all VB Members,
I made a script,
when I have opened a .doc file and run the script.
the script can see the active location, copy all .pdf files from the active path, navigates one level up creates a folder called translation to.
my problem is..
if the folder "translation to" already exists I get an error.
can someone help me to modify my code
I want my script work this way.
if the folder already exists overwrite the folder,
also if the folder contains files keep the files but overwrite the folder.
see Code
Sub Demo()
Dim StrOldPath As String, StrNewPath As String
StrOldPath = ActiveDocument.Path
StrNewPath = Left(StrOldPath, InStrRev(StrOldPath, "\"))
StrOldPath = StrOldPath & "\"
nf = "translation to"
nf = StrNewPath & nf
MsgBox (nf)
MkDir nf
FileExt = "*.pdf*" '<< Change
'You can use *.* for all files or *.doc for Word files
If Right(StrOldPath, 1) <> "\" Then
StrOldPath = StrOldPath & "\"
End If
Set FSO = CreateObject("scripting.filesystemobject")
If FSO.FolderExists(StrOldPath) = False Then
MsgBox StrOldPath & " doesn't exist"
Exit Sub
End If
If FSO.FolderExists(nf) = True Then
MsgBox ToPath & " doesn't exist"
Exit Sub
End If
FSO.CopyFile Source:=StrOldPath & FileExt, Destination:=nf
End Sub
Re: Overwrite existing folder
Quote:
if the folder already exists overwrite the folder,
also if the folder contains files keep the files but overwrite the folder.
you can not overwrite a folder, you could rename it, existing files will remain in the renamed folder
Re: Overwrite existing folder
your logic is broken...
Code:
If FSO.FolderExists(StrOldPath) = False Then
MsgBox StrOldPath & " doesn't exist"
Exit Sub
End If
If FSO.FolderExists(nf) = True Then
MsgBox ToPath & " doesn't exist"
Exit Sub
End If
You check to see if a folder exists, and if it doesn't, you tell the user it doesn't exist and quit.
You then check to see if the other folder exists, and if it does, you tell the user it doesn't exist and quit.
Are you sure that's what you wanted?
Also, I'd suggest not having a space in the folder path as that can lead to problems later (and may also be part of the problem now) ... otherwise you need to enclose the whole path in quotes so it gets treated as a single, complete path by the system. I'm not sure how the FSO deals with it, so it may or may not be an issue, I don't know.
-tg
Re: Overwrite existing folder
@ techgnome
both vb functions and fso can work with space in paths
shell which uses dos commands is the main area with problems of this type
Re: Overwrite existing folder
here is a simpliefied code,
the code just move the pdf files not copying them
do someone know how to change that?
a friend of mine helped me out with this code.
Thank you in advance
Sub Test1()
'
' Test1 Makro
'
Dim StrOldPath As String, StrNewPath As String, strFile
StrOldPath = ActiveDocument.Path
StrNewPath = Left(StrOldPath, InStrRev(StrOldPath, "\"))
StrOldPath = StrOldPath & "\"
StrNewPath = StrNewPath & "translation to\"
If Dir(StrNewPath, vbDirectory) = "" Then
MkDir StrNewPath
End If
strFile = Dir(StrOldPath & "*.PDF", vbNormal)
While strFile <> ""
FileCopy StrOldPath & strFile, StrNewPath & strFile
Kill StrOldPath & strFile
strFile = Dir()
Wend
End Sub
Re: Overwrite existing folder
change what? you through some code at us and asked how to change it? well... by editing it... you didn't say what you want changed. Is it doing something it shouldn't be doing? Is it not doing something it should?
-tg
Re: Overwrite existing folder
westconm1
how could you solve this?
you understand what I am trying to get.
I open source.doc
at C:/jobs/341423/source/source.doc
"in C:/jobs/341423/source/" there are
2 pdfs.
2 word files.
"I need to create a folder in "C:/jobs/341423/translation to"
and put the two pdfs from "C:/jobs/341423/source/" to "C:/jobs/341423/translation to"
Re: Overwrite existing folder
I want my code to work this way
if the folder "translate to" already exists,
then copy the pdfs into the folder.
if the folder doesn't exists create folder translation to and put the pdf files.
Re: Overwrite existing folder
Quote:
if the folder "translate to" already exists,
then copy the pdfs into the folder.
if the folder doesn't exists create folder translation to and put the pdf files.
i posted that already in your other thread
Re: Overwrite existing folder
Solved by myself...
final code.
Sub Demo()
Dim StrOldPath As String, StrNewPath As String, strFile
StrOldPath = ActiveDocument.Path
StrNewPath = Left(StrOldPath, InStrRev(StrOldPath, "\"))
StrOldPath = StrOldPath & "\"
StrNewPath = StrNewPath & "translation to\"
If Dir(StrNewPath, vbDirectory) = "" Then
MkDir StrNewPath
End If
strFile = Dir(StrOldPath & "*.PDF", vbNormal)
While strFile <> ""
FileCopy StrOldPath & strFile, StrNewPath & strFile
strFile = Dir()
Wend
End Sub