Solved

MS DOS - .bat file.

Posted on 2010-11-12
25
492 Views
Last Modified: 2013-11-22

I have .bat file which i received from my client.

I am little confused on this batch file.
Please help me to understand, where it copies and where it deletes?

:: Batch file that deletes the .dat and .idx files in the current directory and then 

:: copies over the current files from the rx30 server which is mapped to drive R:\



del *.dat

del *.idx



copy /y R:\patient.* .

copy /y R:\artrans.* .

copy /y R:\patdob.* .

Open in new window

0
Comment
Question by:chokka
  • 8
  • 7
  • 5
  • +1
25 Comments
 
LVL 3

Accepted Solution

by:
kraiven earned 250 total points
Comment Utility
Hi,

The first two DEL commands will delete ALL .DAT and .IDX files from the directory (folder) from which the command is executed.

The remainder of the script copies the Patient, artrans and patdob files (presumably a .DAT and .IDX for each) into the current folder.

Assuming your live databases files are in the R drive and you execute this .BAT file from somwhere else (say your local C drive) then it is safe.
However, if you are still concerned I suggest changing the first two DEL statements to backup statements as follows:
 
for %a in (*.DAT) do mv %a %a.BAK
for %a in (*.IDX) do mv %a %a.BAK

Open in new window

This will result in your .DAT and .IDX files being moved to files with the added extension .BAK
i.e.
PATIENTS.DAT -> PATIENTS.DAT.BAK

you can then either delete them or take a copy to some backup media.

Hope that helps
0
 
LVL 6

Assisted Solution

by:ipajones
ipajones earned 125 total points
Comment Utility
All a batch file does is simply process the commands line by line sequentially.  I've no idea what or where your "live" database is saved.  Assuming you do have an R drive mapped as the script suggests this batch script will simply delete all files ending .dat and .idx in the current directory and copy across files from the mapped R drive to the current directory.

If you want to ensure you're not deleting anything why not just modify the batch file to rename the "old" files instead of deleting them.  In the version below I've replaced the delete lines with a rename command:

ren *.dat *_dat.old
ren *.idx *_idx.old

copy /y R:\patient.* .
copy /y R:\artrans.* .
copy /y R:\patdob.* .

You can at least now run the batch file knowing that you're not deleting or over-writing anything.
0
 
LVL 51

Assisted Solution

by:Bill Prew
Bill Prew earned 125 total points
Comment Utility
It looks like the BAT file you showed will remove both the DAT extension files and the IDX extension files from whatever the current directory is set to when the BAT file runs.  So as long as you are not in the directory where the live database lives you should be okay.

It then brings in a fresh copy of all files from the three directories shown on the R: drive, again copying them into the current directory.  The /Y on the copy command tells it to automatically replace any files in the current directory with those from the R: drive.

Does this help, if not ask away.

~bp
0
 

Author Comment

by:chokka
Comment Utility

In this line

copy /y R:\patient.* .

Which is current directory and which is mapped directory ?

0
 

Author Comment

by:chokka
Comment Utility


From the Comment ID: 34121927

del *._dat.old
del *._idx.old

ren *.dat *_dat.old
ren *.idx *_idx.old

Hope this will work ?


0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
A single period always refers to the current directory.

So

copy /y R:\patient.* .

copies files from the R:\patient folder into the current directory (referenced with the period).

~bp
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
==> Hope this will work ?

Yes, that would work, it would remove any .OLD files from prior runs, and then rename the active IDX and DAT files with .OLD.

~bp
0
 
LVL 6

Expert Comment

by:ipajones
Comment Utility
del *._dat.old
del *._idx.old

ren *.dat *_dat.old
ren *.idx *_idx.old

Hope this will work ?

I'm assuming you want to delete any ".old" files created from a previous run of the batch script - in which case yes.  Also you asked about the command "copy /y R:\patient.* ." the "."(period) represents the current working directory.
0
 

Author Comment

by:chokka
Comment Utility

So, i need to rename the directories as per my computer .

In my case, i have C : as my local working folder and S : as Mapped directory ..!

copy /C S:\patient.* .
0
 
LVL 3

Expert Comment

by:kraiven
Comment Utility
chokka,

Hi. The Ren *.dat *_dat.old still needs to be executed within a FOR staement as I gave in my answer. The reason is that the RENAME (and MOVE, either are fine for this)  are not clever enough to determine that the basename for the file should be matched in the renamed file i.e. they only work on one file at a time.
So, if you have PATIENT.DAT and PATDOB.DAT in the folder and try your command you will find that it doesn't work. he FOR statement gets around this by executing the command in the DO part of the statement for each object in the FOR part of the statement.

FOR %a (*.DAT) DO REN %a %a.OLD

expands to

REN PATIENT.DAT PATIENT.DAT.OLD
REN PATDOB.DAT PATDOB.DAT.OLD

when it executes.
0
 

Author Comment

by:chokka
Comment Utility
Little Confused. I am sorry. I am poor in MS DOS Scripts.

My Local working folder is C:

and Mapped Directory is S:

In this scenario, how should i remodify the actual posted script ?
0
 
LVL 6

Expert Comment

by:ipajones
Comment Utility
So, i need to rename the directories as per my computer .

In my case, i have C : as my local working folder and S : as Mapped directory ..!

copy /C S:\patient.* .

NO NO NO!!!!  The copy /y  is a switch that simply tells the copy command to overwrite existing files

However if indeed the mapped drive is S then you're right to substitute 'S' for 'R'

You can check which drive is mapped by entering "net use" at the command prompt.

You don't need to worry about your local drive as this is implicit given the batch script just references the current working drive/directory.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
Hi. The Ren *.dat *_dat.old still needs to be executed within a FOR staement as I gave in my answer. The reason is that the RENAME (and MOVE, either are fine for this)  are not clever enough to determine that the basename for the file should be matched in the renamed file i.e. they only work on one file at a time.
So, if you have PATIENT.DAT and PATDOB.DAT in the folder and try your command you will find that it doesn't work. he FOR statement gets around this by executing the command in the DO part of the statement for each object in the FOR part of the statement.


I have to disagree with that, REN can handle wildcards without problem, no need for a FOR loop.

~bp
0
 
LVL 6

Expert Comment

by:ipajones
Comment Utility
kraiven:

You're complicating things, there is no need for a FOR loop in this case as the RENAME command will just use the wildcard to retain the first part of the original filename.
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
To use S: just do:

:: Batch file that deletes the .dat and .idx files in the current directory and then 
:: copies over the current files from the rx30 server which is mapped to drive R:\

del *._dat.old
del *._idx.old

ren *.dat *_dat.old
ren *.idx *_idx.old

copy /y S:\patient.* .
copy /y S:\artrans.* .
copy /y S:\patdob.* .

Open in new window


~bp
0
 

Author Comment

by:chokka
Comment Utility

I am sorry to say, Please make it simpler.
Please help me by providing simple script.
0
 

Author Comment

by:chokka
Comment Utility
bp done.
thanks
0
 
LVL 3

Assisted Solution

by:kraiven
kraiven earned 250 total points
Comment Utility
Hi chokka,

Firstly apologies, your REN solution will work - although the same doesn't work for the MV command (in which case you do need to use a FOR statement).

as for your last post:

As stated elswhere the "." at the end of the copy command refers to the directory from which you are executing your batch file.
S, if your LIVE data is in the S: drive then you commands will be of the form

COPY /y S:\PATIENT.* .

The /y silently overwrites any exsitng files in the destination folder (i.e your current directory) the S:\PATIENT.* refers to all of the files in the S drive with the basename PATIENT (i.e. PATIENT.DAT and PATIENT.IDX) and the final dot refers to the destination folder (i.e. you current folder)

The Copy command syntax is COPY <Source file path> <Destination file path>
0
 
LVL 3

Expert Comment

by:kraiven
Comment Utility
bp 34122178

Your solution won't achieve what I assume chokka requires. I think you forgot to remove the two delete statements.

Should read:
 
0
 
LVL 6

Expert Comment

by:ipajones
Comment Utility
kraiven:  I think the delete statements are there to remove .old files from previous script runs.
0
 
LVL 3

Expert Comment

by:kraiven
Comment Utility
My mistake. Sorry BP
0
 

Author Comment

by:chokka
Comment Utility
kraiven: You are right

Based on bp 34122178 - Comment, i was not able to copy.

But from the Comment ID: 34122221, I was able to copy.

Thank you for all your help


0
 

Author Closing Comment

by:chokka
Comment Utility
Perfect, Thank you
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
Just curious, the solution you selected used a "MV" command, which I don't think exists in the DOS command set?

If you did change them to DOS move commands, then I suspect you will need /Y on those, since you will be overwriting prior executions BAK files, and don't want to be prompted for that all the time.

Could you post up the script you ended up using, just for future reference?

~bp
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
==> Based on bp 34122178 - Comment, i was not able to copy.

It looks like I had an extra period in there, so these lines:

del *._dat.old
del *._idx.old

should have been:

del *_dat.old
del *_idx.old

~bp
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now