the code is:
setlocal EnableDelayedExpansion
FOR /f "usebackq tokens=*" %%X in (`dir /a-d /s /b "!search_path!" 2^>^&1`) DO @(
set file_path=%%X
rem do other stuff
)
Delayed expansion is on because the source path might have special characters like backticks percentages exclamation and ^ escape sing. All these characters are allowed in windows paths and I don’t know if and where they will be present.
The problem arise what to do with double percent parameter %%X, how to pass it to another variable without expansion. If DE is on the exclamation sings will be treated as variables with and that would result with a range of weird errors. The same thing is if I disable DE – the same situation, but this time with percentages.
Any idea how to make these lines safe for every possible allowed path that can be found in windows system with no matter how weird characters ?
The problem boils to how to safe pass data from double percent for parameter into normal %variable% so the data can safe passed through delayed expansion from that moment.
I would try to adapt
FOR /Rto your needs, which will solve some of your escape efforts. You can check the format/match of the file listing in your loop vs. in thedir.FOR /Rwill traverse your directory tree (which you’re doing anyway) and return the files that match the pattern you give.Quick example to list all files of type TXT in a directory and it’s sub-directories goes like this:
UPDATED:
This prints the contents of two files in my directory that have exclamation points in them:
Note the absense of delayed variable expansion. Add’l variable references are found at the bottom of the
for /?listing.