Batch - loop misses commands after few passes

I tried to write simple program. It should find .pak files (zip in this case but with changed etension), extract them and pack them using arc.

@echo off
for /r %%i in (*.pak) do ren %%~i %%~ni.arc
for /r %%i in (*.arc) do (
mkdir %%~ni
cd %%~ni
..\arc.exe x -o+ "%%~i" //extract archive at current location//
del "%%~i"
..\arc.exe a -m9 -r "%%~i" *.* //pack files and folders in current folder and create
archive at specific location//
cd..
RD /s /q %%~ni
)
pause

Arc command works i'm sure about that. Everything works perfect untill few passes later. It seems that it suddenly stop doing cd.. command because it starts creating folder then folder inside then again and again.

Locations of files: ...\arc.exe ...\program.bat ...\file1.pak ...\folder1\file2.pak ...\folder2\file3.pak etc for pack

I tried also

@echo off
setlocal enableextensions enabledelayedexpansion
for /r %%i in (*.pak) do ren %%~i %%~ni.arc
for /r %%i in (*.arc) do (
mkdir %%~ni
cd %%~ni
..\arc.exe x -o+ "%%~i"
del "%%~i"
..\arc.exe a -m9 -r "%%~i" *.*
cd..
RD /s /q %%~ni
)
pause

The same result

Answers


You definitely do not want to enable delayed expansion since it will corrupt file names if they happen to contain ! character.

I believe your problem is simply you haven't quoted your path/file names. Spaces and special characters in path/name will cause problems unless they are quoted.

This is completely untested, but I think it may fix you problem:

@echo off
for /r %%i in (*.pak) do ren "%%i" "%%~ni.arc"
for /r %%i in (*.arc) do (
  mkdir "%%~ni"
  cd "%%~ni"
  ..\arc.exe x -o+ "%%~i" //extract archive at current location//
  del "%%i"
  ..\arc.exe a -m9 -r "%%i" *.* //pack files and folders in current folder and create archive at specific location//
  cd..
  rd /s /q "%%~ni"
)
pause

Need Your Help

My Laravel 5.2.10 Sessions wont persist

php laravel session laravel-5 session-cookies

I have a brand new installation of Laravel 5, in fact I have tried this on multiple versions and keep hitting the same issue.

Repository is rebasing, action to take to not damage the repository?

git smartgit

I'm working in a GIT repo from office and from home. The latest day I work on office I forgot to commit changes to the repo and made changes in the same file but in home where I did the commit. Now...