MS-DOS Batch File ???

Discussion in 'OT Technology' started by deusexaethera, Jul 29, 2005.

  1. deusexaethera

    deusexaethera OT Supporter

    Joined:
    Jan 27, 2005
    Messages:
    19,712
    Likes Received:
    0
    I'm writing a batch file to synch my music player with the music on my computer. I have an XCOPY command to update all the files that were added/changed on my computer, now I need something that will go through the contents of the music player and delete anything that doesn't exist on the computer.

    In an earlier thread, someone suggested a loop. Now that I think about it, I'll need two loops: one for deleting files, a second for deleting empty folders. Problem is, I've never written a loop in a batch file before.

    Is there a generous, properly-educated soul among you who could show me a "basic" example of a loop that can trace through a folder tree and its contents? Once I get that, I can handle the rest.
     
  2. huntz0r

    huntz0r New Member

    Joined:
    Apr 18, 2005
    Messages:
    15,951
    Likes Received:
    0
    Location:
    Charlotte, NC
    I don't think there is any way to do a loop in a batch file, being it's just a series of DOS-mode commands, but I could be wrong.
     
  3. Keyzs

    Keyzs OT Supporter

    Joined:
    Nov 3, 2003
    Messages:
    814
    Likes Received:
    0
    Location:
    Charlotte, MI
    Code:
     :LOOP
           TYPE %1 | MORE
           SHIFT
           [url="http://www.cs.ntu.edu.au/homepages/bea/home/subjects/ith305/description.html#if"]IF[/url] "%1" == "" [url="http://www.cs.ntu.edu.au/homepages/bea/home/subjects/ith305/description.html#goto"]GOTO[/url] END
           GOTO LOOP
           :END
     
     
  4. huntz0r

    huntz0r New Member

    Joined:
    Apr 18, 2005
    Messages:
    15,951
    Likes Received:
    0
    Location:
    Charlotte, NC
    :doh:

    Interesting thing to know anyway.
     
  5. Keyzs

    Keyzs OT Supporter

    Joined:
    Nov 3, 2003
    Messages:
    814
    Likes Received:
    0
    Location:
    Charlotte, MI
    The goal would be much easier to achieve with VB or similar.
     
  6. 5Gen_Prelude

    5Gen_Prelude There might not be an "I" in the word "Team", but

    Joined:
    Mar 14, 2000
    Messages:
    14,519
    Likes Received:
    1
    Location:
    Vancouver, BC, CANADA
    for /r "c:\my music" %%i in (*.mp3) do if not exist "e:%%~pi%%~ni%%~xi" del "%%i"

    Both drives must use the identical path name.
     
  7. deusexaethera

    deusexaethera OT Supporter

    Joined:
    Jan 27, 2005
    Messages:
    19,712
    Likes Received:
    0
    Holy syntax! (cough)

    1. Is this a straight MS-DOS command or does it need compiling?

    2. The drives don't have identical pathnames. How can I modify this to specify different absolute paths? (relative paths within the base folders are identical)

    3. can you explain the syntax in this command? I don't even know where to start dissecting and understanding it.

    Thanks a bunch.
     
  8. 5Gen_Prelude

    5Gen_Prelude There might not be an "I" in the word "Team", but

    Joined:
    Mar 14, 2000
    Messages:
    14,519
    Likes Received:
    1
    Location:
    Vancouver, BC, CANADA
    It's an MS-DOS command. The FOR command is probably the most powerful DOS command there is but a lot of people don't know how to use it, or why you would use it.

    The /f goes thru the directory structure starting with whatever you have as the root (c:\my music), the (*.mp3) pics out the types of files to act on, the do well that's what you want to do for each file, if not exist checks if a file exists, the seamingly random characters after that are the drive, path name, filename and extension, and of course the command after that deletes the file if it can't find it.

    You could add an add an addtional subfolder between the drive letter and the rest of it, but not the other way round:

    e:\bunchofsubfolders\andanother\andanother%%~pi%%~ni%%~xi

    But the %%~pi%%~ni%%~xi portion will be the full path name of the file you're searching for, minus the drive letter: \my music\some.mp3
     
  9. Aimless

    Aimless Resident drunkey

    Joined:
    Nov 5, 2001
    Messages:
    2,534
    Likes Received:
    0
    Location:
    Wisconsin
    You could just use robocopy (xcopy with more functionality) from the Windows Resource Kit. It has a /MIR switch that does what you want.
     
  10. 5Gen_Prelude

    5Gen_Prelude There might not be an "I" in the word "Team", but

    Joined:
    Mar 14, 2000
    Messages:
    14,519
    Likes Received:
    1
    Location:
    Vancouver, BC, CANADA
    There ya go
     
  11. deusexaethera

    deusexaethera OT Supporter

    Joined:
    Jan 27, 2005
    Messages:
    19,712
    Likes Received:
    0
    Pity. I could have used this info years ago.

    Okay, that clears it up somewhat. Damned if I can figure out what possessed the codewriters to pick such arcane combinations of symbols when they could have used words instead. (shrugs)

    Yeah, I did manage to figure that out. Though, I am curious; is the command smart enough to remember the location of the file it's deleting, or will I have to include that whole %%~pi... thing again? I expect not, but I wouldn't put it past a programmer not to cheap out on back-end labor.

    Excellent, so I CAN specify different root directories for the search-and-compare to operate on. Though, when you say that the symbolic crap is the FULL path name of the file, does that mean it will include an "extra copy" of the hard-coded root directory that I typed in? Because that would obviously be a pain in the ass.
     
  12. 5Gen_Prelude

    5Gen_Prelude There might not be an "I" in the word "Team", but

    Joined:
    Mar 14, 2000
    Messages:
    14,519
    Likes Received:
    1
    Location:
    Vancouver, BC, CANADA
    Honestly, I'd go with Penguins idea, but if you're curious to know how things can work, read on...

    %%i just the full path with drive letter. What the ~? character does is identify a specific portion of the filename, in this case to take into account subfolders, I needed ALL of it except for the drive letter.

    In your case, if your "master" directory is on the c drive and your "clone" is on the e drive, it's actually the opposite that I wrote out. You want to run thru every file on the e drive, compare it with everything on the c drive, and if not found, erase the e drive. So if on your c drive, you have your mp3's buried in a subdirectory like this:

    c:\optionalfolder\optionalfolder2\stuff\stuff2\mp3s

    your e drive would have to have this as your directory structure:

    e:\stuff\stuff2\mp3s

    and your command would be:

    for /r "e:\stuff\stuff2\mp3s" %%i in (*.mp3) do if not exist "c:\optionalfolder\optionalfolder2%%~pi%%~ni%%~xi" del "%%i"

    So yes, if you start burying your mp3's, it can get pretty stupid pretty quick.

    To answer your question about if it knows which file it's on - that's what the variable %%i is, the full path name of the file it's on in the loop

    BTW, the for command can also parse text files too - it's really cool for what it does and of course since it's a DOS command, it's compatible regardless of what Windows version you're running.
     
  13. deusexaethera

    deusexaethera OT Supporter

    Joined:
    Jan 27, 2005
    Messages:
    19,712
    Likes Received:
    0
    Huzzah! It works! Here's the code:

    @ xcopy "C:\Documents and Settings\Deus Ex Aethera\My Documents\My Music\Music\*.*" "E:\Music" /c /d /s /w /y
    @ for /r "e:\music" %%i in (*.*) do if not exist "c:\documents and settings\deus ex aethera\my documents\my music%%~pi%%~ni%%~xi" del "%%i" /p
    @ for /f /r "e:\music" %%i in (*) do if not exist "c:\documents and settings\deus ex aethera\my documents\my music%%~pi%%~ni%%~xi" del "%%i" /p
    @ pause

    Obviously the directories would need to be changed for it to work for anyone else, but just for reference it will copy/update any new files from the source to the destination and then delete any files and folders at the destination that no longer exist at the source. It copies automatically, but it prompts before deleting. Chop off the "/p" to fix that.

    Handy when you rename files and you need the old unrenamed copies to get wiped. Thanks for all your help, 5Gen.
     

Share This Page