Page 1 of 1

% in file list causing issues with call :command

Posted: 29 Mar 2020 15:51
by stevehero
I'm having an issue with a list of files that contain the % sign.

The % sign of any files are getting eaten up when I do the call :PROCESS_FILES "%%~D"

The contents of the file.txt is the following:

Code: Select all

"Z:\Folder 100%\filename 01.doc"
"Z:\Folder 100%\filename 02.doc"
"Z:\Folder 100%\filename 01.doc"
"Z:\Folder 100%\filename 02.doc"
"Z:\Folder 100%\filename 03.doc"
"Z:\Folder 100%\filename 01.doc"
"Z:\Folder & 100%\filename 01.doc"
"Z:\Folder & 100%\filename 02.doc"
"Z:\Folder & 100%\filename 01.doc"
"Z:\Folder & 100%\filename 02.doc"
"Z:\Folder & 100%\filename 03.doc"
"Z:\Folder & 100%\filename 01.doc"

Code: Select all

@echo off & title Example to go through a list of file and echo, Directory then files... & color 5F & chcp 65001 >NUL

@SetLocal DisableDelayedExpansion
@For %%G In (PDir PPDir)Do @Set "%%G="
@For /F "UseBackDelims=" %%G In ("%Temp%\music-checker-list-2020-03-29_2034-21.txt")Do @(Set "PDir=%%~dpG"
  SetLocal EnableDelayedExpansion
  If /I Not "!PPDir!"=="!PDir!" Echo Directory - "!PDir!"
  EndLocal
  call :PROCESS_FILES "%%~fG"
  rem Echo fn that prints out ok: "%%~fG"
  Set "PPDir=%%~dpG")
@Pause
exit
@EndLocal

:PROCESS_FILES
rem Any files with % in the list are failing.
rem They echo to the buffer without the % sign.
echo "%~f1"
goto :EOF
It outputs this: (Notice the % are missing, which is causing problems when I go to call a command in the PROCESS_FILE section)

Code: Select all

Directory - "Z:\Folder 100%\"
"Z:\Folder 100\filename 01.doc"
"Z:\Folder 100\filename 02.doc"
"Z:\Folder 100\filename 01.doc"
"Z:\Folder 100\filename 02.doc"
"Z:\Folder 100\filename 03.doc"
"Z:\Folder 100\filename 01.doc"
Directory - "Z:\Folder & 100%\"
"Z:\Folder & 100\filename 01.doc"
"Z:\Folder & 100\filename 02.doc"
"Z:\Folder & 100\filename 01.doc"
"Z:\Folder & 100\filename 02.doc"
"Z:\Folder & 100\filename 03.doc"
"Z:\Folder & 100\filename 01.doc"

Re: % in file list causing issues with call :command

Posted: 29 Mar 2020 22:52
by jeb
Hi stevehero,

the problem is the call command, it starts a second round of parsing, therefore the percent is interpreted and eventually removed.

It's better to assign "%%~fG" to a variable, and later, use that variable.

Code: Select all

@For /F "UseBackDelims=" %%G In ("%Temp%\music-checker-list-2020-03-29_2034-21.txt")Do @(Set "PDir=%%~dpG"
  SetLocal EnableDelayedExpansion
  If /I Not "!PPDir!"=="!PDir!" Echo Directory - "!PDir!"
  EndLocal
  set "filename=%%~fG"
  call :PROCESS_FILES
  rem Echo fn that prints out ok: "%%~fG"
  Set "PPDir=%%~dpG")
  
  ....
:PROCESS_FILES
rem Now the % works
echo "%filename%"


Re: % in file list causing issues with call :command

Posted: 30 Mar 2020 15:35
by stevehero
Thanks, jeb. Greatly appreciated.

That may be what was causing another issue I had:
Maximum setlocal recursion level reached.

Re: % in file list causing issues with call :command

Posted: 31 Mar 2020 08:13
by Squashman
stevehero wrote:
30 Mar 2020 15:35
Maximum setlocal recursion level reached.
That was caused by the fact that the SETLOCAL was within the FOR command but the ENDLOCAL was outside of the FOR command.

Re: % in file list causing issues with call :command

Posted: 31 Mar 2020 13:02
by stevehero
Oh, thanks to you two I have a somewhat better understanding of how SetLocal EnableDelayedExpansion, SetLocal and EndLocal work in general.