Simple dos Batch Script

Discussion in 'Software' started by cutterdog, Apr 24, 2008.

  1. cutterdog

    cutterdog Private E-2

    I'm fairly certain this can be done (simply), but it's been years since I wrote a batch file, and I'm scared I'll screw things up and end up deleting all the files on my HD

    Here's what I'm trying to accomplish.

    I have a directory of audio files (.mp3), and a program I use writes a plot file for each audio file (it's an .mxm file).

    When you rename an audio file, it will create a new .mxm file, but it leaves behind the old .mxm file. The orphaned .mxm is no longer needed and in fact kinda makes a mess things.

    I'm trying to find a simple batch file (or even a shareware program) that will:

    1. Look at all the files within a directory that have an extension of .mxm

    2. See if, within that directory, there is a matching .mp3 file (where the filename of the .mp3 is the exact same filename of the .mxm file)

    3. If there is a matching .mp3 to the .mxm, then go to the next .mxm file and look for a .mp3, etc ...

    4. If there is NO matching .mp3 to the .mxm, then delete the unneeded .mxm file then move to the next .mxm for a comparison.

    5. Hopefully have the batch file end once all the .mxm files are compared (but if it starts at the top of the directory again and runs in a continuous loop, I can always stop that manually.

    6. I've tried using this code with no luck. I think it's because these are long file names, not DOS 8.3 file names.

    @ echo off
    for %%f in (*.mxm) do if not exist %%~nf.mp3 del %%f

    I certainly don't mind if someone wants to give me some instructions as to how to go about writing such a batch file. I'll be glad to do it. But I figure it will probably be easier and quicker for someone to throw together a quickie batch file and just be done with it.

    Either way, I sure appreciate anyone's help.

    Thanks
     
  2. PC-XT

    PC-XT Master Sergeant

    I've been working with batch files for almost 12 years. (started on a PC-XT :)) I found it easier to do what you are attempting with BASIC, usually QBasic, because the %%f in the batch file's for statement will be the full filename, including the extention. It gets a bit tricky to change the extention. I don't know if you have QBasic. [It can be downloaded from the microsoft website.] I remember working on a utility to chop off the extention of a file, and put it in an environment variable. It was a .com program I was making in debug. I don't remember if I finished, but I could probably rewrite it. I could also write it in C, as well, though I'd have to look up how to handle the DOS environment in C. If you had a utility like that, you could have one batch file [named delmxm in this example] like this:
    Code:
    @echo off
    remext %1
    if not exist %remext%.mp3 del %remext%.mxm
    (remext is the utility)
    Then your batch file could call the previous one like this:
    Code:
    @echo off
    for %%f in (*.mxm) call delmxm %%f
    I've used batch files less in the last few years, so I may have forgotten something, but this seems to be the solution I found simplest. I think I'll probably attempt that utility again anyway. I suppose I could even write it in assembler, if it would compile in C's assembler. Let me know if you want it posted in a zip file, but it'll take a few days since I'm fully loaded with work right now. Maybe over the weekend :confused
     
  3. PC-XT

    PC-XT Master Sergeant

    I forgot to mention Windows alternatives. The Windows Scripting Host uses VBScript with extra objects that let it handle file systems and other system stuff. I think it may go something like:
    Code:
    path = "C:\Media"'[path to directory to check]
    Set fSys = CreateObject("Scripting.FileSystemObject")
    Set dir=fSys.GetFolder(path)
    Set AllFiles=dir.Files
    for each file in AllFiles
     f=file.Name
     if right(f,4)=".mxm" then
      mp3=left(f,len(f)-4)&".mp3"
      If not fSys.FileExists(mp3) Then
       fSys.DeleteFile(f)
      end if
     End If
    next
    Just save it with a .vbs extention, and double-click it. (You will need to have the Windows Scripting Host installed. I think it's available in the Windows component tab of the Add/Remove Programs control. I think it's in accessories.)
     
  4. cutterdog

    cutterdog Private E-2

    VBS File !
    YES !!!

    Worked perfectly.

    MANY MANY Thanks.
     
  5. PC-XT

    PC-XT Master Sergeant

    Great! :) You're welcome. Glad it worked!
     
  6. patrickMc

    patrickMc Private E-2

    Re: Simple dos Batch Script to delete unneeded .mxm files, based on .mp3 files

    cutterdog says:

    Here is script written using biterscripting (http://www.biterscripting.com) . You can use any batch scripting language.
    Code:
    cd "C:/some directory" ; var str l ; lf -n "*.mxm" > $l
    while ($l <> "")
    do
      var str mxmfile, mp3file ; lex "1" $l > $mxmfile
      set $mp3file = {stex -p "]^.mxm^l" $mxmfile}+".mp3"
      var str out ; set $out = "" ; lf $mp3file > $out 2>null
      # If file does not exist, $out will be blank.
      if ($out == "")
        system del ("\""+$mxmfile+"\"") >null 2>null
      endif
    done
    Patrick
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds