10

I have one file destination.txt with path information about my CDs:

 C:\Users\NekhayenkoO\Desktop\LOG Dateien CD Imaging\SME99.ISO
C:\Users\NekhayenkoO\Desktop\LOG Dateien CD Imaging\Biomasse.iso 
C:\Users\NekhayenkoO\Desktop\LOG Dateien CD Imaging\SAMPE36.ISO

Now I have to rename the ISOs with the numbers that are in the file PPN.txt one after another:

470692405 
394006801 
348117876 

So it should be

SME99.ISO -> 470692405.ISO
Biomasse.iso -> 394006801.ISO
Sampe36.ISO -> 348117876.ISO

I have the following code for it:

< "PPN.txt"  (for /F "usebackq delims=" %%a in ("destination.txt") do (
  set/P out="" & rename "%%a" "!out!%%~xa" 

I want to modify the code that way that it works for the file destination.txt:

Success on: "C:\Users\NekhayenkoO\Desktop\LOG Dateien CD Imaging\SME99.ISO" 
Error on:   "C:\Users\NekhayenkoO\Desktop\LOG Dateien CD Imaging\Biomasse.iso" 
Success on: "C:\Users\NekhayenkoO\Desktop\LOG Dateien CD Imaging\SAMPE36.ISO" 

If "Success on" stays before the path in destination.txt the image should be renamed as always only with the number from PPN.txt. But if "Error on" stays before the path in destination.txt the image should be renamed like this e_%number from PPN.txt% . So it should be additional prefix e_ there

Oleg_08
  • 447
  • 1
  • 4
  • 23

2 Answers2

5

Edit Batch now takes the prefix into account, splitting the lines from destination.txt at the colon into %%A and %%B

@Echo off&SetLocal EnableExtensions EnableDelayedExpansion
< "PPN.txt"  (
    for /F "usebackq tokens=1,* delims=:" %%A in ("destination.txt") do (
        Set "out="
        Set /P "out="
        Set "out=!out: =!"
        If /i "%%A" equ "Success on" (
            ren %%B "!out!.ISO" && echo Renamed %%B to "!out!.ISO" 
        ) Else If /i "%%A" equ "Error on" (
            ren %%B "e_!out!.ISO"  && echo Renamed %%B to "e_!out!.ISO" 
        ) Else (Echo unknown prefix "%%~A")
    ) 
)

Simulated ISO files returned this output:

Renamed  "C:\Users\NekhayenkoO\Desktop\LOG Dateien CD Imaging\SME99.ISO"  to "470692405.ISO"
Renamed    "C:\Users\NekhayenkoO\Desktop\LOG Dateien CD Imaging\Biomasse.iso"  to "e_394006801.ISO"
Renamed  "C:\Users\NekhayenkoO\Desktop\LOG Dateien CD Imaging\SAMPE36.ISO"  to "348117876.ISO"
  • Syntaxerror Error on: "Success on: "C:\Users\NekhayenkoO\Desktop\LOG Dateien CD Imaging\SME99.ISO" " Syntaxerror. Syntaxfehler. Error on: "Error on: "C:\Users\NekhayenkoO\Desktop\LOG Dateien CD Imaging\Biomasse.iso" " Syntaxerror. Syntaxerror. Error on: "Success on: "C:\Users\NekhayenkoO\Desktop\LOG Dateien CD Imaging\SAMPE36.ISO" " Syntaxerror. – Oleg_08 Jul 21 '17 at 15:56
  • In your output it should be Error on: "C:\Users\NekhayenkoO\Desktop\LOG Dateien CD Imaging\Biomasse.iso " instead of Success on – Oleg_08 Jul 21 '17 at 15:58
  • Sorry did some test with English and German user accounts and can't get the errors you describe, even if runnig the batch from powershell. Without echo I get errors like `The system cannot find the path specified. Error on: "C:\Users\NekhayenkoO\Desktop\LOG Dateien CD Imaging\SME99.ISO"` The syntax error let's me assume there is an error on transfering the code. Did you copy and paste and save the batch as plain ascii? –  Jul 21 '17 at 16:11
  • yes, I made it all in ascii, but it's the same problem – Oleg_08 Jul 21 '17 at 16:40
  • What exact windows version do you use and what extension does the batch file have? –  Jul 21 '17 at 16:48
  • I use Windows 8.1 Pro and the batch file has .bat extension. It seems to me that there some problems with encoding quotes: maybe in destination.txt or in the batch – Oleg_08 Jul 21 '17 at 16:52
  • I scraped the files from this website and didn't have problems with it (using firefox on my up2date Win10pro). –  Jul 21 '17 at 16:55
  • The sense of the script is to recognize whether Error on or Success on stands before Path and dependent on this to rename the ISO in appropriate way. So we need a condition here instead of echo. Sorry, maybe I didn't define it clear enough in my question – Oleg_08 Jul 27 '17 at 10:48
  • It means that the file destination.txt already has Success on or Error on before path – Oleg_08 Jul 27 '17 at 10:59
  • Understood, see changed batch. –  Jul 27 '17 at 11:56
  • yes, that's it. But there is always one blank between the number and .iso after renaming e.g. e_394006801 .ISO . Do you have any idea how to delete it? – Oleg_08 Jul 27 '17 at 12:10
  • The space stems presumably from the ppn.txt file, I changed the batch to remove it. –  Jul 27 '17 at 12:39
  • 2
    Instead of `ren %%B ...` I would write `ren "%%~B" ...` to always have proper quoting... – aschipfl Aug 03 '17 at 08:31
  • 2
    @aschipfl because %%B contains leading spaces the ~ wouldn't work. Ren wouldn't mind the spaces but for the ~ modifier it aren't outer quotes. –  Aug 03 '17 at 09:44
2

Try:

< PPN.txt (
  for /F "delims=" %%a in (destination.txt) do (
    set/P out="" & rename "%%a" "!out!%%~xa"&&echo Success on: "%%a"||(
      echo Error on:   "%%a" & rename "%%a" "e_!out!%%~xa"
    )
  )
)

&& will handle sucess of previous operations (errorlevel 0), || will handle failure.

LS_ᴅᴇᴠ
  • 10,823
  • 1
  • 23
  • 46