convert this batch file into an applescript?

Discussion in 'Mac Programming' started by Icefire817, Jan 13, 2012.

  1. macrumors newbie

    Joined:
    Mar 26, 2011
    Location:
    WA
    #1
    Hey, since you guys seem to know infinitely more about scripting than I do, could someone help me convert this batch file into an applescript?

    for /F "tokens=*" %%a in (dupefilelist.txt) DO DEL "%%a"
    for /F "tokens=*" %%a in (dupefolderlist.txt) DO rmdir "%%a" /S /Q

    DEL dupefilelist.txt
    DEL dupefolderlist.txt

    Attrib -r
    DEL %0

    I felt that it went along w/ this thread....
     
  2. jiminaus, Jan 13, 2012
    Last edited: Jan 14, 2012

    macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #2
    I don't know about doing this in AppleScript, but this is an easy shell script to write.

    Code:
    cat dupefilelist.txt | xargs rm
    cat dupefolderlist.txt | xargs rm -rf
    
    rm dupefilelist.txt
    rm dupefodlerlist.txt
    
    chmod 755 $0
    rm $0
    
    (BTW Dup is short for duplicate. Dupe means "to trick or deceived". For example, "Did this guy dupe me into doing his work for him?")
     
  3. thread starter macrumors newbie

    Joined:
    Mar 26, 2011
    Location:
    WA
    #3
    Thanks! I'll see if I can get this shell script to work in terminal then.
     
  4. thread starter macrumors newbie

    Joined:
    Mar 26, 2011
    Location:
    WA
    #4
    Well, the shell script itself works but it keeps telling me that no such file or directory exists. I had to change the script to incorporate directories of the two duplicate list files but now i just don't know where to go from here.
     
  5. macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #5
    Is that because you don't want have both dupefilelist.txt and dupefolderlist.txt, or is it because these lists include files/folders that don't exist?
     
  6. thread starter macrumors newbie

    Joined:
    Mar 26, 2011
    Location:
    WA
    #6
    both exist, but it doesn't seem to recognize that they do or it doesn't recognize the file path (which would take more time than I have to change)
     
  7. macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #7
    This script (as does the batch file) assumes dupefilelist.txt and dupefolderlist.txt are in the current working directory.

    Note that there is an important difference between Windows and Mac OS X with regards to starting a shell script by double-clicking its icon. Under Windows, the current working directory will be set to the directory of the script. Under Mac OS X, the current working directory is your home directory.

    To be safe, put a cd command at the top of the script.

    For example, if the *.txt files and the files and folders to be deleted where all in ~/Movies, then put this cd command the start of the script.

    Code:
    cd ~/Movies
    
     
  8. thread starter macrumors newbie

    Joined:
    Mar 26, 2011
    Location:
    WA
    #8
    I think it just doesn't recognize the file path even with the "cd ~/" because when in terminal it will group up folders that should be separate ones. for example:

    Code:
    rm: texturesclutterbriefcasedetonatorbriefcasedetonator01_n.dds\r: No such file or directory
    When it should be:

    Code:
    rm: textures/clutter/briefcasedetonator/briefcasedetonator01_n.dds/
     
  9. macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #9
    The trailing \r is because the file as DOS line endings (\r\n) instead of UNIX line endings (\n).

    Can you post a sample of these two files?
     
  10. subsonix, Jan 16, 2012
    Last edited: Jan 16, 2012

    macrumors 68040

    Joined:
    Feb 2, 2008
    #10
    What you see at the end there is half of the new line sequence "\r\n" used on Windows, on unix it's '\n' only, so '\r' is interpreted as being part of the filename. You need to strip these of from the names.

    Edit:

    You can easily add this to what jiminaus posted earlier btw:

    Code:
    cat dupefilelist.txt | sed 's/\\r//g' | xargs rm
    
    Applies to both rm lines of course.
     
  11. thread starter macrumors newbie

    Joined:
    Mar 26, 2011
    Location:
    WA
    #11
    Well, I added "| sed 's/\\r//g' |" in, but it still does the same thing. Also, i'm not quite sure you got that what I input above was all in terminal. The actual dupefilelist.txt has over a thousand files. A few examples are:
    Code:
    meshes\ammo\10mmammo.nif
    meshes\ammo\308ammo.nif
    meshes\ammo\44magnumammo.nif
    meshes\ammo\5mmammo.nif
    meshes\ammo\alienpowercell.nif
    meshes\ammo\eccell.nif
    The above path's are exactly like they are found in the file.
    So, again, I'm kinda lost. I still think its just that terminal isn't recognizing the file path.
     
  12. macrumors 68040

    Joined:
    Feb 2, 2008
    #12
    Of course I do, why else would I suggest sed? 1 file or ten million doesn't matter since it's piped into xargs.

    Well there's your problem. You see those are windows file paths (back slash) not the forward slash (/) used in OS X and unix.
     
  13. jiminaus, Jan 16, 2012
    Last edited: Jan 16, 2012

    macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #13
    Going on from subsonix, you can also use sed to replace all those Windows backslashes with Unix forwardslashes.

    Code:
    cat dupefilelist.txt | sed 's/\\r//g' | sed 's/\\/\//g' | xargs rm
    

    Edit: If those sed's are scaring you, you can do that same with tr in a more readable fashion.
    Code:
    cat dupefilelist.txt | tr -d '\r' | tr '\\' '/' | xargs rm
    
     
  14. Icefire817, Jan 17, 2012
    Last edited: Jan 17, 2012

    thread starter macrumors newbie

    Joined:
    Mar 26, 2011
    Location:
    WA
    #14
    Well now i'm just a little lost. All I need to do is put in

    Code:
    cat dupefilelist.txt | sed 's/\\r//g' | sed 's/\\/\//g' | xargs rm
    or
    Code:
    cat dupefilelist.txt | tr -d '\r' | tr '\\' '/' | xargs rm
    In with the rest of my command and it should make all of it work? Or do I have to go back and change every single slash in my file.

    Edit: Oh, got it now.
    Code:
    cat dupefilelist.txt | tr -d '\r' | tr '\\' '/' | xargs rm
    Worked. it changed all the file path's, but now it still doesn't recognize that the files exist.

    Example:
    Code:
    rm: meshes/ammo/10mmammo.nif: No such file or directory
    rm: meshes/ammo/308ammo.nif: No such file or directory
    rm: meshes/ammo/44magnumammo.nif: No such file or directory
    rm: meshes/ammo/5mmammo.nif: No such file or directory
    So, now I am lost again. Some more advice please?
     
  15. macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #15
    Those extra codes are fixing your files on-the-fly.

    And don't forget to replicate them on the second line as well.
     
  16. subsonix, Jan 17, 2012
    Last edited: Jan 17, 2012

    macrumors 68040

    Joined:
    Feb 2, 2008
    #16
    Are you in the directory where those files are located? Try:

    Code:
    ls meshes
    
    If you get "No such file or directory" then you are in the wrong directory or those files does not exist.

    Oh btw, you can see your current directory with:

    Code:
    pwd
     
  17. Icefire817, Jan 17, 2012
    Last edited: Jan 17, 2012

    thread starter macrumors newbie

    Joined:
    Mar 26, 2011
    Location:
    WA
    #17
    Subsonix, I'm not quite sure I understand. Here is what I'm inputting into terminal.


    Code:
    cd ~/Users/lespeterson/Desktop/RFCW_Temp
    
    cat /Users/lespeterson/Desktop/RFCW_Temp/dupefilelist.txt | tr -d '\r' | tr '\\' '/' | xargs rm
    cat /Users/lespeterson/Desktop/RFCW_Temp/dupefolderlist.txt | tr -d '\r' | tr '\\' '/' | xargs rm -rf
    
    chmod 755 $0
    
    The dupefilelist.txt and the Meshes file is in my RFCW_Temp file on my desktop. So, is it that i'm not putting in the right file path at the beginning? Also, when I input pwd into terminal, it tells me that I'm just in:
    Code:
    /Users/lespeterson
    Thanks for being patient with me guys!

    Edit: So, I figured I could just move all of my files into my users and that might work then, but to no avail. Same thing came up, even though everything was in my home directory.
     
  18. macrumors member

    Joined:
    Aug 28, 2008
    #18
    Your cd command is wrong. The tilde means "/Users/lespeterson" (in your case), so your cd command is saying go to

    Code:
    /Users/lespeterson/Users/lespeterson/Desktop/RFCW_Temp
    Try this

    Code:
    cd ~/Desktop/RFCW_Temp
    
    cat dupefilelist.txt | tr -d '\r' | tr '\\' '/' | xargs rm
    cat dupefolderlist.txt | tr -d '\r' | tr '\\' '/' | xargs rm -rf
     
  19. thread starter macrumors newbie

    Joined:
    Mar 26, 2011
    Location:
    WA
    #19
    Finally!

    There! That finally got it. Thanks guys! Appreciate all the help! :D
     

Share This Page