Page 1 of 1
Read input file, create seperate files based on table
Posted: 09 Aug 2010 06:17
by darioit
Hello
I read this article "batch file to read imput file, create seperate files " and I have a issue very similar
This is my sequential input file:
AA00codeotherdata
AAotherdata
......
AA99codeotherdata
this is my table:
code;newcode
.....;.........
I'd like to create new file called newfile.newcode.txt splitting with beginning AAcode from table and end with AA99code
this is example of file name
AA001234other
AAsomedata
AA991234other
AA005678other
AAsomedata
AA995678other
table:
1234;rome
5678;milan
new file :
fix.rome.txt
fix.milan.txt
Thanks
Re: Read input file, create seperate files based on table
Posted: 09 Aug 2010 11:07
by aGerman
Are all files you want to rename in the same folder?
Are always the first 4 characters used for the new files too?
Is the length of (old) code always 4 characters?
What file extensions do the old file names have?
Are there special characters (like "&") used in file names?
Regards
aGerman
Re: Read input file, create seperate files based on table
Posted: 09 Aug 2010 15:39
by darioit
Are all files you want to rename in the same folder? yes
Are always the first 4 characters used for the new files too? variable
Is the length of (old) code always 4 characters? yes it depends on table, but yes 4
What file extensions do the old file names have? g00v00xxx where xxx is sequential but one is processed at time
Are there special characters (like "&") used in file names? no not used only dot separe file name
Tomorrow I give you a more detailed sample
Kind Regards
Dario
Re: Read input file, create seperate files based on table
Posted: 10 Aug 2010 01:01
by darioit
layout input file:
col 1-2 'AA' code product
col 3-4 "00" start data or "99" end data
col 5-8 "1234" old code
table1 (4 char + 8 char fix)
1234;000milan
5678;0000rome
table2 (2 char + 12 char fix)
AA;ABCDEFG_0000
BB;QWERTYO_0000
name of new file with insire record from AA00code (table1) end AA99code (table1)
somecharfix.newcode(from table1).newcod(from table2)
Re: Read input file, create seperate files based on table
Posted: 10 Aug 2010 08:01
by aGerman
You could try this:
The batch file, Table1.txt and Table2.txt are in the same folder (but not together with the other files).
Code: Select all
@echo off &setlocal
pushd "C:\your\folder\with\files"
for /f "delims=" %%a in ('dir /a-d /b') do (
set "name=%%a"
call :procName
)
popd
pause
goto :eof
:procName
set "newCode1="
set "newCode2="
set "prodCode=%name:~0,2%"
set "num=%name:~2,2%"
set "oldCode=%name:~4,4%"
for /f "usebackq tokens=1* delims=;" %%a in ("%~dp0Table1.txt") do (
if "%oldCode%"=="%%a" set "newCode1=%%b"
)
for /f "usebackq tokens=1* delims=;" %%a in ("%~dp0Table2.txt") do (
if "%prodCode%"=="%%a" set "newCode2=%%b"
)
ECHO ren "%name%" "%prodCode%%num%.%newCode1%.%newCode2%.txt"
goto :eof
If you think it would work, remove the ECHO command.
Regards
aGerman
Re: Read input file, create seperate files based on table
Posted: 10 Aug 2010 09:42
by darioit
Hello AGerman Thanks a lot it's almous perfect!
I make some changes and I have one two more question
The first is when I write the data
ECHO %name% >> "ADDRESS.%newCode1%.%newCode2%"
it's add a blank space at the end of line
The second question in when the batch read the code into the table to find the code I think there's no reason to read the table to the end, so the code must be fast
Here the code modified
Code: Select all
@echo off &setlocal
pushd "C:\your\folder\with\files"
set "newCode1="
set "newCode2="
for /f "delims=" %%a in (fileinput.txt) do (
set "name=%%a"
call :procName
)
popd
goto :eof
:procName
set "prodCode=%name:~0,2%"
set "num=%name:~2,2%"
set "oldCode=%name:~4,4%"
for /f "usebackq tokens=1,2 delims=;" %%a in ("%~dp0Table1.txt") do (
if "%oldCode%"=="%%a" set "newCode1=%%b"
)
for /f "usebackq tokens=1,2 delims=;" %%a in ("%~dp0Table2.txt") do (
if "%prodCode%"=="%%a" set "newCode2=%%b"
)
ECHO %name% >> "ADDRESS.%newCode1%.%newCode2%"
goto :eof
Re: Read input file, create seperate files based on table
Posted: 10 Aug 2010 11:41
by jeb
darioit wrote:The first is when I write the data
ECHO %name% >> "ADDRESS.%newCode1%.%newCode2%"
it's add a blank space at the end of line
It's because of the space behind %name%
You can use
Code: Select all
ECHO %name%>> "ADDRESS.%newCode1%.%newCode2%"
or
>> "ADDRESS.%newCode1%.%newCode2%" ECHO %name%
darioit wrote:The second question in when the batch read the code into the table to find the code I think there's no reason to read the table to the end, so the code must be fast
You can break a for loop with a goto :label
Code: Select all
for /f "usebackq tokens=1,2 delims=;" %%a in ("%~dp0Table1.txt") do (
if "%oldCode%"=="%%a" (
set "newCode1=%%b"
goto :break_loop
)
)
:break_loop
Re: Read input file, create seperate files based on table
Posted: 10 Aug 2010 15:29
by darioit
ok thanks for your suggestion
I have simplify all operation and now is very fast, what do you think is correct writing code in this way?
@echo off &setlocal
pushd "E:\bat\Temp"
for /f "delims=" %%a in (fileinput.txt) do set "name=%%a"&call :procName
popd
goto :eof
:procName
if "%name:~2,2%"=="00" (
for /f "usebackq tokens=1,2 delims=;" %%a in ("%~dp0Table2.txt") do if "%name:~4,4%"=="%%a" set "newCode1=%%b"
for /f "usebackq tokens=1,2 delims=;" %%a in ("%~dp0Table1.txt") do if "%name:~0,2%"=="%%a" set "newCode2=%%b"
)
>> "ADDRESS.%newCode1%.%newCode2%" ECHO %name%
goto :eof
Re: Read input file, create seperate files based on table
Posted: 10 Aug 2010 15:48
by aGerman
Think you could make it like jeb suggested.
Code: Select all
@echo off &setlocal
pushd "E:\bat\Temp"
for /f "delims=" %%a in (fileinput.txt) do set "name=%%a"&call :procName
popd
goto :eof
:procName
if "%name:~2,2%"=="00" (
for /f "usebackq tokens=1,2 delims=;" %%a in ("%~dp0Table2.txt") do if "%name:~4,4%"=="%%a" (
set "newCode1=%%b"
for /f "usebackq tokens=1,2 delims=;" %%c in ("%~dp0Table1.txt") do if "%name:~0,2%"=="%%c" (
set "newCode2=%%d"
goto break_loop
)
)
)
:break_loop
>> "ADDRESS.%newCode1%.%newCode2%" ECHO %name%
goto :eof
[Edit: Think you have to use different names for the dynamic variables in the 2nd FOR loop in this case /]
Regards
aGerman
Re: Read input file, create seperate files based on table
Posted: 11 Aug 2010 02:49
by darioit
Thanks you very mutch now it works very fast, but I have one more question, in a row I have
AA001234 data data & data data
and when I write %name% the simbol "&" is view like a separator and don't write the full row, why?