|
-
Jul 10th, 2014, 06:54 AM
#1
Thread Starter
Lively Member
[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
-
Jul 10th, 2014, 07:04 AM
#2
Re: Overwrite existing folder
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
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Jul 10th, 2014, 07:05 AM
#3
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
-
Jul 10th, 2014, 07:33 AM
#4
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
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Jul 10th, 2014, 08:40 AM
#5
Thread Starter
Lively Member
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
-
Jul 10th, 2014, 08:55 AM
#6
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
-
Jul 10th, 2014, 08:58 AM
#7
Thread Starter
Lively Member
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"
-
Jul 10th, 2014, 09:00 AM
#8
Thread Starter
Lively Member
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.
-
Jul 10th, 2014, 04:07 PM
#9
Re: Overwrite existing folder
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
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Jul 11th, 2014, 02:20 AM
#10
Thread Starter
Lively Member
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
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|