Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I get started with .bat files? 4

Status
Not open for further replies.

OhioSteve

MIS
Mar 12, 2002
1,352
US
I have inherited an ancient .bat file and I want to ad new features to it. Unfortunately, I know almost nothing about .bat files. Currently, when you click it you get a brief message on the screen and the prompt "press any key to continue." When you press a key, it extracts a dump file from an Oracle db and puts it in the same directory as the .bat file.

I want the .bat file to run without any user interaction. I don't want to get the "press any key" stuff. I have looked at the .bat file in Notepad and I think I can guess how to achieve that. I need to eliminate the lines with ECHO "[text]" and the lines with PAUSE. Is that a good guess?

I also want the .bat file to put the dump in a specific directory. I want it to overwrite the existing file in that directory. I want all of this to happen without any Windows dialogue boxes.

When I have finished my modifications, I plan to put the file on my Windows scheduler. That is why I want to eliminate the user input
 
Hello

Post the code here and I'll have a look at it. I'm sure there must be some batch file tutorials out on the web somewhere, but I'm not aware of any good ones at the moment.

John
 
the /y switch on copy should overwrite the file without prompting.

spend a little time in dos and on google, go to cmd prompt and type help for a quick list of some of the cmd line functions, such as if, replace, echo, etc..

at the cmd prompt, typing the command you think you want to use, followed by /? will give you a lot of info on how to use the command.
i.e. "echo /?" displays:

C:\>echo /?
Displays messages, or turns command-echoing on or off.

ECHO [ON | OFF]
ECHO [message]

Type ECHO without parameters to display the current echo setting.
C:\>

posting up what you have will definitely help us see what you need to do
 
The Microsoft reference was very helpful to me. My .bat file is still in the experimental stage. This is what I have so far:

ECHO OFF
CLS
ECHO.
ECHO A TEST OF A NEW .BAT FILE
ECHO ------------------------------
ECHO.
ECHO THIS IS A TEST OF THE NEW .BAT FILE
ECHO IT SHOULD COPY ABC.TXT FROM ALPHA DIR TO OMEGA DIR
ECHO IT SHOULD OVERWRITE ANY EXISTING FILES
ECHO.
ECHO PRESS [Ctrl]-C TO ABORT THIS TEST
PAUSE
COPY C:\ALPHA\ABC.TXT /y C:\OMEGA\ABC.TXT
ECHO.
ECHO TEST COMPLETE
PAUSE

It seems to work great except for one thing. The Windows task scheduler will NOT run it! My whole reason for creating a .bat file is to automate this process so that I can go on Xmas vacation. Does Scheduler only run .exe files?
 
NO, you can run .bat files.
This file has "pause" and this is reason. Just del/remark those lines and it will run .
 
Igortsin is correct in that removing the pause commands will make it run without user interaction. However, at the moment there are still possibilities of it failing without errors being picked up:

* Target file exists and is read only, or NTFS access permissions prevent write/update in the folder, so copy command can't copy file.
* Source file may not exist.

There's no easy way of picking up access permissions within a batch file, and the cacls command can be used to change them - but only if the current user has administrator permissions or is owner of the folder, so I will leave the first one for the moment as it is more complex.
However, you can sort the second one by doing

Code:
IF EXIST C:\OMEGA\ABC.TXT DEL C:\OMEGA\ABC.TXT
before the copy
and
Code:
FC C:\ALPHA\ABC.TXT C:\OMEGA\ABC.TXT > NUL
IF ERRORLEVEL 0 IF NOT ERRORLEVEL 1 ECHO Copy completed successfully
IF ERRORLEVEL 1 ECHO Two copies of files differ
afterwards

This works by deleting the target file if one exists first, then comparing the two afterwards using the FC (File Compare) command. If they differ, it displays a message.

John
 
Actually, the problem was quite simple. I screwed up in the Windows Scheduler wizard. After a couple more times, I was able to schedule a .bat file run.

Ideally, I would like to run this every morning. I would like to keep a file for each weekday. So the target directory might contain these files:

Tuesday.txt
Wednesday.txt
Thursday.txt
Friday.txt
Saturday.txt

The thing that I'm backing up changes each weekday. The scheduled task will run early each morning. So Tuesday.txt will archive Monday's work. Saturday.txt will archive Friday's work. I want to overwrite files that are older than a week. So Tuesday.txt will overwrite the last Tuesday.txt.

Now how do I tell DOS "If today is Tuesday, call the file 'Tuesday.txt'"?
 
Of course, the answer is so obvious. I will just create five similar .bat files. Then I will use the Scheduler to run each one on a certain weekday. The Scheduler wizard has an option for that. Thanks guys!
 
you can create 7 bat files - each for week day and schedule it
monday.bat
......bat
 
Actually the target folder is on another server. The folder is shared. Currently, I have to provide an additional password when I log onto the primary server. That gives me access to the shared folder.

Now I want the .bat file to run at night, without my intervention. How do I tell DOS to log onto that shared folder?
 
Hi Steve,

use

NET USE z: \\Server\Sharename Password /User:Username

John
 
That worked perfectly! Now, how shut off tbe connection? I tried this command to shut it off:

NET USE E: \\Safety2\Backups /DELETE

I got an error message.
 
It says "helpmsg 3510, a command was used with conflicting switches". The example on the MS website is just like my syntax.

I am SOOO close to completing this!
 
I found thise to be useful myself, however how do I overcome the space in a directory?

I.e.

C:\Video Game
 
It works! It works! No man can remain an atheist after his .bat file runs without choking.
 
Spyder757

Just put the path of the folder inside quote marks. eg

"C:\Video Game\"

John
 
Sweet.

I grew up on MS DOS but years of Windows use has ruined my brain.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top