1

I have a batch file that I'm configuring to check if the user whose logged in is a specific user or not. No matter what I do the batch file always crashes on the ) ELSE ( line. I've spent five hours on this and I've definitely shrunk and improved the commands over what they were, but it's still not working. I'd appreciate any suggestions on how I can get it to work (also, if I'm doing plainly stupid, let me know, I'm kind of a dummy when it comes to batch files):

@ECHO OFF
::
SETLOCAL
::
IF "%USERNAME%" EQU "jdoe" (
    IMDISK -a -s 32M -m #: -p "/FS:exFAT /Q /Y /V:MERGEABLES"
    FOR /F "tokens=2 delims==" %%D IN ('WMIC LOGICALDISK WHERE "size=33538048" GET Name /format:value'^) DO (
        COPY "\\WIN2K8R2-F-01\Remote Applications\Batches\Merge PDFs.lnk" "%%D\Merge PDFs.lnk"
        GOTO End
    ^)
) ELSE (
    IMDISK -a -s 256M -m #: -p "/FS:exFAT /Q /Y /V:RAMDRIVE"
    FOR /F "tokens=2 delims==" %%D IN ('WMIC LOGICALDISK WHERE "size=268304384" GET Name /format:value'^) DO (
        COPY "\\WIN2K8R2-F-01\Remote Applications\Batches\Archive Folders.lnk" "%%D\Archive Folders.lnk"
        GOTO End
    ^)
)
::
:End
::
IF EXIST "%SystemDrive%\BGInfo.exe" (
    "%SystemDrive%\BGInfo.exe" "%SystemDrive%\BGInfo.bgi" /Timer:0 /AcceptEula /Silent
)
::
ENDLOCAL

UPDATE

So, I rewrote my version and honestly I don't see much difference between my original and the rewrite, but the rewrite worked. However, in the end I can't use this script because of one issue courtesy of Microsoft. The issue is that normal users are not allowed to format drives. That throws IMDISK out the window and now I'll have to mess with hidden folders and shortcuts, etc. RUNAS doesn't help and I couldn't get it to work anyway. I'm pretty sure I messed it up, but I don't see the point in pursuing a fix anymore. For anyone who cares, here's the final version of the script. Maybe it will help someone else:

@ECHO OFF
::
IF /I "%USERNAME%" EQU "jdoe" (
    IMDISK -a -s 32M -m #: -p "/FS:exFAT /Q /Y /V:MERGEABLES"
::
    TIMEOUT 4
::
    FOR /F "tokens=2 delims==" %%D IN (
        'WMIC LOGICALDISK WHERE "size=33423360" GET Name /format:value'
    ) DO (
        PUSHD %%D
::
        COPY "\\WIN2K8R2-F-01\Remote Applications\Batches\Merge PDFs.lnk" "Merge PDFs.lnk"
::
        POPD
::
        GOTO End
    )
) ELSE (
    IMDISK -a -s 256M -m #: -p "/FS:exFAT /Q /Y /V:RAMDRIVE"
::
    TIMEOUT 4
::
    FOR /F "tokens=2 delims==" %%D IN (
        'WMIC LOGICALDISK WHERE "size=268304384" GET Name /format:value'
    ) DO (
        PUSHD %%D
::
        COPY "\\WIN2K8R2-F-01\Remote Applications\Batches\Archive Folders.lnk" "Archive Folders.lnk"
::
        POPD
::
        GOTO End
    )
)
::
:End
::
IF EXIST "%SystemDrive%\BGInfo.exe" (
    "%SystemDrive%\BGInfo.exe" "%SystemDrive%\BGInfo.bgi" /Timer:0 /AcceptEula /Silent
)

One thing I couldn't figure out is how to pass the %%D variable from the loop directly to the COPY command. That's why I'm using PUSHD and POPD, but that just feels dirty...

2
  • You could try putting SETLOCAL ENABLEDELAYEDEXPANSION instead of just SETLOCAL. I don't know if it will work though. Commented Mar 6, 2013 at 5:21
  • I tried that as well, but it didn't make a difference so I removed it. Commented Mar 6, 2013 at 16:00

2 Answers 2

4

I'd remove all the carets before the close-parentheses if I were you.

The caret escapes the special meaning of the next character - it means 'this charater literally'

BTW - you can format

for /f ... in ('whatever...') do (

as

for /f ... in (
  'whatever...'
 ) do (

if you wish - might make it a little more readable...

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

1 Comment

I took your advice and split the FOR into a couple of lines. Definitely more legible.
1

Your IF statement appears to be correct. I tried it out (on Windows 7) with the IMDISK and FOR loop commented out and it worked just fine. Here is the batch I ran.

@ECHO OFF
::
SETLOCAL
::
IF "%USERNAME%" EQU "jdoe" (
    ECHO jdoe User Found
    REM IMDISK -a -s 32M -m #: -p "/FS:exFAT /Q /Y /V:MERGEABLES"
    REM FOR /F "tokens=2 delims==" %%D IN ('WMIC LOGICALDISK WHERE "size=33538048" GET Name /format:value'^) DO (
    REM     COPY "\\WIN2K8R2-F-01\Remote Applications\Batches\Merge PDFs.lnk" "%%D\Merge PDFs.lnk"
    REM     GOTO End
    REM ^)
) ELSE (
    ECHO User Not Found
    REM IMDISK -a -s 256M -m #: -p "/FS:exFAT /Q /Y /V:RAMDRIVE"
    REM FOR /F "tokens=2 delims==" %%D IN ('WMIC LOGICALDISK WHERE "size=268304384" GET Name /format:value'^) DO (
    REM     COPY "\\WIN2K8R2-F-01\Remote Applications\Batches\Archive Folders.lnk" "%%D\Archive Folders.lnk"
    REM     GOTO End
    REM ^)
)
::
:End
::
IF EXIST "%SystemDrive%\BGInfo.exe" (
    REM "%SystemDrive%\BGInfo.exe" "%SystemDrive%\BGInfo.bgi" /Timer:0 /AcceptEula /Silent
)
::
ENDLOCAL

Your IMDISK call seems pretty standard. Can't see anything wrong there but I did not have the ability to test it.

I would take a closer look at the FOR loop. In my experience, FOR loops are sensitive when you nest them in other blocks. I often get all kinds of weird results. Try pulling the FOR loop into a separate batch file to test it and work out any bugs.

If you can get the FOR loop to work in a separate batch file but still not in your IF ELSE statement, try setting up a CALL to a Label that contains the IMDISK and FOR loop statements. For some reason using a CALL statement handles the nesting better.

Here is a reference for using a CALL subroutine: http://www.quepublishing.com/articles/article.aspx?p=1154761&seqNum=11

1 Comment

I rewrote my version and it pretty much looked like yours and it worked, but I discovered other issues that are unfortunately preventing me from going forward with it. I'll update my question about those.

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.