PDA

View Full Version : convert this batch file into an applescript?




Icefire817
Jan 14, 2012, 01:03 AM
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....



jiminaus
Jan 14, 2012, 01:22 AM
I don't know about doing this in AppleScript, but this is an easy shell script to write.


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?")

Icefire817
Jan 14, 2012, 02:18 PM
Thanks! I'll see if I can get this shell script to work in terminal then.

Icefire817
Jan 14, 2012, 02:47 PM
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.

jiminaus
Jan 14, 2012, 03:24 PM
Well, the shell script itself works but it keeps telling me that no such file or directory exists.

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?

Icefire817
Jan 15, 2012, 11:35 PM
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)

jiminaus
Jan 16, 2012, 12:39 AM
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)

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.


cd ~/Movies

Icefire817
Jan 16, 2012, 08:03 PM
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:

rm: texturesclutterbriefcasedetonatorbriefcasedetonator01_n.dds\r: No such file or directory

When it should be:

rm: textures/clutter/briefcasedetonator/briefcasedetonator01_n.dds/

jiminaus
Jan 16, 2012, 08:10 PM
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?

subsonix
Jan 16, 2012, 08:13 PM
rm: texturesclutterbriefcasedetonatorbriefcasedetonator01_n.dds\r: No such file or directory


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:


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


Applies to both rm lines of course.

Icefire817
Jan 17, 2012, 12:50 AM
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:
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.

subsonix
Jan 17, 2012, 01:05 AM
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.

Of course I do, why else would I suggest sed? 1 file or ten million doesn't matter since it's piped into xargs.


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.

Well there's your problem. You see those are windows file paths (back slash) not the forward slash (/) used in OS X and unix.

jiminaus
Jan 17, 2012, 01:11 AM
Going on from subsonix, you can also use sed to replace all those Windows backslashes with Unix forwardslashes.


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.

cat dupefilelist.txt | tr -d '\r' | tr '\\' '/' | xargs rm

Icefire817
Jan 17, 2012, 03:58 AM
Well now i'm just a little lost. All I need to do is put in

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

or
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. 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:
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?

jiminaus
Jan 17, 2012, 04:02 AM
Those extra codes are fixing your files on-the-fly.

And don't forget to replicate them on the second line as well.

subsonix
Jan 17, 2012, 04:35 AM
Example:
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?

Are you in the directory where those files are located? Try:


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:

pwd

Icefire817
Jan 17, 2012, 02:14 PM
Subsonix, I'm not quite sure I understand. Here is what I'm inputting into terminal.


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: /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.

sero
Jan 17, 2012, 02:54 PM
Your cd command is wrong. The tilde means "/Users/lespeterson" (in your case), so your cd command is saying go to

/Users/lespeterson/Users/lespeterson/Desktop/RFCW_Temp

Try this

cd ~/Desktop/RFCW_Temp

cat dupefilelist.txt | tr -d '\r' | tr '\\' '/' | xargs rm
cat dupefolderlist.txt | tr -d '\r' | tr '\\' '/' | xargs rm -rf

Icefire817
Jan 17, 2012, 08:18 PM
There! That finally got it. Thanks guys! Appreciate all the help! :D