Can't figure out how to loop to next variable

January 24, 2011 at 10:37:19
Specs: Windows XP
have directory of:
1111.rtf
1111.jpg
1111.xml
1112.txt
1113.rtf
1113.jpg

my code parses the 1111 files great
it gives me 1111.txt and inserts the the correct text in the 1111.txt created by the code

What i cant figure out is - how this should loop for the next variable or filename to be more accurate.

I feel like I am so close that I may need to add the entire code to the DO section of the code but what do I know.

@echo off
::grab content of file and put into memory array

SetLocal EnableDelayedExpansion
::This line is just in case n has been defined before the batch file is run
Set n=
Set _InputFile=outgoing.txt
For /F "tokens=*" %%I IN (%_InputFile%) DO (
Set /%a n+=1
Set _var!n!=%%I
)
:: This line will display the variables just assigned
:: For testing only, delete when not needed

Set _


::This needs to be looped for every file in folder not of same name

::creating cvl file name and inserting data into that file

Set _var1=!_var1: =!
echo COPY OUT !_var1!.000 >> "!_var1!.txt"
echo.>>"!_var1!.txt"
echo DEL OUT !_var1!.000 >> "!_var1!.txt"
echo.>>"!_var1!.txt"


::look for files of same name

echo %_var2%
echo %_var1%

pause

IF %_var2%==%_var1% goto ADDFILENAME


GOTO END

:ADDFILENAME

echo COPY OUT !_var1!.001 >> "!_var1!.txt"
echo.>>"!_var1!.txt"
echo DEL OUT !_var1!.001 >> "!_var1!.txt"
echo.>>"!_var1!.txt"

:END

echo %_var3%
echo %_var1%

pause

::look for files of same name X2

IF %_var3%==%_var1% goto 2ADDFILENAME


GOTO END

:2ADDFILENAME

echo COPY OUT !_var1!.002 >> "!_var1!.txt"
echo.>>"!_var1!.CVL"
echo DEL OUT !_var1!.002 >> "!_var1!.txt"
echo.>>"!_var1!.txt"

:END


@echo off
EndLocal
@echo off

Please lemme know if you can point me in the right direction.


See More: Cant figure out how to loop to next variable

Report •


#1
January 24, 2011 at 11:49:46
Why use an array when you could just use a subroutine...

@echo off
SetLocal EnableDelayedExpansion
Set _InputFile=outgoing.txt

For /F "tokens=*" %%I IN (%_InputFile%) DO (
  call :ADDFILENAME %%I
  call :DOSOMETHINGELSE %%I
)
call :END
goto :EOF

:ADDFILENAME
  Set _OutputFile=OUT %1.000

  :: Here you could do the following...
  :: If exists %1.000 set _OutputFile=OUT %1.001
  :: Not sure waht you wanted here need more detail about files already exist.

  echo COPY %_OutputFile% >> "%1.txt"
  echo.>>"%1.txt"
  echo DEL %_OutputFile% >> "%1.txt"
  echo.>>"%1.txt"
goto :EOF
:: goto :EOF  is like a Return in a GOSUB, which returns you back to where the CALL was made.

:DOSOMETHINGELSE
  echo Doing something on: %1
  pasue
goto :EOF 
:: goto :EOF  is like a Return in a GOSUB, which returns you back to where the CALL was made.

:END
  echo %_var3%
  echo %_var1%
goto :EOF

This is coded to do every thing as subroutines so you don't need an array. This is not exactly what you want but it shows you how you can recode it it as subroutines and the power of doing so.


Report •

#2
January 31, 2011 at 11:10:58
that works a little better - still having issue with text inside file that is created -- will post more if I can not find the answer my self -- thanks

Report •

#3
January 31, 2011 at 12:10:01
With out an example of how you want the exact out put to be formatted, I can not help you there.

Report •

Related Solutions

#4
January 31, 2011 at 12:33:13
I need the file name variable Set _OutputFile = OUT %1.000
to increase the Extension by 1 if it exists already

so on the next iteration of the loop if OUT %1.000 already exists
Set _OutputFile = OUT %1.001
Set _OutputFile = OUT %1.002

and so on and so forth

hope that made sense

if the a documents exist more than once the entry gets written to the file as

COPY OUT xxxxx.000

DEL OUT xxxxx .000

COPY OUT xxxxx.000

DEL OUT xxxxx.000

COPY OUT xxxxx.000

DEL OUT xxxxx.000

and it should be

COPY OUT xxxxx.000

DEL OUT xxxxx .000

COPY OUT xxxxx.001

DEL OUT xxxxx.001

COPY OUT xxxxx.002

DEL OUT xxxxx.002


Report •

#5
February 1, 2011 at 10:00:00
Again not clear. Do you want separate files like...

file.000

which has...

COPY OUT xxxxx.000
DEL OUT xxxxx .000

and...

file.001

which has

COPY OUT xxxxx.001
DEL OUT xxxxx.001

and so on?

If it is to create a new file ratcheting the number up each time; what is suppose to be in each file? Give examples of File.000 and File.001. Also if you could just explain what you are trying to with these files it might help.


Report •

#6
February 2, 2011 at 05:16:19
Many files in this directory could have the same file name with a different extension indicating they are related. The programming creates a .cvl file for each unique file name and inserts an entry into that file - (xxxxx =%1 in the code)

COPY OUT xxxxx.000

DEL OUT xxxxx .000
into that file.

For each non unique file name it should add another entry into the same file. The programming is actually doing this, but it is repeating the same

COPY OUT xxxxx.000

DEL OUT xxxxx .000

when it should be adding a 1 for every iteration (hope thats the right word). The second entry should read

COPY OUT xxxxx.001

DEL OUT xxxxx .001

If there are three with same name the entry on the third time around would be

COPY OUT xxxxx.002

DEL OUT xxxxx .002


Report •

#7
February 4, 2011 at 06:17:11
here is the scenario

lets say in the directory we are working with (WORK)
has 7 files in it

AAAAA.txt
AAAAA.png
AAAAA.tif
BBBBB.txt
BBBBB.png
CCCCC.txt

Running this script should output three different file (which it currently does)
these files are
AAAAA.CVL
BBBBB.CVL
CCCCC.CVL

The content of AAAAA.CVL
currently contains

COPY OUT AAAAA.000

DEL OUT AAAAA.000

COPY OUT AAAAA.000

DEL OUTAAAAA.000

COPY OUT AAAAA.000

DEL OUT AAAAA.000

and it should contain

COPY OUT AAAAA.000

DEL OUT AAAAA.000

COPY OUT AAAAA.001

DEL OUTAAAAA.001

COPY OUT AAAAA.002

DEL OUT AAAAA.002

The content of BBBBB.CVL
currently contains

COPY OUT BBBBB.000

DEL OUT BBBBB.000

COPY OUT BBBBB.000

DEL OUT BBBBB.000


and it should contain

COPY OUT BBBBB.000

DEL OUT BBBBB.000

COPY OUT BBBBB.001

DEL OUT BBBBB.001

The content of CCCCC.CVL
currently contains

COPY OUT CCCCC.000

DEL OUT CCCCC.000

this is correct.

I hope I explained this to where you understand it and can assist ??


Report •

#8
February 4, 2011 at 06:17:56
ok 6 files in the directory :)

Report •


Ask Question