0

I am a Linux user forced to work on a Windows workstation at work. I have a script that I wrote for bash, that I need to convert to Windows Batch. In the past, I haven't had too much trouble, but now I have run into two issues I cannot figure out. Here is the original Linux script:

#!/bin/bash
if [ $# != 1 ] ; then
    echo "usage: createMavenDirs.sh <name>"
    exit 1
fi
timestamp=`date +"%Y-%m-%d-%s"`
projectName="${1}_${timestamp}"
groupID="com.hostname"
version="1.0"
pomFill="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<project>\n\t<modelVersion>4.0.0</modelVersion>\n\t<groupId>${groupID}.${1}</groupId>\n\t<artifactId>${1}</artifactId>\n\t<name>\${artifactId}</name>\n\t<version>${version}</version>\n</project>"

clear

mkdir -p ${projectName}/src/{main/{java,resources,filters,assembly,config,webapp},test/{java,resources,filters},site}
touch ${projectName}/{LICENSE,NOTICE,README}.txt
touch ${projectName}/pom.xml
echo -e ${pomFill} > ${projectName}/pom.xml

tree ${projectName}

exit 0

First off, I want to say that "yes", I know there are tools for doing this, but due to VERY tight constraints at work, I have to use an option like this. Second, here are the two problems I am having.

  1. The "mkdir" line: Does Windows have a way to do this like Linux does?

  2. The "echo" line where I create the pom file: Can Windows echo a newline and tab, and redirect it into an external file like I am doing in my Bash script?

Like I said, I know there are other ways to do this, but the powers that be have forced me down this path. Anyway, thank you to all who have taken the time to read.

Have a good day :-)

UPDATE: I realize that Windows does have an "echo" and "mkdir", but I need to be able to use them in a very specific way, just like I have used them in my Linux script. Specifically, I would like to make directories exactly like I have in my Linux script, and I need to echo a newline '\n' or '\r' and a tab '\t' into an external file.

UPDATE 2: I cannot install any new software/utilities/etc. onto my workstation. I must use only what is there.

Brian
  • 1,726
  • 2
  • 24
  • 62
  • http://stackoverflow.com/questions/913912/bash-shell-for-windows - look at the answers and install unixy tools on your windows. cmd shell programming is not fun at all. – Mat Apr 27 '12 at 14:17
  • 2. http://stackoverflow.com/questions/132799/how-can-you-echo-a-newline-in-batch-files – askovpen Apr 27 '12 at 14:23
  • I cannot install any new software/utilities/etc. I must use what has come with my workstation, and nothing else. – Brian Apr 27 '12 at 14:27
  • Brian - point out to your management that if you cannot install anything, then you cannot do your job as a programmer, since you cannot install the programs (of which scripts are an example) which you write. You'll need to have a dialogue about how to decide what is and is not appropriate for installation (ie, licensing, etc). – Chris Stratton Apr 27 '12 at 17:40
  • See if they will let you have (Microsoft) Powershell. It's horrible, but at least it is a real programming language. Since you have Java, if you like Ruby, you can go a long way with JRuby (without "installing" anything in the strict sense). (I would rather script in Ruby than Bash anyway, but this is a personal preference.) Always be careful and make sure you check with your manager before putting your job at risk. – theglauber Apr 27 '12 at 17:45
  • I have tried running PowerShell, and I like it. However, some things we do cannot be done through PowerShell. I'm not sure how to tell you what, because I am not a Windows guy. I was told by a more senior developer (not an old, stuck in the past guy) that PowerShell would not work, that I should use the CMD prompt. I find it "funny" (i.e., irritating) that CMD and PowerShell have different sets of commands even though they are on the same OS. I know Bash, Korn, Tcsh, etc., have different command variations, but still the vast majority are the same. – Brian Apr 27 '12 at 18:07

3 Answers3

2

@brice is right, batch programming on Windows is downright crazy. But I beleive you can still make it work, it will not be a line-to-line equivalent, though.

Things to worry about :

  • The time stamp is locale dependant. I have a French version, please adjust
  • To output tabs you must insert tabs in notepad and leave them there. HTML will eat the tabs in my original script.
  • The dot (.) is mandatory if you echo a tab as your first caracter
  • I didn't find where you get the artifact id

As far as I can test, this batch file does the same thing as your orignal script:

@echo off

setlocal

if "%1"=="" (
    echo usage: createMavenDirs.bat ^<name^>
    goto:eof
)

::BEWARE: %date% and %time% are locale dependant, 
::I get 2012-04-27113505,55 YMMV
set timestamp=%date%%time::=%
set projectName=%1_%timestamp:,=%
set groupID=com.hostname
set version=1.0
set artifactId=What_is_this

::Create root dir
md "%projectName%"

::Make everything relative to %projectName%
pushd "%projectName%"

::Generate the pom file
(
echo.^<?xml version="1.0" encoding="UTF-8"?^>
echo.^<project^>
echo.   ^<modelVersion^>4.0.0^</modelVersion^>
echo.   ^<groupId^>%groupID%.%1^</groupId^>
echo.   ^<artifactId^>%1^</artifactId^>
echo.   ^<name^>\%artifactId%^</name^>
echo.   ^<version^>"%version%"^</version^>
echo.^</project^>
) > pom.xml

::Loop unrolling !
md src
    cd src
    md main
        cd main
        md assembly config filters java resources webapp
        cd ..
    md site
    md test
        cd test
        md filters java resources
        cd ..
    cd ..


::This is touch: copy /b LICENCE.txt +,, 
::but it will not create the file. Use this echo trick instead
echo. 2>LICENCE.txt
echo. 2>NOTICE.txt
echo. 2>README.txt

::Leave %projectName%
popd 

::Display dirs and files
tree /a /f "%projectName%"

endlocal

I never though I would use loop unrolling in a batch script ;)

ixe013
  • 9,559
  • 3
  • 46
  • 77
1

After update:

There is essentially no way to do what you request with batch files.

Batch files are broken. Bite the bullet and use Java. (Or cheat by using Jython...)

Original:

  1. The equivalent command for windows to mkdir is md or mkdir:

    c:\>mkdir \foo\bar
    
    c:\>dir
    [...]   foo  [...]
    
  2. echo is available in batch too:

    c:\>echo some text> Myfile.txt
    
    c:\>type Myfile.txt
    some text
    
    c:\>
    

    Note the gotcha: quotes will end up in your script too. So for example, echo "abcdef" will also print the double quotes.

    As far as I understand, the echo command does not support tab character escape.

You can learn more at this batch reference.

As an aside, I'd recommend just running bash on windows, via Cygwin if you can... If you have Python, that could also be a lifesaver. I feel for you if neither of those options are available...

brice
  • 24,329
  • 7
  • 79
  • 95
  • I cannot install any new software/utilities/etc. I must use what has come with my workstation, and nothing else. Yes, but I need to echo newlines and tabs into "MyFile.txt". The way I use mkdir in Linux is extremely efficient as opposed to mkdir -> chdir -> mkdir -> chdir -> etc ... – Brian Apr 27 '12 at 14:29
  • I don't know what came with your workstation. But if you have VB, you might be better off using that instead of batch... – brice Apr 27 '12 at 14:32
  • Nope ... no VB. At this point, I'm getting ready to write a Java application that will do it for me. Every day I am forced to use Windows, I die a little inside. – Brian Apr 27 '12 at 14:33
  • Java but no Python or Cygwin? Interesting IT policies... Perhaps you can find your solution [elsewhere](http://careers.stackoverflow.com/jobs)? ;-D – brice Apr 27 '12 at 14:37
  • brice - LOL ... aside from the Windows nonsense, this is a good place to work. – Brian Apr 27 '12 at 14:41
-4

You could just run bash on windows, using either cygwin or mingw

Chris Stratton
  • 39,853
  • 6
  • 84
  • 117