Click here to Skip to main content
15,888,025 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I would like to read the name of all *.kla files from a directory (C:\TSData\Klarf) and change their names and then move them to a new directory (E:\MESSDAT\DATEN\Klatencor\Klarf).

For file name renaming, in each .kla file, there are two variables that i need to get their values: (for example) LotID "N_123"; Slot 1; and combine these variables like this: LotID_Slot.kla to make a new name.

I wrote this code but after moving a .kla file to a new directory its name is not changed correctly. The file name becomes _. kla. i think these variables (LotID,SLOT) are null. what shoud i do?
C#
@echo off

setlocal enableextensions disabledelayedexpansion
set "LotID="
set "Slot="

for /f %%l  in ('dir /b C:\TSData\Klarf\*.kla') do (

  for /R C:\TSData\Klarf\ %%i in (*.*) do echo %%~nxi
     for /f "usebackq tokens=1,*" %%a in (
        "C:\TSData\Klarf\%%l"
    ) do for %%c in (%%b) do set "%%a=%%~c"

       move /-y "C:\TSData\Klarf\%%l" "E:\MESSDAT\DATEN\Klatencor\Klarf"
       ren "E:\MESSDAT\DATEN\Klatencor\Klarf\%%l" "%LotID%_%Slot%.kla"
        echo %%l moved!
)


Consider that in C:\TSData\Klarf\ there is a file name= 1.kla after execution of path file this file will be renamed to k1_1.kla (because for this file we have LotID=k1, Slot=1) and will move to new directory called E:\MESSDAT\DATEN\Klatencor\Klarf\ with its new name (k1_1.kla).

I dont know why this following For works well alone for a clear .kla file.

C++
:: Finding LotID, Slot in each .kla file
for /R C:\TSData\Klarf\ %%i in (*.*) do echo %%~nxi
  for /f "usebackq tokens=1,*" %%a in (
    "C:\TSData\Klarf\K.kla"
) do for %%c in (%%b) do set "%%a=%%~c"
   move /-y "C:\TSData\Klarf\k.kla" "E:\MESSDAT\DATEN\Klatencor\Klarf"
   ren E:\MESSDAT\DATEN\Klatencor\Klarf\k.kla "%LotID%_%Slot%.kla"

Thanks a lot for ur help!
Posted
Updated 11-Oct-14 15:01pm
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900