views:

81

answers:

5

Hi,

I have a bash shell script on my Mac, that I need to convert to a .bat file (at least, that's what I've been told) for users that are on PCs. I was wondering the best way to go about this, or if anyone knows of any good references. I don't seem to be asking Google the right question to point me in the right direction.

Specifically, things like how would I do a...

cd ~/Documents/DropFolder

(...where ~/ equals the root of the user's home directory, regardless of the user's name)?

Or, when working on variables and "do" statements...

for i in *.xml
do
  java -Xss650K -Xms128m -Xmx2048m -jar /Applications...
done

And finally, identifying and using basenames...

  cp -p `basename $i .xml`.xml ~/Documents/ReadyForServer/`basename $i .xml`/

Thanks for any guidance, or suggestions for other solutions. LO

A: 

Take a look at powershell. This site has many examples:

http://technet.microsoft.com/en-us/scriptcenter/powershell.aspx

buckbova
+2  A: 

The windows shell is a different scripting language than the bash shell. You will need to port it. Alternatively, you could use something like cygwin or mingw to run the bash shell on windows.

Maz
The first sentence is kinda obvious, actually. The second one is – while potentially helpful – way overkill for what they want here ...
Joey
A: 

F1 help on windows is surprisingly useful when writing DOS style batch files. From the windows desktop, hit F1 and search for batch. The link for Using Batch Parameters is pretty helpful for doing odd tricks with DOS. The 'Using Batch Files' page is also quite good. As other posters mention, you're better off avoiding batch as your implementation language, but if you must, you must.

tonyj
A: 

You should convert by hand. Learn batch commands, then do the equivalent. Alternatively, you can use GNU win32 unix tools.

ghostdog74
+4  A: 

Actually, the things you mention are trivial to port to a Windows batch file. While you certainly can use Windows ports of all Unix tools (or even use an emulation layer for even more fun) this is not hard to do:

  1. ~ for the user's home folder

    The user's profile resides in the environment variable %USERPROFILE%, so the following should do it:

    cd %USERPROFILE%\Documents\DropFolder
    
  2. Iterating over a set of files

    The for command is helpful here:

    for %%i in (*.xml) do -Xss650K -Xms128m -Xmx2048m -jar ... %%i
    

    Obviously you need to adapt the path to the JAR file, though.

    And for has many more uses beyond this one, so take a look at help for as well.

  3. basename

    You need to do this either in a subroutine or a for loop, as the following syntax is specific to loop variables or parameters. It won't work with environment variables as is. You can get what basename is giving you by using %%~ni where %%i if the loop variable or %~n1 if %1 is the argument to a subroutine or batch file you have. So the following would probably do the same:

    copy "%%~ni.xml" "%USERPROFILE%\Documents\ReadyForServer\%%~ni\"
    

    The help on for has more information over those things near the end.

Joey