PDA

View Full Version : Creating batch files



Don Julio Anejo
2013-11-14, 07:46 AM
Hi guys, I'm in a pinch at work (this is pretty far outside of my skills), and I need to create a bat file to basically process my data.

I have: directory with a large number of items in it. These have to be run through an executable with the following argument:

executable [filepath1] [filename]

Executable creates a plaintext output file (with given filename and custom extension) with some information in it, so ideally I'd like to use a few elements from the output to create the file name, to easily distinguish between files. Output file is formatted like { "element1" "element2" "element3" }, so ideally I'd like to use some of the output file to make the file name something like element1_element2_element3.extension.

Finally, a few elements from every single output file have to be gathered into a "superfile", formatted kinda like that:

{
"filename1.extension": { "title": "element1", "type": "element2" }
"filename2.extension": { "title": "element1", "type": "element2" }
}

I could do this in VB, but I have absolutely no idea where to start with batch files (my knowledge is limited to basic command line tasks like cd or mkdir). I'd figure it out myself, but it's on a major time crunch so I simply don't have 2 days or so it would take for me to learn it.

I'd be extremely glad if someone could either help me write an example code (I can fill in the blanks myself), or teach me the basics of what I need to know to do this. Willing to pay a small amount ($40 or so seems fair?), in $CDN, or if you play League of Legends, RP.

Tylorious
2013-11-14, 07:51 AM
are all of the files you are going to be working with formatted identically?

Don Julio Anejo
2013-11-14, 08:02 AM
are all of the files you are going to be working with formatted identically?
Yes, they will be. Only the values of things like element1 and file paths change.

Krazzman
2013-11-14, 08:38 AM
OK, I never really wrote a .bat myself but here we use external .txt as inputfiles for what that thing has to do.

Example .bat:
c:\windows\system32\ftp -s:"[PATH TO INPUT DATASET]"
pause

then in the .txt file we use as input:
open HOST-ID
ID
PW
cd ..
lcd d:\

quote site xlate=ftpansi

get 'HOST-DATASET' "OUTPUT PATH"

close
quit
bye

Basically you need to search for a function you can use and then implement it that way. Is there maybe a commandline expert you can shoot an email to? He might know where a good documentation is...

Don Julio Anejo
2013-11-14, 09:08 AM
I've figured out some basics, such as how to save a list of data files to a separate list:

setlocal
dir /b /s *.exe > list.txt

But I have no idea how to implement a

For each item in list.txt do...

Unfortunately, it's 6AM here so anyone I can contact is probably asleep (and even if they're not, it's still pretty rude). If someone can point me to a good reference, I'd be really grateful too.

All the ones I've found so far are either extremely basic ("this is a bat file... it runs things.. this is a "Hello World" echo"), or assume extensive previous knowledge of DOS and command line.

Krazzman
2013-11-14, 09:13 AM
Unfortunately, it's 6AM here so anyone I can contact is probably asleep (and even if they're not, it's still pretty rude). If someone can point me to a good reference, I'd be really grateful too.

Maybe use this? (http://www.computerhope.com/overview.htm)

I don't know if this can help you. Else maybe search for something that resembles what you want in the term of command and google how to use it. The good part about this is that it is old enough that you get enough examples... the bad thing is some specific things are that old that no one is interested in sharing it...

Krazzman
2013-11-14, 09:14 AM
Also are you dead set on using .bat?
Why don't you make a dirty solution using VB? Then fix it when you have time?
SAS could probably do what you want too but I am unsure what you are even doing...

Lorn
2013-11-14, 09:26 AM
Ok. I'm simulating what I think you have. Correct me if I'm wrong.

Basically: One directory, several subdirectories, each with a few things that need running through this one program.

So, something like:




BASE FOLDER
| 1
| 2
| 3
| 4
|
+---SUBDIR 1
| 1
| 2
| 3
| 4
|
\---SUBDIR 2
1
2
3
4


Wherein, SUBDIR [x] is a subdirectory, and the numbers are the files that need running through this executable?

Without knowing what the individual files are or how the executable file works I can't immediately give you a huge amount of help, though I can probably say how to create the output file - at the end of the argument, add >>output.txt, and it'll append each result to output.txt as a new line in the file. Make sure that that's >> as opposed to >, else it'll just overwrite it each time.

Beyond that, my guess would be that you're looking to get a way to pick up the filenames as a user input kind of thing, though I'm not immediately sure how to do that automatically - I've not done much .bat stuff for a long, long time. Looking through the stuff I wrote about five years ago there's not much that can help you.

You might be able to do something with Excel and the concatenate function? Get every single filename into Excel, concatenate them together with the arguments, copy every cell, then paste the raw data into a txt file... though I'm not sure how well that'd work, and it would be *incredibly* clunky. A wildcard (CMD uses * by default) might yield results as well.

Grinner
2013-11-14, 09:28 AM
I've been working on this for a little while now. I need to be going, but here's what I've got so far. Hopefully it will be of some help

For the initial data processing, you could use something like:


executable C:\file\path *.extension

That is unless the files do not share the same extension or if they're stored in different directories.


For the last part, there aren't too many write commands available. The best I can think of involves creating two batch files, "run.bat" and "actual.bat".

run.bat would look something like this:

executable C:\file\path *.extension
actual.bat > C:\output.txt
pause

And actual.bat would look something like:

@echo off
echo {
echo "filename1.extension": { "title": "element1", "type": "element2" }
echo "filename2.extension": { "title": "element1", "type": "element2" }
echo }


You would need to replace the names with variables (declared with the command "set" and called with two %'s).

So it would look more like:

@echo off
setlocal

set filename1=test
set title=test
set element1=test
set type=test
set element2=test
set loop_counter=0
set total_files=6 ::maybe dir can help here?

echo {
:whileloop
echo %filename1%.extension: { %title%: %element1%, %type%: %element2% }
set /A loop_counter=%loop_counter%+1
if loop_counter=total_files goto endloop
goto whileloop
:endloop
echo }

endlocal


I'm not sure about reading the strings that title, element1, type, and element2 represent out of the files.

factotum
2013-11-14, 12:12 PM
I'm not convinced batch files are the right approach to this, especially when you're not familiar with them--the batch file language in DOS/Windows isn't really all that powerful, y'see. I have no idea how you'd do stuff like reading the contents of the output file and renaming it to match, for instance.

However, you say you can do this in VB--in all modern versions of Windows there's a thing called the Windows Scripting Host which basically allows you to run VB or Java scripts from a command prompt. In order to use this, you'd write your VBScript (bearing in mind that the scripting host implements its own classes and methods for things like file read/write, so you'll need to look those up), and then call it from a batch file using a command like this:

CSCRIPT //B //NOLOGO MYFILE.VBS

WSH is a lot more powerful than the standard batch file parser, so I'd use it for something like this.

Don Julio Anejo
2013-11-14, 02:34 PM
I'm basically told it has to be a bat file, or they won't accept my work.

Thanks for the help guys, I'm going to play around for a few hours and see what I can do.