1

i have a problem, i need a code as in title. purpose of the script: i need to copy the file with a different language code, f.e. XXX_x_xYYY_EN_xx to XXX_x_xYYY_ES_xx and XXX_x_xYYY_DE_xx. So far i have this, but it does not work:

setlocal enableDelayedExpansion
for /l %%H in (1,1,2) do (
set endf[%%H]=!fName[%%H]:*_EN_=!
set trim[%%H]=_EN_!endf[%%H]!
set beginf[%%H]=!fName[%%H]:%trim[%%H]%=!
)

fName is set elswere, its something like this fName[1]=XXX_x_xYYY_EN_xx fName[2]=XXXXX_x_xYYYY_EN_x Everything works except the set beginf(it spits out "beginf[1]=fName[1]:="), i've tryed myriads of % nad ! combos. Fun fuct it works in this case (other script, same puprose but work for only one file in directory, i'd like to make it more versatile):

set beginf=!NAME:%langcode%=!

help :) thanks!

deCanaba
  • 13
  • 4
  • 2
    Yes, you are correct. The last line will not work. As you already know, you need to use delayed expansion for all your variables inside a code block. So essentially you need to have double delayed expansion. There is no such thing. But you can use the `CALL` command to get two phases of expansion or you can use a `FOR` command to expand your trim variable and use the `SET` command after the `DO` clause. – Squashman Oct 22 '18 at 17:56
  • I am not sure why you are going through all of this to get what is on the left and right of the language. If you want to remove the language just remove the language. – Squashman Oct 22 '18 at 18:32
  • hey, i need to copy the file with a different language code, f.e. XXX_x_xYYY_EN_xx to XXX_x_xYYY_ES_xx and XXX_x_xYYY_DE_xx – deCanaba Oct 22 '18 at 18:33

2 Answers2

1

So you have two diferent conversions for two array elements, isn't it? So you just need to match each filename with its corresponding conversion, right?

Something like this, perhaps?

set "conv[1]=ES"
set "conv[2]=DE"

for /l %%H in (1,1,2) do (
   for %%c in (!conv[%%H]!) do set "newName[%%H]=!fName[%%H]:_EN_=_%%c_!"
)

I suggest you to read this answer.

EDIT: New method added

This code do exactly the same than the one in your posted answer:

@echo off
setlocal EnableDelayedExpansion

for %%a in (*.xml) do (
    set "fName=%%a"
    for %%c in (ES DE PL) do (
        copy "%%a" "!fName:_EN_=_%%c_!"
    )
)

A comparison of your code vs. this one:

  • You really not need an array of file names. If you create the array just to process its elements once with no further processing, then you are wasting the space occupied by the array. You may do the same thing using an individual file name variable.
  • Also, you not need an array of conversions. If you just want to repeat a command with several conversions, then it is much simpler to use a list of conversions instead.
  • It is a bad idea to use special characters as FOR replaceable parameters (where the documentation specifies a letter). Batch files are intrinsically cryptic, so there is no need to include additional complexities...
  • If you just want to process all files (no rename they) then it is simpler to use a plain for command instead of a for /F one on a 'dir /B' command. The second form requires to execute an additional copy of cmd.exe program and to create a temporary disk file...
  • If the newfName (array) variable is used just to execute the copy command in the next line, then such a variable is (again) a waste of space. You may create the new name in the copy command itself.
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • hey, not exactly as this copy 1st file with ES and second with DE. But it really helped me with the correct solution :) so thank you very much. – deCanaba Oct 22 '18 at 20:48
  • Well, your description was not clear enough... Please, see the new method in my answer. – Aacini Oct 23 '18 at 00:28
  • well clearly your script is far superior than mine :) this is part of a bigger bat file i use for sorting stuff, and the lang array is already there and in use, and i got few of those for different types of lang codes (so it's easier for me to copy _DE_ file into a DE-German folder inside a de-de folder :D) and the # thingie i picked up on some other thread here, in the end im just a simple noob stitching some stuff together ;) Thank you for all your help and knowledge! – deCanaba Oct 23 '18 at 14:07
0

I found the solution :) Full script with file counting below:

@echo off
setlocal enableDelayedExpansion
set /a count=0
for /f %%# in ('dir *.xml /b') do (
    set /a count+=1
    set fName[!count!]=%%~xn#
)

set "conv[1]=ES"
set "conv[2]=DE"
set "conv[3]=PL"

for /l %%H in (1,1,!count!) do (
    for /l %%G in (1,1,3) do (
        for %%c in (!conv[%%G]!) do (
            set "newfName[%%G]=!fName[%%H]:_EN_=_%%c_!"
            copy !fName[%%H]! !newfName[%%G]!
        )
    )   
)

loops clarification:

for /l %%H - loops through the fName array

for /l %%G - loops through the lang table (conv) array

for %%c - combine above loops and copy files with changed names

thanks @Aacini for your input, it put me on the right tracks :)

deCanaba
  • 13
  • 4