Asheara
Asheara

Reputation: 73

Batch - mkdir gives "filename, directory name or volume syntax is incorrect" in loop

I am trying to make batch file for converting and copying files, while keeping the hierarchy of subfolders.

My code so far (the troublesome part) is:

cd "%inputdir%"
setlocal disableDelayedExpansion
for /f "delims=" %%A in ('forfiles /s /m *.tga /c "cmd /c echo @relpath"') do (
  set "file=%%~A"
  setlocal enableDelayedExpansion
  echo !file:~1,-4!
  echo %inputdir%!file:~1!
  set filenametmp=%outputdir%!file:~1,-4!.paa
  echo %outputdir%!file:~1,-4!.paa

  For %%A in ("%filenametmp%") do (
    Set foldertmp=%%~dpA
  )

  IF NOT EXIST "%foldertmp%" (
  mkdir "%foldertmp%"
  ) 
  endlocal
)

Problem is the part with creating folder. The echo under setting filename tmp gives "C:\Users\Asheara\Desktop\cicik\BI\M14\data\M14_body_CO.paa", which is correct

For under it should get a directory part of the path ("C:\Users\Asheara\Desktop\cicik\BI\M14\data") and it works if i do:

For %%A in ("C:\Users\Asheara\Desktop\cicik\BI\M14\data\M14_body_CO.paa") do (
    Set foldertmp=%%~dpA
)

before the loop (above the code i pasted before). However inside this loop it always throws this error, though the for itself and input data are the same.

Does anyone know how to deal with this issue? Thanks

EDIT: This is the working result:

cd "%inputdir%"
setlocal disableDelayedExpansion
for /f "delims=" %%A in ('forfiles /s /m *.tga /c "cmd /c echo @relpath"') do (
  set "file=%%~A"
  setlocal enableDelayedExpansion
  set filenametmp=%outputdir%!file:~1,-4!.paa

  setlocal enableDelayedExpansion
  For %%A in ("!filenametmp!") do (
    Set foldertmp=%%~dpA
  )

  setlocal enableDelayedExpansion
  IF NOT EXIST "!foldertmp!" (
    mkdir "!foldertmp!"
  )
  endlocal
)

Upvotes: 0

Views: 1189

Answers (1)

aphoria
aphoria

Reputation: 20209

You need to enable delayed expansion using setlocal EnableDelayedExpansion and then change %foldertmp% to !foldertmp!.

Upvotes: 1

Related Questions