0

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
)
Asheara
  • 73
  • 3
  • 12

1 Answers1

1

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

aphoria
  • 19,796
  • 7
  • 64
  • 73
  • Also need to change `%filenametmp%` to `!filenametmp!` – David Ruhmann Feb 06 '15 at 16:15
  • thanks a lot, that helped. I will update the question with the final result :) If you don't mind, may I also ask how to make it search for more file types? I used [this answer](http://stackoverflow.com/questions/11719347/using-forfiles-with-multiple-file-types-for-search-mask) and first for looks like `for /f "delims=" %%A in ('for %%G in (.tga, .png) do 'forfiles /s /m *%%G /c "cmd /c echo @relpath"'') do (...` and that says "system cannot find file for %G..." – Asheara Feb 06 '15 at 18:08
  • @Asheara You should create a new question to ask that. – aphoria Feb 06 '15 at 18:27
  • @Asheara I think I can answer your second question, but it should be done separately. – aphoria Feb 06 '15 at 18:35
  • @aphoria I agree, just was not sure. New question [here](http://stackoverflow.com/questions/28372515/batch-forfiles-in-loop-with-multiple-file-types). – Asheara Feb 06 '15 at 18:37