0

I'm trying to convert the following unix shell script to windows:

for strWorkerDirectory in $BASEDIR/worker01/applogs $BASEDIR/worker01/filtered   $BASEDIR/worker01/filtered/error-logs $BASEDIR/worker01/filtered/logs $BASEDIR/worker01/filtered/session-logs
do
  if [ ! -d $strWorkerDirectory ]; then
    mkdir -p $strWorkerDirectory
  fi
done

So far, I came up with this:

FOR %%strWorkerDirectory IN (%BASEDIR%worker01\applogs %BASEDIR%worker01\filtered %BASEDIR%worker01\filtered\error-logs %BASEDIR%worker01\filtered\logs %BASEDIR%worker01\filtered\session-logs) DO 
( 
IF exist %%strWorkerDirectory ( echo %%strWorkerDirectory exists ) ELSE ( mkdir %%strWorkerDirectory && echo %%strWorkerDirectory created)
)

but i'm getting an error message which just says something is wrong here

"%strWorkerDirectory" kann syntaktisch an dieser Stelle nicht verarbeitet werden.

What would be the correct conversion here?

1 Answer 1

3

Two issues:

  1. The "loop variable" can only have one character. Try using just %%s for example. Note that depending on the type of the FOR loop the names have a meaning (see FOR /? for more information); for example when used with `FOR /F "tokens=..."``. In your case it shouldn't matter though.
  2. The opening brace must be on the same line as the DO

Complete example:

FOR %%s IN (%BASEDIR%worker01\applogs %BASEDIR%worker01\filtered %BASEDIR%worker01\filtered\error-logs %BASEDIR%worker01\filtered\logs %BASEDIR%worker01\filtered\session-logs) DO (
IF exist %%s ( echo %%s exists ) ELSE ( echo mkdir %%s && echo %%s created)
)

Hint: you can use a caret (^) as line continuation character to avoid overly long lines. Just make sure there is really no other character (not even whitespace) after the caret.

FOR %%s IN (%BASEDIR%worker01\applogs  ^
  %BASEDIR%worker01\filtered ^
  %BASEDIR%worker01\filtered\error-logs ^
  %BASEDIR%worker01\filtered\logs ^
  %BASEDIR%worker01\filtered\session-logs) DO (
    IF exist %%s ( echo %%s exists ) ELSE ( echo mkdir %%s && echo %%s created)
)

EDIT As commenter @dbenham points out, the line continuation inside above are not actually neccessary.

Sign up to request clarification or add additional context in comments.

2 Comments

The caret (line continuation) is not needed within the IN clause. The parentheses of the IN clause allow new lines just fine.
@dbenham Uh, good thanks. I was so keen to illustrate the (often underused) line continuation that I missed that one. ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.