# Anybody want to help me write a batch file?

**URL:** https://boards.straightdope.com/t/anybody-want-to-help-me-write-a-batch-file/193578
**Category:** Factual Questions
**Created:** [August 7, 2003, 1:50pm UTC](https://boards.straightdope.com/t/anybody-want-to-help-me-write-a-batch-file/193578 "2003-08-07T13:50:26Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![NoGoodNamesLeft](https://avatars.discourse-cdn.com/v4/letter/n/8797f3/32.png) [@NoGoodNamesLeft](https://boards.straightdope.com/u/NoGoodNamesLeft)
#### Post date: [August 7, 2003, 1:50pm UTC](https://boards.straightdope.com/t/anybody-want-to-help-me-write-a-batch-file/193578/1 "2003-08-07T13:50:26Z")

</div>

I need to create a batch file that will:

1 . Make a backup of a file (say, “ren file.zip backup.zip”).

1. Then (using PKZip) compress a file and name it the same name as the file I just backed up in the previous step (“pkzip file.zip file.stv”)

2. Then open my FTP and make a connection. (Not as important, but hey, if I can it would be helpful).

Any ideas? I don’t thik I need ot “call” pkzip, do I? I mean in DOS, it’s not like I have top type “run pkzip” _then_ zip the file, I can just do it all with the command “pkzip file.zip file.stv” (which is [program] [compressed file name] [source file] ).

How would I handle this in a batch file?

---

<div class="post-metadata">

### Author: ![Popup](https://avatars.discourse-cdn.com/v4/letter/p/94ad74/32.png) [@Popup](https://boards.straightdope.com/u/Popup)
#### Post date: [August 7, 2003, 2:08pm UTC](https://boards.straightdope.com/t/anybody-want-to-help-me-write-a-batch-file/193578/2 "2003-08-07T14:08:23Z")

</div>

Batch files (under DOS) are simply commands that will be executed in order.

If you make a file backup.bat that contains the following

> [@](#):
>
> ren file.zip backup.zip  
> pkzip file.zip file.stv  
> ftp -s ftpcommands.txt

and another file called **ftpcommands.txt** containing

> [@](#):
>
> open _remotehost_  
> user _username__password_
> 
> cd _remotedirectory_  
> put file.zip  
> bye

The, if you execute that file all the commands will be executed in order.  
the -s flag to ftp will cause it to read commands from a file. (Note, I’m a bit rusty on my ftp commands under DOS, but I’m pretty sure that ti works like that.

Good luck!

---

<div class="post-metadata">

### Author: ![daniel801](https://avatars.discourse-cdn.com/v4/letter/d/b2d939/32.png) [@daniel801](https://boards.straightdope.com/u/daniel801)
#### Post date: [August 7, 2003, 2:08pm UTC](https://boards.straightdope.com/t/anybody-want-to-help-me-write-a-batch-file/193578/3 "2003-08-07T14:08:23Z")

</div>

copy ren file.zip backup.zip  
pkzip fle.zip file.stv  
winftp.exe (or whatever)  
exit

put all the above in notepad, and save as “Backup.bat”

---

<div class="post-metadata">

### Author: ![danvanf](https://avatars.discourse-cdn.com/v4/letter/d/13edae/32.png) [@danvanf](https://boards.straightdope.com/u/danvanf)
#### Post date: [August 7, 2003, 2:10pm UTC](https://boards.straightdope.com/t/anybody-want-to-help-me-write-a-batch-file/193578/4 "2003-08-07T14:10:37Z")

</div>

Pretty much, put the exact items you type on a command line to do the functions. Assuming this batch file is called with a parameter being the file name (no extension) of the file to be backed up. (Backit.bat file)  
Backit.bat file listing.  
@Echo Off  
Copy %1.Zip %1Back.Zip  
Del %1.Zip  
Pkzip %1.zip Knownfilename.txt  
ftp [yourserver.com](http://yourserver.com)  
:End of file

You can even send text to the ftp app from a text file so if you had a text file like this  
---- ftporders.txt Start -----  
open [yourserver.com](http://yourserver.com)  
binary  
put filename.zip  
close  
exit  
----- ftporders.txt end ----  
Then on the ftp line it would read  
ftp \<ftporders.txt

There’s no error checking but it can speed things up.

Dan

All heck triple post

---

<div class="post-metadata">

### Author: ![engineer\_comp\_geek](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/engineer_comp_geek/32/504_2.png) [@engineer\_comp\_geek](https://boards.straightdope.com/u/engineer_comp_geek)
#### Post date: [August 7, 2003, 2:14pm UTC](https://boards.straightdope.com/t/anybody-want-to-help-me-write-a-batch-file/193578/5 "2003-08-07T14:14:50Z")

</div>

If you’re going to make a backup I don’t see why you need to copy the file. Just make the zip version. Your batch file would just have 2 lines:

PKZIP zname.zip fname  
FTP -s:mycmds.txt

zname.zip is the output zip file name, fname is the file you want to back up, mycmds.txt contains a list of ftp commands that you want ftp to execute, such as

open \<address\>  
put zname.zip

This combination of batch file and ftp command file would create a zipped backup of a file and archive it on the ftp server.

---

<div class="post-metadata">

### Author: ![xash](https://avatars.discourse-cdn.com/v4/letter/x/c6cbf5/32.png) [@xash](https://boards.straightdope.com/u/xash)
#### Post date: [August 7, 2003, 2:14pm UTC](https://boards.straightdope.com/t/anybody-want-to-help-me-write-a-batch-file/193578/6 "2003-08-07T14:14:59Z")

</div>

Make sure your path includes the directory in which pkzip.exe is. Otherwise add a path command to your .bat file, or copy the pkzip.exe file to the current folder.

---

<div class="post-metadata">

### Author: ![UrbanChic](https://avatars.discourse-cdn.com/v4/letter/u/9dc877/32.png) [@UrbanChic](https://boards.straightdope.com/u/UrbanChic)
#### Post date: [August 7, 2003, 2:20pm UTC](https://boards.straightdope.com/t/anybody-want-to-help-me-write-a-batch-file/193578/7 "2003-08-07T14:20:04Z")

</div>

Well, it’s been a few years since I’ve authored batch files (I miss that, ya know), but if pkzip is at the root of C, I think, and has a .exe file extention, there should be no need to use the call command.

---

<div class="post-metadata">

### Author: ![NoGoodNamesLeft](https://avatars.discourse-cdn.com/v4/letter/n/8797f3/32.png) [@NoGoodNamesLeft](https://boards.straightdope.com/u/NoGoodNamesLeft)
#### Post date: [August 7, 2003, 2:23pm UTC](https://boards.straightdope.com/t/anybody-want-to-help-me-write-a-batch-file/193578/8 "2003-08-07T14:23:15Z")

</div>

Thanks everybody, I haveb’t time to do this today, but I’m a bit relieved now…

> [@](#):
>
> If you’re going to make a backup I don’t see why you need to copy the file. Just make the zip version.

I need a backup because every week I create a new file. I want to keep the previous week’s file for one week. So, each time I rename the file as ‘backup.zip’, it’s actually the previous week’s data. Then, I create the new zip from the .stv file.

So at any given time I have the current week data, and a copy of last week’s data on file. (In case it gets corrupted or lost by the bean heads I FTP this stuff to).

---

<div class="post-metadata">

### Author: ![pjd](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/pjd/32/3009_2.png) [@pjd](https://boards.straightdope.com/u/pjd)
#### Post date: [August 7, 2003, 3:15pm UTC](https://boards.straightdope.com/t/anybody-want-to-help-me-write-a-batch-file/193578/9 "2003-08-07T15:15:43Z")

</div>

the following batch file code will copy the supplied file  
to a file with the date as a prefix :-

[QUOTE]

@echo off  
set $tok=1-3  
for /f "tokens=1 delims=.:/-, " %%u in (‘date /t’) do set $d1=%%u  
if “%$d1:~0,1%” GTR “9” set $tok=2-4  
for /f "tokens=%$tok% delims=.:/-, " %%u in (‘date /t’) do ( set dd=%%u  
set mm=%%v  
set yyyy=%%w)

copy %1 %dd%%mm%%yyyy%%1  
for %%u in ($tok $d1 dd mm yyyy) do set %%u=  
[\QUOTE]  
If you save that as a file called (for example) save\_as\_date.bat  
and call it with the following in your batch program :

save\_as\_date file.zip

it will create a file called 07082003file.zip

As it is, it leaves the original file so you can just delete that  
or overwrite it.

---

<div class="post-metadata">

### Author: ![pjd](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/pjd/32/3009_2.png) [@pjd](https://boards.straightdope.com/u/pjd)
#### Post date: [August 7, 2003, 3:20pm UTC](https://boards.straightdope.com/t/anybody-want-to-help-me-write-a-batch-file/193578/10 "2003-08-07T15:20:55Z")

</div>

bloody forum. why can’t i edit my last post ?!

Just ignore the

> [@](#):
>
> &

in the above post

---

<div class="post-metadata">

### Author: ![pjd](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/pjd/32/3009_2.png) [@pjd](https://boards.straightdope.com/u/pjd)
#### Post date: [August 7, 2003, 3:22pm UTC](https://boards.straightdope.com/t/anybody-want-to-help-me-write-a-batch-file/193578/11 "2003-08-07T15:22:19Z")

</div>

AAAARRRRRRRRRRRRRRRRRRGGGGGGGGGGGGGHHHHHHHHH.

HOW COME IT WORKED IN THAT LAST POST BUT NOT  
THE PREVIOUS ONE ?!?!?!?!?!?!!

---

<div class="post-metadata">

### Author: ![pjd](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/pjd/32/3009_2.png) [@pjd](https://boards.straightdope.com/u/pjd)
#### Post date: [August 7, 2003, 3:25pm UTC](https://boards.straightdope.com/t/anybody-want-to-help-me-write-a-batch-file/193578/12 "2003-08-07T15:25:22Z")

</div>

OK, i had a \ where i should have had a /

still doesn’t explain why i can’t edit my posts though …

---

<div class="post-metadata">

### Author: ![NoGoodNamesLeft](https://avatars.discourse-cdn.com/v4/letter/n/8797f3/32.png) [@NoGoodNamesLeft](https://boards.straightdope.com/u/NoGoodNamesLeft)
#### Post date: [August 7, 2003, 3:32pm UTC](https://boards.straightdope.com/t/anybody-want-to-help-me-write-a-batch-file/193578/13 "2003-08-07T15:32:00Z")

</div>

Thanks. The edit option isn’t available. Lots of previous posts about it…I thought there was a sticky in ‘About this board’ about it, but i can’t find it.

Basically, it’s to avoid people flaming off, then going back and changing their OPs so they don’t look like an idiot.

Thanks again for that code!
