batch-filedirectoryspace

Why does my batch code give a syntax error for a folder name with space in it?


This is my first question here and I am not a programmer but an mechanical engineer doing some IT stuff for my company. So my task is to write a batch code to find all PDF files in a directory and check them for their correct names. I get this done with one exeption. The programm is given a directory by the user e.g. c:\users\guy\one space\nospace\ then the programm works as is should. The space in the folder name "one space" is no problem. However, if the given directory would be c:\users\guy\one space\ then the code aborts with something like "cannot be processed syntactically at this point" (translated from my german command promt). So this is my code, maybe someone has a clue about my mistake:

@echo off
setlocal
setlocal enabledelayedexpansion

echo.
echo If the input folder path contains a space in its name, the program won't work
echo.
echo.


REM User input for folderPath and check
:checkFolderPath
set /p folderPath="Please enter the folderpath: "
if not exist "!folderPath!" (
    echo The given path does not exist. Please enter a valid path:
    goto :checkFolderPath
)

REM User input for maxLevel and check
:checkMaxLevel
echo.
set /p maxLevel="Please enter the max search depth for subfolders where !folderPath! represents Level 0: "
echo.
for /f "tokens=* delims=0123456789" %%i in ("%maxLevel%") DO (
    if not "%%i"=="" (
        echo The input has to be a number. Please enter a valid number e.g. 2.
        goto :checkMaxLevel
    )
)

REM initiate counter
if not defined maxLevel set maxLevel=1
set currentLevel=-1
set totalFiles=0
set correctFiles=0
set renameFiles=0
set incorrectFiles=0
set changedFiles=0
set folderCount=0
set tempPath=!folderPath!

REM replace space symbol by "_" and "\" by "\\"
set "tempPath=!tempPath: =_!"
set "tempPath=!tempPath:\=\\!"

REM count number of backslashes in the path (equals folder depth)
set /a "depth=1"

:loop
if defined tempPath (
    set "tempPath=!tempPath:*\\=!"
    if not "!tempPath!"=="!tempPath:\\=!" (
        set /A "depth+=1"
        goto :loop
    )
)

set /a rootLevel=%depth%

echo The absolute folder depth of "%folderPath%" is: %rootLevel%
echo.
echo.

REM Skip loop through folders if given maxLevel is 0
if !maxLevel! == 0 (    
    pushd !folderpath!
    FOR %%R IN (*.pdf) DO (                
        set "filename=%%~nxR"
        echo %%~fR
        set /a totalFiles+=1
        REM echo totalFiles=!totalFiles!
        REM Check if filename beginns with "DRW_"
        if "!filename:~0,4!"=="DRW_" (
            set /a correctFiles+=1
        ) else (
            REM Check if filename beginns with "500"
            if "!filename:~0,3!"=="500" (
                set /a renameFiles+=1
            ) else (
                set /a incorrectFiles+=1
            )
        )
    )
) else (
REM Loop through subfolders up to given maxLevel
FOR /R "%folderPath%" %%D in (.) DO (
    set "depth=-1"
    REM echo now in %%D

    REM set tempPath to variable %%D and remove the . at the end
        set tempPath=%%D
    set "tempPath=!tempPath:.=!"

    REM replace space symbol by "_"
    set "tempPath=!tempPath: =_!"
    REM echo temp_space_replacement=!tempPath!

    REM replace "\" by "\\"
    set "tempPath=!tempPath:\=\\!"          
    REM echo temp_backslash_doubled=!tempPath!

    REM count number of backslashes in the path (equals folder depth)
    REM echo tempPath=!tempPath!

    FOR /L %%K IN (1,1,15) DO (
        if defined tempPath (
            REM echo tempPath=!tempPath!        
            set "tempPath=!tempPath:*\\=!"          
            REM echo D1 =%%D
            if not "!tempPath!"=="!tempPath:\\=!" (
                set /A "depth+=1"
                REM echo xLevel=!depth!
                REM echo D2 =%%D               
            )
        )
    )
    
    set /a currentLevel=!depth!-!rootLevel!

    REM echo calculating relative level
    REM echo.
    REM echo.
    REM echo absolute level=!depth!
    REM echo relative level=!currentLevel!
    REM echo maxLevel=!maxLevel!
    REM echo folderpath=%folderPath%
    REM echo D3 =%%D
    REM echo.
    REM echo.
        
    REM loop through files in the current folder
    if !currentLevel! leq !maxLevel! (
        Pushd %%D
        REM Echo now in %%D
        REM echo proecessing files in this folder...
        REM echo.
        if !currentLevel! leq !maxLevel! (
            FOR %%G IN (*.pdf) DO (                
                set "filename=%%~nxG"
                echo %%~fG
                set /a totalFiles+=1
                REM echo totalFiles=!totalFiles!
                REM Check if filename beginns with "DRW_"
                if "!filename:~0,4!"=="DRW_" (
                    set /a correctFiles+=1
                ) else (
                    REM Check if filename beginns with "500"
                    if "!filename:~0,3!"=="500" (
                        set /a renameFiles+=1
                    ) else (
                        set /a incorrectFiles+=1
                    )
                )
            )
        REM echo.
        ) else (
        echo reached maxLevel for file loop which should not happen    
            )
    ) else (
        REM echo reached maxLevel
    )
    popd
)
)

REM summary of found files
echo.
echo Number of total checked PDF files: %totalFiles%
echo Number of correct named PDF files: %correctFiles%
echo Number of PDF files to be renamed: %renameFiles%
echo Number of other PDF files: %incorrectFiles%


pause

I have no other idea, but maybe someone else has one.


Solution

  • You are using most of the time the set "var=value" way to set variables. But there are a few lines where you have used the set var=value way. This is likely the cause of the problems with the folder with a space and may als cause problems with special characters in file/folder names, for example the & character. Lines to changes are:

    set tempPath=!folderPath!
    set tempPath=%%D
    

    I am not sure if it is necessary for the pushd command, but when using file/foldernames always use double quot's. Better safe than sorry.

    One more remark regarding the use of delayed expansion. If you have folders/files with the ! character these will fail because the enabled delayed expansion will strip the ! character.