% in file list causing issues with call :command

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
stevehero
Posts: 5
Joined: 27 Jan 2020 09:11

% in file list causing issues with call :command

#1 Post by stevehero » 29 Mar 2020 15:51

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"

jeb
Expert
Posts: 967
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

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

#2 Post by jeb » 29 Mar 2020 22:52

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%"


stevehero
Posts: 5
Joined: 27 Jan 2020 09:11

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

#3 Post by stevehero » 30 Mar 2020 15:35

Thanks, jeb. Greatly appreciated.

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

Squashman
Expert
Posts: 4195
Joined: 23 Dec 2011 13:59

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

#4 Post by Squashman » 31 Mar 2020 08:13

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.

stevehero
Posts: 5
Joined: 27 Jan 2020 09:11

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

#5 Post by stevehero » 31 Mar 2020 13:02

Oh, thanks to you two I have a somewhat better understanding of how SetLocal EnableDelayedExpansion, SetLocal and EndLocal work in general.

Post Reply