0

I did not write most of the following code. In fact I just modified it a bit from the following post Stack Overflow answer, pretty impressive stuff.

https://stackoverflow.com/a/21727860/2579968

The problem is when a file name contains a symbol (e.g. !), rather than a letter/number.

The idea is for the code to output the current directory folder name and size, followed by the subfolders.

Folder 1 - 151.3 MB
   SubFolder 1 - 151.3 MB
Folder 2 - 10.3 MB
   SubFolder 1 - 5.3 MB
   SubFolder 2 - 5.0 MB

I have a feeling that it is this nasty, but of code causing the problem.

/f "tokens=3,5"

I understand that with in the loop the 3rd and 5th item to be processed. So perhaps when it is seeing the !, it thinks it's another item.

;@echo off

echo("" >Current.txt

    setlocal disabledelayedexpansion

    set "folder=%~1"

    if not defined folder set "folder=%cd%"

    for /d %%a in ("%folder%\*") do (
        set "size1=0"
        for /f "tokens=3,5" %%b in ('dir /-c /a /w /s "%%~fa\*" 2^>nul ^| findstr /b /c:"  "') do if "%%~c"=="" set "size1=%%~b"
        setlocal enabledelayedexpansion
        call :GetUnit !size1! unit1
        call :ConvertBytes !size1! !unit1! newsize1

        for /d %%h in ("%folder%\%%~nxa\*") do (

            setlocal enabledelayedexpansion
            for /f "tokens=3,5" %%b in ('dir /-c /a /w /s "%%~fh\*" 2^>nul ^| findstr /b /c:"  "') do if "%%~c"=="" set "size2=%%~b"
                setlocal enabledelayedexpansion
                call :GetUnit !size2! unit2
                call :ConvertBytes !size2! !unit2! newsize2

                set /p VAR1=<Current.txt

               if not !VAR1! EQU %%~nxa (
                   echo(%%~nxa - !newsize1! !unit1! >>List.txt
                   echo(%%~nxa - !newsize1! !unit1!

               )

               echo(   %%~nxh - !newsize2! !unit2! >>List.txt
               echo(   %%~nxh - !newsize2! !unit2!
               echo(%%~nxa>Current.txt

               endlocal
            )

    endlocal
)

endlocal
exit /b

:ConvertBytes bytes unit ret
setlocal
if "%~2" EQU "KB" set val=/1024
if "%~2" EQU "MB" set val=/1024/1024
if "%~2" EQU "GB" set val=/1024/1024/1024
if "%~2" EQU "TB" set val=/1024/1024/1024/1024
> %temp%\tmp.vbs echo wsh.echo FormatNumber(eval(%~1%val%),1)
for /f "delims=" %%a in (
  'cscript //nologo %temp%\tmp.vbs'
) do endlocal & set %~3=%%a
del %temp%\tmp.vbs
exit /b


:GetUnit bytes return
set byt=00000000000%1X
set TB=000000000001099511627776X
if %1 LEQ 1024 set "unit=Bytes"
if %1 GTR 1024   set "unit=KB"
if %1 GTR 1048576  set "unit=MB"
if %1 GTR 1073741824  set "unit=GB"
if %byt:~-14% GTR %TB:~-14% set "unit=TB"
endlocal & set %~2=%unit%
exit /b

Any pointers would be much appreciated!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Phil
  • 1
  • 1

1 Answers1

0

With delayed expansion in force ! becomes a poison character in text being parsed.

There are workarounds by disabling and then re-enabling it before and after certain points, but you might find other solutions that don't rely on it.

foxidrive
  • 40,353
  • 10
  • 53
  • 68