Results 1 to 9 of 9
  1. #1
    5 Star Lounger
    Join Date
    Jul 2002
    Location
    Hatsukaichi, Hiroshima, Japan
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Batch File to move files without overwriting (XP Pro SP2)

    Is there any way to set up a batch file to move files automatically without asking me but without overwriting files that have the same name. I thought I could use the /-y switch but this pauses the batch file and waits for an answer. If I use the /y switch will that overwrite files without asking? I want the process to run automatically but I don't want to overwrite files. I hope someone can help.

    Thanks,

    Chris (hunt)

  2. #2
    Platinum Lounger
    Join Date
    Jan 2001
    Location
    Quedgeley, Gloucester, England
    Posts
    5,333
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Batch File to move files without overwriting (

    MOVE is one of the less-good DOS-type utilities. You are fairly non-specific about what you want doing, but a BATch file something along the lines of:<pre>md c:targetdir
    cd c:sourcedir
    for %%a in (*.*) do if not exist "c:targetdir%%a" ECHO move "%%a" c:targetdir</pre>

    Test it first to see what would get moved, then remove the word ECHO.

    John
    <font face="Script MT Bold"><font color=blue><big><big>John</big></big></font color=blue></font face=script>

    Ita, esto, quidcumque...

  3. #3
    5 Star Lounger
    Join Date
    Jul 2002
    Location
    Hatsukaichi, Hiroshima, Japan
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Batch File to move files without overwriting (

    Thanks John, your idea works wonderfully except for file names beginning with ( . First off I tried the following but it didn't work:

    for %%a in ((*.cdr) do if not exist "F:Coreldraw Files A-ZCoreldraw Files 1-10 etc%%a" move "%%a" F:Coreldraw Files A-ZCoreldraw Files 1-10 etc

    I've tried various ways to escape the ( character but I haven't been successful. Is it possible?

    What I'm doing is automatically moving files from a working folder to folders named alphabetically. If I accidentally create a file with a duplicate file name I don't want to overwrite the existing file. I hardly ever create files beginning with a number or with the ( character so they all go together in the folder:

    F:Coreldraw Files A-ZCoreldraw Files 1-10 etc

    But I have one series of files that does start with ( so if I can get the batchfile to work with them as well it would be useful.

    Thanks again,

    Chris

  4. #4
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: Batch File to move files without overwriting (

    VBScript version (only tested once, but successfully):
    <pre>Option Explicit
    Dim oFSO, oSFolder, oFile
    Dim strTFolder, strSFolder, strFileName
    Dim intCount

    Set oFSO = CreateObject("Scripting.FileSystemObject")

    strTFolder = "C:targetdir"
    strSFolder = "C:sourcedir"

    Set oSFolder = oFSO.GetFolder(strSFolder)

    For Each oFile In oSFolder.Files
    strFileName = oFile.Name
    If Not oFSO.FileExists(oFSO.BuildPath(strTFolder, strFileName)) Then
    intCount = intCount + 1
    oFSO.MoveFile oFile.Path, strTFolder & ""
    End If
    Next
    MsgBox intCount & " files were moved."

    Set oFSO = Nothing
    Set oSFolder = Nothing</pre>


  5. #5
    Platinum Lounger
    Join Date
    Jan 2001
    Location
    Quedgeley, Gloucester, England
    Posts
    5,333
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Batch File to move files without overwriting (

    Chris

    Although I never imagined filenames would start with a ( I tried some and didn't find a problem. Could you give an example?

    I would also put the "target directory" in double-quotes if it has blanks in it!

    John
    <font face="Script MT Bold"><font color=blue><big><big>John</big></big></font color=blue></font face=script>

    Ita, esto, quidcumque...

  6. #6
    5 Star Lounger
    Join Date
    Jul 2002
    Location
    Hatsukaichi, Hiroshima, Japan
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Batch File to move files without overwriting (

    Thanks John, the lack of double quotes around the target directory was the problem. After I changed that it now works perfectly.

    Best wishes,

    Chris

  7. #7
    5 Star Lounger
    Join Date
    Jul 2002
    Location
    Hatsukaichi, Hiroshima, Japan
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Batch File to move files without overwriting (

    Thanks for making the VBscript, it is too much for my present task but I'll bear it in mind for other projects.

    Best wishes,

    Chris

  8. #8
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: Batch File to move files without overwriting (

    No problem. I'd assumed from your previous post that the parentheses were causing the same problem that ampersands caused in your earlier project. It sounds like the files you're dealing with in your latest project probably don't have ampersands (or other DOS-allergic characters) in their names.

  9. #9
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: Batch File to move files without overwriting (

    Just FYI (you may have discovered this already), and contrary to my previous post (and the batch file in your earlier thread), John's batch line for moving files seems to have no difficulty with ampersands.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •