References to filenames and variables in *.bat

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
yfm
Posts: 3
Joined: 16 Feb 2012 01:01

References to filenames and variables in *.bat

#1 Post by yfm » 16 Feb 2012 01:16

I need to write a batch that would process many input files with similar names like "xxx-yyy-zzz.in". "xxx" and "zzz" are always the same, "yyy" changes and takes values from 0 to 60000 with the step of 3000. All of those files are situated in different subdirectories. I need some cycle that would call an awk program for each of those files in order of increasing of "yyy". Also I need to store "yyy" as a numerical variable to process it with awk and store to output file every time. I'm unsure how to do it :(
I would be very grateful if somebody explains me.
I attach an abstract from the code which is about to do that.

Code: Select all

call sxstart %0
call sx %0
set falist=439GT
set bstep=0 3000 6000 9000 12000 15000 18000 21000 24000 27000 30000 33000 36000 39000 42000 45000 48000 51000 54000 57000 60000
for %%f in (%falist%) do (
 for %%b in (%bstep%) do (
   call nfeat.bat %f-%b-res.sx > %f-%b-rf.tmp
  )
 )
cat %f-%b-rf.tmp > %f-nfeat.wqs
del *.tmp
sxexit


The code is not mine so i have to figure something out for me to understand. How do variables"%f" and "%b" work? They were not introduced earlier... I would like to hear about variables and references to them... like this "%f" and "%b" and different "%1","%2" etc

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: References to filenames and variables in *.bat

#2 Post by foxidrive » 16 Feb 2012 01:40

This is untested. It looks for all filenames with the filespec, extracts the number and left-pads it with zeros, putting the left-padded number and the filename in a sortlistA.txt file, which is then sorted by the padded numbers so that sortfilesB.txt should look like this:

03000 3000 "c:\path1\folder2\xxx-3000-zzz.in"
06000 6000 "c:\path2\folder5\xxx-6000-zzz.in"
09000 9000 "c:\path4\folder6\xxx-9000-zzz.in"
...
60000 60000 "c:\path3\folder9\xxx-60000-zzz.in"

and onward so that the files are all sorted by the number from YYY

Then the last FOR /F loop should merely echo the filename and YYY number in order - where you can add your awk command.

Let me know if any hiccups occur and give examples from sortfiles A and B


Code: Select all

@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%a in ('dir "xxx-*-zzz.in" /b /s /a-d') do (
for /f "tokens=2 delims=-" %%b in ("%%a") do (
set var=00000%%b
set var=!var:~-5!
echo !var! %%b "%%a">>sortlistA.txt
)
)
sort <sortlistA.txt >sortlistB.txt

for /f "tokens=2*" %%a in ("sortlistB.txt") do (
echo %%b %%a
)

yfm
Posts: 3
Joined: 16 Feb 2012 01:01

Re: References to filenames and variables in *.bat

#3 Post by yfm » 16 Feb 2012 01:46

Why are variables mentioned like %f and %b? I'm a newbie and I'm only beginning to study batch and awk syntax, so it's not clear to me :(
I know that %1, %2 are mentionings of variables of the command line in order of their appearance, but how do "%f" and "%b" work?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: References to filenames and variables in *.bat

#4 Post by foxidrive » 16 Feb 2012 02:05

type at a command line:

for %a in (*.*) do @echo %a

It will echo all the filenames in the current folder.

In a batch file the percent signs must be doubled - but %%a here is a metavariable and the scope of *.* is iterated and placed in %%a for each file in the folder.

@echo off
for %%a in (*.*) do @echo %%a


The variable name can be from a to z and A to Z, and several non-alpha characters but they are best avoided.

When using delims and tokens you can segment the filename/or string being parsed, and so use other metavariables from %%a %%b %%c etc for parts of the filename/string in the same for in do loop.

yfm
Posts: 3
Joined: 16 Feb 2012 01:01

Re: References to filenames and variables in *.bat

#5 Post by yfm » 16 Feb 2012 02:24

Could you help me, tell me why does the code I've posted in the first post doesn't work in an appropriate way?..
there're 21 files named "439GT-0-res.sx" up to "439GT-60000-res.sx" with the step of 3000.
The "nfeat.bat" contains an awk program. The idea is that "nfeat.bat" should be executed for every input file, and the result should be stored in an output *.tmp file named "439GT-0-rf.tmp" up to "439GT-60000-rf.tmp" accordingly for every input file.
After that all the *.tmp's (each of them contains 1 line) should be merged together using "cat" into one file "439GT-nfeat.wqs", consisting of 21 lines.
But the batch doesn't work. It only creates a file named "--rf.tmp" that contains a path to the folder, where it is executed and the code of the nfeat.bat

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: References to filenames and variables in *.bat

#6 Post by foxidrive » 16 Feb 2012 05:31

yfm wrote:Could you help me, tell me why does the code I've posted in the first post doesn't work in an appropriate way?..
there're 21 files named "439GT-0-res.sx" up to "439GT-60000-res.sx" with the step of 3000.
The "nfeat.bat" contains an awk program. The idea is that "nfeat.bat" should be executed for every input file, and the result should be stored in an output *.tmp file named "439GT-0-rf.tmp" up to "439GT-60000-rf.tmp" accordingly for every input file.
After that all the *.tmp's (each of them contains 1 line) should be merged together using "cat" into one file "439GT-nfeat.wqs", consisting of 21 lines.
But the batch doesn't work. It only creates a file named "--rf.tmp" that contains a path to the folder, where it is executed and the code of the nfeat.bat


Oh... so they aren't in all different folders.

call sxstart %0
call sx %0


first two lines call different batch files or programs.

call nfeat.bat %f-%b-res.sx > %f-%b-rf.tmp

That should be the following:

Code: Select all

   call nfeat.bat %%f-%%b-res.sx > %%f-%%b-rf.tmp


cat %%f-%%b-rf.tmp > %%f-nfeat.wqs

The command CAT is not a windows command but can probably be replaced with TYPE except it is outside the for loop and so the metavariables fail.

sxexit

Again that is not a batch file command.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: References to filenames and variables in *.bat

#7 Post by foxidrive » 16 Feb 2012 05:36

From what you have described, this could work.

Code: Select all

@echo off
set falist=439GT
set bstep=0 3000 6000 9000 12000 15000 18000 21000 24000 27000 30000 33000 36000 39000 42000 45000 48000 51000 54000 57000 60000
del "%falist%-nfeat.wqs" 2>nul
for %%f in (%falist%) do (
 for %%b in (%bstep%) do (
   call nfeat.bat %%f-%%b-res.sx >>"%falist%-nfeat.wqs"
  )
 )

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: References to filenames and variables in *.bat

#8 Post by Squashman » 16 Feb 2012 07:22

Why not use FOR /L in this instance? Wouldn't that speed up the script considerably?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: References to filenames and variables in *.bat

#9 Post by foxidrive » 16 Feb 2012 07:42

Yeah, FOR /L would work and be more elegant . I'm not sure the 0.2 milliseconds in runtime difference for a dozen iterations would be noticed though. ;)

This might work (untested):

Code: Select all

@echo off
set falist=439GT

del "%falist%-nfeat.wqs" 2>nul

for /L %%b in (0,3000,60000) do (
   call nfeat.bat %falist%-%%b-res.sx >>"%falist%-nfeat.wqs"
)


falist can't be expanded with extra prefixes here but only the OP knows if that is needed.

Post Reply