Results 1 to 10 of 10

Thread: [RESOLVED] Overwrite existing folder

  1. #1

    Thread Starter
    Lively Member elmnas's Avatar
    Join Date
    Jul 2009
    Posts
    127

    Resolved [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

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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

  5. #5

    Thread Starter
    Lively Member elmnas's Avatar
    Join Date
    Jul 2009
    Posts
    127

    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

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7

    Thread Starter
    Lively Member elmnas's Avatar
    Join Date
    Jul 2009
    Posts
    127

    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"

  8. #8

    Thread Starter
    Lively Member elmnas's Avatar
    Join Date
    Jul 2009
    Posts
    127

    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.

  9. #9
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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

  10. #10

    Thread Starter
    Lively Member elmnas's Avatar
    Join Date
    Jul 2009
    Posts
    127

    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
  •  



Click Here to Expand Forum to Full Width