Results 1 to 8 of 8
  1. #1
    Star Lounger
    Join Date
    Feb 2005
    Posts
    86
    Thanks
    0
    Thanked 0 Times in 0 Posts

    mv append to dir

    This is a bit of a shot in the dark. I don't know if batch is discussed on the forum. Feel free to refer me to a good batch programming forum.

    A batch question about mv commmand. How do I do something akin to append with files/directories?

    I have a bunch of files and dirs in a temp directory.
    say:
    /2006/temp/20060501/filea
    /2006/temp/20060501/fileb
    /2006/temp/20060502/filea
    etc..

    and my bat file has the commands
    cd /2006/temp/
    mv * ..
    Which works fine, unless there is already
    a /2006/20060501 directory. In which case it fails.

    I want to issue mv such that it appends the files to the directory if it already exists (kind of like what Windows does), without prompting.

    What's the trick? I already tried mv -f * .. but that surprisingly fails too.

    Thanks,
    -Alex

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

    Re: mv append to dir

    I am puzzled by your terminology.

    a) MV does not exist as a standard Windows Command Prompt command, the nearest is MOVE
    [img]/forums/images/smilies/cool.gif[/img] your paths have forward slashes (/) instead of backslashes ()
    c) you talk about appending files to a directory, rather than copying them, or adding them, to a directory

    Is your operating system Microsoft Windows of any variety?

    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
    Star Lounger
    Join Date
    Feb 2005
    Posts
    86
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: mv append to dir

    lol. Yes, you do have a point. I have used unix/linux a lot (which shows in my terminology) but I can assure you that this question pertains to XP.

    a) Yes, mv, was integrated at some point in Command.exe, by analogy with unix. In fact, I assumed that it was simply an alias to Move but to my dismay I just realized it's not! It has different syntax and options. Bizarre. In any case, after reading up on move's syntax, I decided to try "move /y * .." or "move /y 20060501 .." and both failed as well.

    [img]/forums/images/smilies/cool.gif[/img] Yes, I meant the backslashes for the path.

    c) I picked append by analogy with file appending. I certainly mean adding the files to a directory. Specifically, I want to recapitulate by batching the exact behavior of XP gui when one moves a directory x to a location that already has a directory x. After a prompt, it adds all the files from the first directory x to the second directory x. I want to accomplish the same with a batch command but without the prompt (i.e. -force).

    Again, this might be wrong place to ask this question. I already tried another forum, but I don't think I know of a good batching forum (the equivalent of this one for MS Office questions). Can anyone give me a hint or point me somewhere?

    Thanks,
    Alex

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

    Re: mv append to dir

    OK, I will translate your questions into Windows, then!

    MOVE is one of the least user-friendly utilities, and much better is COPY, and even better is XCOPY.

    To determine whether a directory exists already, you can do something like
    md "directoryname"
    if errorlevel 1 (
    echo Directory already existed
    rem ... and you get an error message (which can be suppressed)
    ) ELSE (
    echo Directory was created
    )

    I've read your post several times, and I still can't see what you start with, and what you want to end up with, sorry! Please could you describe what you want to achieve again?

    I suspect you will want to end up with one or more commands of the form:
    XCOPY "C:<sourcedirectorypath>*.*" "C:<targetdirectorypath>" /s
    Try typing, in a Command Prompt window
    XCOPY /?
    and reading the help information it gives.

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

    Ita, esto, quidcumque...

  5. #5
    Star Lounger
    Join Date
    Feb 2005
    Posts
    86
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: mv append to dir

    Ah, it's probably my fault. Let me try again:

    I am sitting in: 2006temp
    this directory contains:
    20060501c.txt
    20060501d.txt
    20060502a.txt
    etc..

    After my batch programs fiddles around with these files, I want to move all of them one directory up (..)
    Thus, from 2006temp to 2006

    and if 2006 already has a directory named 200620060501

    I want the files to be added automatically to the target directory if it already exists (rather than replace it)
    thus, 200620060501 would become populated with a.txt, b.txt, c.txt, etc
    2006temp20060501 would, of course, be deleted.

    This is how the append command (>>) works in file writing and how the XP OS works in gui copying.

    But how to do it in batch? Perhaps I would need to use vba?!

    Incidentally, the directories contains pictures and the directory names are auto-generated from the picture date. (and all pictures from the same day go in the same folder). I don't know if that helps but just so you know that this is part of a -larger- workflow.

    I'll take a look at Xcopy too. Thanks,
    -Alex

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

    Re: mv append to dir

    Here's my attempt called MOVER.BAT<pre>@echo off
    echo +-------+
    echo I MOVER I copies all the files from C:2006temp[whatever] to
    echo +-------+ C:2006[whatever] and deletes the [whatever] directory

    :: point to the directory at which we start
    cd c:2006temp

    :: list each lower-level subdirectory name and process
    for /d %%a in (*) do (
    echo.
    rem each time the subdirectory name is %%a
    rem check XCOPY /? for what is being done
    echo copying files from subdirectory c:2006temp%%a ...
    ECHO xcopy C:2006temp%%a*.* c:2006%%a /y

    echo deleting the subdirectory c:2006temp%%a ...
    ECHO rd /s /q %%a
    )
    :: note that the C:2006temp directory remains, but is empty of subdirs

    :: change back to the root of C:
    cd c:</pre>

    Please test ever so carefully. I suggest you make a copy of the entire C:2006 directory tree before starting!

    If you run it just as it is, it will just ECHO the commands XCOPY and RD to the screen, so you can see which directories are being processed.

    Now edit out the two words ECHO (just the ones in CAPITAL LETTERS) and rerun. It should now do what you want (if I have understood you correctly).

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

    Ita, esto, quidcumque...

  7. #7
    Star Lounger
    Join Date
    Feb 2005
    Posts
    86
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: mv append to dir

    Oh man, you've taken my request for help and just did the whole thing for me! Help beyond my expectations.

    I haven't tested it yet but just from looking at your code I bet it will work or very close to it.

    I'll report back.

    Thanks again,
    -Alex

  8. #8
    Star Lounger
    Join Date
    Feb 2005
    Posts
    86
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: mv append to dir

    Ok. Only had time to test it right now.

    Works like a charm. Thanks a lot. It's too bad it's not as a simple as an option for mv or move, but I've learned to look at xcopy in the future. Thanks a lot

    Should mention that the only thing missing from the script was to wrap the removing of directories in a error check (if NOT ERRORLEVEL 1) to make sure it only deletes the directory if the xcopying goes smoothly.

    Thanks again.
    -Alex

Posting Permissions

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