Sam Rohod
Sam Rohod

Reputation: 21

Problem with EnableDelayedExpansion, For, and filenames with special characters like ! % &

The Setup:

  1. I have a set of files of type .type1 and want to batch convert them to .type2 using a program I have
  2. The converting program Program.exe (in a folder called TOOLS) takes two arguments -i for InputFile and -o for OutputFile but is unable to create folders/directories (if the output folder doesn't exist it fails)
  3. The .type1 files are in different folders. All of these folders are in a folder called InputFolder
  4. I want the .type2 converted files to be outputted to the OutputFolder preserving the folder structure of the originals
  5. So, In the main folder I have 4 things:-
    A. InputFolder :where all the input files (divided into different folders) exist
    B. OutputFolder:where all the converted files should be outputted (divided into different folders matching the InputFolder)
    C. TOOLS :where the converter Program.exe exists
    D. Batch Convert.bat :the batch file I wrote to batch convert the files. The contents of which are in the next section

What I tried:

rem -------Setting some general variables-------
set "BatchPath=%~dp0"
set "InputPath=%BatchPath%InputFolder\"
set "OutputPath=%BatchPath%OutputFolder\"

rem -------Saving a list of all .type1 files-------
dir /s/b/a-d "%InputPath%*.type1">"%BatchPath%TOOLS\FilesList.txt"

rem -------Starting a loop for each file listed in FilesList.txt -------
for /f "usebackq delims=" %%j in ("%BatchPath%TOOLS\FilesList.txt") do (

setlocal EnableDelayedExpansion

rem --Setting variables replacing "input string" with "output string"--
set "InputFileType2=%%~dpnj.type2"
set "OutputFile=!InputFileType2:%InputPath%=%OutputPath%!"

set "InputFilePath=%%~dpj"
set "OutputFilePath=!InputFilePath:%InputPath%=%OutputPath%!"

rem --Creating output folders--
mkdir "!OutputFilePath!"

rem --Start the converting process--
"%BatchPath%TOOLS\Program.exe" -i "%%j" -o "!OutputFile!"

endlocal
)

The Problem:
Some of the folders and the files inside the InputFolder have special characters (like ! % &) which cause problems with the converting. Is there a way to do this with the least amount of conflict with special characters (like removing the need to EnableDelayedExpansion to remove conflict with !?
is there a way to do it without the PwerShell or the Call function?
(because I have thousands of big files and the Call function can be slow and it seems like it always tries to access the HDD first before a :Label)

Upvotes: 2

Views: 129

Answers (1)

jeb
jeb

Reputation: 82307

As compo said, you just need to change the order of lines.
It's important to expand the FOR variables only with delayed expansion disabled.
Because expanding them inside delayed expansion enabled, destroys the exclamation marks !

for /f "usebackq delims=" %%j in ("%BatchPath%TOOLS\FilesList.txt") do (
 
  rem --Setting variables replacing "input string" with "output string"--
  set "InputFileType2=%%~dpnj.type2"
  set "OutputFile=!InputFileType2:%InputPath%=%OutputPath%!"

  set "InputFilePath=%%~dpj"
  set "helper=%%j"

  setlocal EnableDelayedExpansion

    set "OutputFilePath=!InputFilePath:%InputPath%=%OutputPath%!"

    rem --Creating output folders--
    mkdir "!OutputFilePath!"

    rem --Start the converting process--
    "%BatchPath%TOOLS\Program.exe" -i "!helper!" -o "!OutputFile!"

  endlocal
)

Upvotes: 1

Related Questions