Batch Scripting with Variables from .txt

Batch, ASP, JScript, Kixtart, etc.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
praetorian
Posts: 2
Joined: Thu Aug 06, 2009 3:55 pm

Batch Scripting with Variables from .txt

Post by praetorian » Thu Aug 06, 2009 4:08 pm

I was wondering how I might be able to write a batch file that pulls lines of a text file and uses them as variables.

Specifically, I've got a lot of folders that I need to each be in their own .zip file. Like...over a thousand folders. Much more than I'd care to individually zip up myself.

I've got the command-line version of 7-zip and the syntax for the command is:

7za a <archivename> <filename...>

I figured since I've got all the folders that need zipping in one large one, I can just pipe the directory listing into a textfile with "dir/b > list.txt" and wind up with something like:

folder1
folder2
folder3

...and so on. Then make a batch file run something like

7za a %filename%.zip %filename%

over and over. Using the first line of the textfile to fill the %filename% variable, then move on to the second line, then the third, and so on.

Unfortunately, I have no earthly idea how to do that.

My experience with anything even remotely related to scripting is playing around a bit with SmallBasic...well...except when I wrote a batch file that ran dsadd a number of times to add a bunch of users to Active Directory. But otherwise, no idea what I'm doing!

Any advice on this would be greatly appreciated!

User avatar
praetorian
Posts: 2
Joined: Thu Aug 06, 2009 3:55 pm

Batch Scripting with Variables from .txt

Post by praetorian » Thu Aug 06, 2009 4:08 pm

I was wondering how I might be able to write a batch file that pulls lines of a text file and uses them as variables.

Specifically, I've got a lot of folders that I need to each be in their own .zip file. Like...over a thousand folders. Much more than I'd care to individually zip up myself.

I've got the command-line version of 7-zip and the syntax for the command is:

7za a <archivename> <filename...>

I figured since I've got all the folders that need zipping in one large one, I can just pipe the directory listing into a textfile with "dir/b > list.txt" and wind up with something like:

folder1
folder2
folder3

...and so on. Then make a batch file run something like

7za a %filename%.zip %filename%

over and over. Using the first line of the textfile to fill the %filename% variable, then move on to the second line, then the third, and so on.

Unfortunately, I have no earthly idea how to do that.

My experience with anything even remotely related to scripting is playing around a bit with SmallBasic...well...except when I wrote a batch file that ran dsadd a number of times to add a bunch of users to Active Directory. But otherwise, no idea what I'm doing!

Any advice on this would be greatly appreciated!

User avatar
jvierra
Posts: 13718
Joined: Tue May 22, 2007 9:57 am
Contact:

Batch Scripting with Variables from .txt

Post by jvierra » Thu Aug 06, 2009 4:20 pm

Hi - so you want to write batch files.

Start by going to a command prompt and typeing "help for". The instructions for how to take batch input from a file are documented there.

Type just help to get a list of most commands.



Locked