Solved

MS DOS - .bat file.

Posted on 2010-11-12
25
497 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
ID: 34121912
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
ID: 34121927
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 54

Assisted Solution

by:Bill Prew
Bill Prew earned 125 total points
ID: 34121930
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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:chokka
ID: 34121993

In this line

copy /y R:\patient.* .

Which is current directory and which is mapped directory ?

0
 

Author Comment

by:chokka
ID: 34122009


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 54

Expert Comment

by:Bill Prew
ID: 34122030
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 54

Expert Comment

by:Bill Prew
ID: 34122051
==> 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
ID: 34122073
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
ID: 34122080

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
ID: 34122091
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
ID: 34122129
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
ID: 34122145
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
 
LVL 54

Expert Comment

by:Bill Prew
ID: 34122146
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
ID: 34122176
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 54

Expert Comment

by:Bill Prew
ID: 34122178
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
ID: 34122181

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

Author Comment

by:chokka
ID: 34122187
bp done.
thanks
0
 
LVL 3

Assisted Solution

by:kraiven
kraiven earned 250 total points
ID: 34122190
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
ID: 34122221
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
ID: 34122237
kraiven:  I think the delete statements are there to remove .old files from previous script runs.
0
 
LVL 3

Expert Comment

by:kraiven
ID: 34122259
My mistake. Sorry BP
0
 

Author Comment

by:chokka
ID: 34122481
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
ID: 34122485
Perfect, Thank you
0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 34122626
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 54

Expert Comment

by:Bill Prew
ID: 34122646
==> 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Solaris 10.  Nmap installation fails 2 72
how to get the largest file size? 33 61
how to export this list 4 61
Why  my code (program) build with old compiler? 11 79
Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
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…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

820 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