2

I relatively new to Batch Scripts and i am trying to create a windows batch file that renames a static array values in one group against an static array values in another - moving to another folder. Something like this:

setlocal EnableDelayedExpansion

set currentDate=%date:~-4,4%%date:~-10,2%%date:~-7,2%
set fromPath=C:\
set toPath=C:\Temp\    

set fileList=(temp1.txt temp2.txt temp3.txt)
set toList=(name1 name2 name3)

I'm looking at this style of array as its looks easier for the user i'm giving it to to add to the list.

Alone (without the toList) the fileList works fine:

for %%i in %fileList% do (
    IF EXIST %FromPath%%%i 
      ren %FromPath%%%i %%~ni_%currentDate%%%~xi & 
      move %FromPath%%%~ni_%currentDate%%%~xi %toPath%
)

But obviously this does not do the renaming from toList in the same order as the list in toList.

I tried code with a counter with an index like this (tried other variants of below aswell) with no luck:

for /L %%i IN (0,1,10) DO (
  IF EXIST %FromPath%%fileList[%%i]%
    ren %FromPath%%%i %%~ni_%currentDate%%%~xi & 
    move %FromPath%%%~ni_%currentDate%%%~xi %toPath%
)

Is something like this possible? If not, I'm open to any other suggestion to the above.

Thanks in advance!

DanielV
  • 2,076
  • 2
  • 40
  • 61
tranceport
  • 21
  • 2

1 Answers1

0

Try this:

@echo off
setlocal EnableDelayedExpansion

set currentDate=%date:~-4,4%%date:~-10,2%%date:~-7,2%
set FromPath=C:\
set toPath=C:\Temp\    

set fileList=(temp1.txt temp2.txt temp3.txt)
set toList=(name1 name2 name3)

rem Separate toList elements into an array
set i=0
for %%a in %toList% do (
   set /A i+=1
   set "toList[!i!]=%%a"
)

set i=0
for %%f in %fileList% do (
   set /A i+=1
   IF EXIST %FromPath%%%f (
      ren %FromPath%%%f %%~Nf_%currentDate%%%~Xf
      for %%i in (!i!) do move %FromPath%%%~Nf_%currentDate%%%~Xf %toPath%!toList[%%i]!
   )
)

For further information about array management in Batch files, see: Arrays, linked lists and other data structures in cmd.exe (batch) script

Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • 'move C:\test1_20152108.txt C:\Temp\ !toList[1]! The syntax of the command is incorrect.' 'move C:\test2_20152108.txt C:\Temp\ !toList[2]! The syntax of the command is incorrect.' The output with echo is something like this... C:\>( set /A i+=1 IF EXIST C:\test1.txt ( ren C:\test1.txt test1_20152108.txt for %i in (!i!) do move C:\test1_20152108.txt C:\Temp\ !toList[%i]! ) ) – tranceport Aug 21 '15 at 11:13
  • You have wrong additional spaces in `set toPath=C:\Temp\ ` line in your original code. Is better to enclose the values in quotes this way: `set "FromPath=C:\"` - `set "toPath=C:\Temp\"` - etc... – Aacini Aug 21 '15 at 11:40
  • @tranceport: Did my solution worked after complete the modifications suggested? – Aacini Aug 26 '15 at 17:07
  • no the toList[%%i] did not convert to the value in the array, even with the " " in the set lines – tranceport Aug 27 '15 at 09:45