0

My batch file is silently ending execution on the first loop (for /d "delims= eol=|" %%d in (*.*) do () and I can't tell why.

  • I'm not calling any other batch files
  • My subfolder names DO contain spaces, which I tried to handle with "delims= eol=|"

I never see !parent!!folder! echoed and it doesn't pause at the end. What am I doing wrong?


@Echo off
setlocal enabledelayedexpansion

pushd "%~dp0"

set "parent=%~dp0"
echo !parent!
set "destination=\\(test destination folder)\"

rem SET "destination=\\(prod destination folder)\"


set "FileCount=0"


for /d "delims= eol=|" %%d in (*.*) do (
    set "folder=%%d"
    echo !parent!!folder!
    pause
    for %%f in ("!parent!!folder!\(file identifying-pattern)*.DAT") do (
        echo "File Type 1"
        pause
        if !FileCount! lss 200 ( 
            set /a !FileCount!+=1
            ECHO !FileCount!
            ECHO !parent!!folder!%%f
            ECHO !destination!%%f
            MOVE "%%f" "!destination!"
        )
    (
    for %%f in ("!parent!!folder!\(file identifying-pattern)*.DAT") do (
        echo "File Type 2"
        pause
        if !FileCount! lss 200 ( 
            set /a !FileCount!+=1
            ECHO !FileCount!
            ECHO !parent!!folder!%%f
            ECHO !destination!%%f
            MOVE "%%f" "!destination!"
        )
    )
)


for /d %%d in (*.*) do (
set "folder=%%d"
if not %%d==Archive (
if not %%d==Hold (

dir /b "!folder!"|find /v "">nul && ECHO "!folder! NOT EMPTY"||rmdir "!parent!!folder!"

)
)
)


pause

popd
8
  • Why not For /F instead of For /D? Commented Feb 9, 2018 at 21:18
  • for /d can handle the spaces, no need for delims or eol (in fact, they aren't allowed with /d). Also, no need for *.*, for can handle a simple * for all matches. Might there be an extension or not. Commented Feb 9, 2018 at 21:37
  • ... and you don't see your echo output, because your script stops with a syntax error at the for /d line. For Troubleshooting don't run your script with a doubleclick, but manual from an open command line window. And run it with echo on to see, what exactly happens. Commented Feb 9, 2018 at 21:39
  • @Stephan Unfortunately getting rid of the delims and eol parameters and commenting the echo off line and running from the command line doesn't display any errors, nor does it complete successfully. Commented Feb 9, 2018 at 21:47
  • @Compo Mostly because I was getting the subfolder name and saving it to !folder! there. Do you have an alternative suggestion? Commented Feb 9, 2018 at 21:48

1 Answer 1

0

I've took a guess at what you were doing, trying not to change the structure too much!

You'll need to add your destinations on lines 4 and 5 and your file patterns to lines 8 and 9 as necessary. (Please make sure you do not end your destination paths with back slashes):

@Echo Off
CD /D "%~dp0" 2>Nul || Exit /B

Set "Destination=\\(test destination folder)"
Rem Set "Destination=\\(prod destination folder)"
If Not Exist "%destination%\" Exit /B

Set "Pattern1=(file identifying-pattern)"
Set "Pattern2=(file identifying-pattern)"

Set /A Type1Count=Type2Count=0
SetLocal EnableDelayedExpansion
For /D %%A In (*) Do (
    For %%B In ("%%A\%Pattern1%*.dat") Do (
        Echo "File Type 1"
        Set /A Type1Count +=1
        If !Type1Count! Lss 200 (
            Echo !Type1Count!. "%CD%\%%A\%%B" --^> "%Destination%\%%B"
            If Not Exist "%Destination%\%%B\" MD "%Destination%\%%B"
            Move /Y "%%B" "%Destination%\%%B">Nul
        )
    )
    For %%C In ("%%A\%Pattern2%*.dat") Do (
        Echo "File Type 2"
        Set /A Type2Count +=1
        If !Type2Count! Lss 200 (
            Echo !Type2Count!. "%CD%\%%B\%%C" --^> "%Destination%\%%C"
            If Not Exist "%Destination%\%%C\" MD "%Destination%\%%C"
            Move /Y "%%C" "%Destination%\%%C">Nul
        )
    )
    If /I Not "%%A"=="Archive" (
        If /I Not "%%A"=="Hold" (
            Set "file="
            For %%D In (*) Do Set "file=1"
            If Defined file (Echo %%A NOT EMPTY) Else RD "%%A" 2>Nul
        )
    )
)

Pause

If you're happy with it in your test you can Remark line 4 and unRemark line 5.

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

1 Comment

You did a great job of reading my mind - I didn't realize how unclear I was until I was adapting your answer.

Your Answer

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