0

I am having an issue with a Bat file i am writing.

Image: http://i67.photobucket.com/albums/h308/denash1/files.png

(I am not allowed to post images yet)

The Script in the "EXTRACT_ALL(no_decompression).bat is as follows:

@echo off
for /r %%f in (*.cpk) do (
SET filename=%%~nf)
mkdir %filename%
copy  %filename%.cpk .\%filename%
copy CriPakTools.exe .\%filename%
cd %filename%
for /r %%f in (*.cpk) do "CriPakTools.exe" "%%f" ALL
pause

Every-time I run this code it runs fine the first time and it finds the file "725.cpk", it then puts it into the folder 725, and then extracts the content into that folder, which is fine.

But the second time i run it, it still only moves and extract the file 725.cpk and ignores the other CPK file.

Even if I then delete the file "725.cpk" it then complains that the file is missing. Can someone please help me and explain why the .cmd file never checks the other file, which is "110638.cpk", as it is in the same directory?

Thank you very much for your help!

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • your first `for` sets the variable `filename` to every `.cpk` file, so it ends holding the last of them. – Stephan Apr 24 '16 at 19:14
  • I am very sorry i dont understand what you mean.. – Denash Subramaniam Apr 24 '16 at 19:16
  • run your code with `echo on` and you'll see. – Stephan Apr 24 '16 at 19:18
  • I put the echo on and got the following putput. – Denash Subramaniam Apr 24 '16 at 19:31
  • I put the echo on in the code as you have mentioned and saw the following – Denash Subramaniam Apr 24 '16 at 19:31
  • http://i67.photobucket.com/albums/h308/denash1/echo%20on%20output.png – Denash Subramaniam Apr 24 '16 at 19:31
  • see the three `set` commands after the first `for`? The **two** `set filename=725` are because the `/r` which tells `for` to search recursive (including in subdirectories) - that's why it finds `725` even if you delete it: it's still in it's new subdirectory. – Stephan Apr 24 '16 at 19:37
  • ok, that makes sense to me, but i am still trying to figure out a way for the code to see both cpk. do you think maybe i should try using something different instead of for /r to achieve what i need? Because from that explanation i do not think it would help me stick with it, i am not very strong on command line, my main strength is java, and thats why i do not know what i am doing here. – Denash Subramaniam Apr 24 '16 at 19:49
  • first, remove both `/r` (you don't want recursion). Then extend your first `for` loop to include the rest of the code (move the closing `)`). You will need [delayed expansion](http://stackoverflow.com/a/30284028/2152082) for that step. – Stephan Apr 24 '16 at 19:54

1 Answers1

0

(untested code)

@echo off
REM with every file fitting to this file mask do the following:
for  %%f in (*.cpk) do (
  REM get the name only ,without path or extension and make a directory with this name, redirect "already exist" message to NUL:
  mkdir %%~nf 2>nul
  REM copy the file to the new folder:
  copy  %%f .\%%~nf
  REM copy another file to the same destination:
  copy CriPakTools.exe .\%%~nf
  REM set the working directory to the new folder
  pushd %%~nf
  REM execute the tool:
  "CriPakTools.exe" "%%f" ALL
  REM go back to previous working directory:
  popd
)
pause
Stephan
  • 53,940
  • 10
  • 58
  • 91