Solved

.bat sintax

Posted on 2012-03-21
19
533 Views
Last Modified: 2012-03-21
hi
i need to made a .bat file that do a rename if find a file, if dont, do the opositive thing
somthing like this
g:
cd proyectos
cd 1008
cd psds
cd isoextractor
cd config

if  exist pipo_tuberia.ini

ren pipo.ini pipo_electrico.ini
ren pipo_tuberia.ini pipo.ini
pause
0
Comment
Question by:edo60
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 4
  • +2
19 Comments
 
LVL 7

Expert Comment

by:micropc1
ID: 37749530
.
0
 
LVL 8

Expert Comment

by:d0ughb0y
ID: 37749559
Let me make sure I understand what you're trying to do:

You're switching to the G: drive, and then changing to the g:\proyectos\1008\psds\isoextractor\config directory. Then you're looking for a file called pipo_tuberia.ini. If it exists, you want to rename pipo.ini as pipo_electrico.ini, and then rename pipo_tuberia.ini as pipo.ini. But if pipo_tuberia.ini doesn't exist, you want it to skip the whole thing. Right?

Batch file syntax isn't quite as structured as some others, like PowerShell or VBScript. To do what I think you're trying to do, you need to do something like this:

G:
cd proyectos\1008\psds\isoextractor\config

if exist pipo_tuberia.ini goto CHANGEFILES
goto END

:CHANGEFILES
ren pipo.ini pipo_electrico.ini
ren pipo_tuberia.ini pipo.ini

:END
pause
0
 
LVL 7

Expert Comment

by:micropc1
ID: 37749583
Yeah, I posted something, but then i read it again and I was a little confused... by 'opositive thing' do you mean 'opposite thing'? something like...

g:
cd g:\proyectos\1008\psds\isoextractor\config

if not exist pipo_tuberia.ini goto notexist

ren pipo.ini pipo_electrico.ini
ren pipo_tuberia.ini pipo.ini

goto end

:notexist
ren pipo_electrico.ini pipo.ini 
ren pipo.ini pipo_tuberia.ini 

:end
pause

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:edo60
ID: 37749590
yes and if find pipo_electrico what to do?
i mean viceversa
i need if find pipo_electrico.ini convert it in pipo.ini

tsm
0
 
LVL 8

Expert Comment

by:d0ughb0y
ID: 37749594
Then do what micropc1 suggested. Same thing, he just added the code to switch them the other way - I didn't because I wasn't sure that's what you wanted to do.
0
 
LVL 7

Expert Comment

by:micropc1
ID: 37749613
actually i think i had the second 'ren's backwards. I'm thinking it should be....

g:
cd g:\proyectos\1008\psds\isoextractor\config

if not exist pipo_tuberia.ini goto notexist

ren pipo.ini pipo_electrico.ini
ren pipo_tuberia.ini pipo.ini

goto end

:notexist
ren pipo.ini pipo_tuberia.ini 
ren pipo_electrico.ini pipo.ini 


:end
pause

Open in new window

0
 
LVL 8

Expert Comment

by:d0ughb0y
ID: 37749685
Yes. That looks correct.
0
 
LVL 43

Accepted Solution

by:
Steve Knight earned 500 total points
ID: 37749790
And you can simplify / complicate a little more with using some more of the batch syntax:

This will look for pip_tuberia.ini and if it exists then rename current pipo.ini to pipo.electrico.ini, then rename pip_tuberia. to pipo.ini

Otherwise it checks for pipo.electrico.ini and if THAT exists then it renames pipo.ini to pipo_tuberia.ini,  then renames pipo.electrico.ini to pipo.ini

Otherwise it just says "no rename done"

Steve

@ECHO OFF
CD /d G:\proyectos\1008\psds\isoextractor\config

IF EXIST pipo_tuberia.ini (
   RENAME pipo.ini pipo_electrico.ini
   RENAME pipo_tuberia.ini pipo.ini
   ECHO File pipo_tuberia.ini renamed
) ELSE (
   IF EXIST pipo_electrico.ini (
      RENAME pipo.ini pipo_tuberia.ini pipo.ini
      RENAME pipo_electrico.ini pipo.ini pipo_electrico.ini
      ECHO File pipo_electrico.ini renamed
    ) ELSE (
      ECHO Files NOT renamed as neither exists
    )
)
PAUSE

Open in new window

0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37749888
This doesn't make a lot of sense at all.


Okay, am I reading this right...

    if pipo_tuberia.ini exists then
        rename pipo.ini to pipo_electrico.ini and,
        rename pipo_tuberia.ini to pipo.ini
    otherwise if pipo_tuberia.ini does NOT exist then
        what do we do here i.e, what do we rename to what?
    end-if
0
 
LVL 8

Expert Comment

by:d0ughb0y
ID: 37749958
Cool! I didn't realize you could do all that () stuff in a batch file. I tested that out and it works nicely.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 37750092
Paul - I assume he has two INI files which he swaps and renames to pipo.ini for an application that expects that file.

Could keep two copies and copy over the main one if they were fixed but if they are dynamically written to by the app. then best off renaming them back and forwards if the application doesn't allow a way of doing it differently I guess.

Steve
0
 

Author Comment

by:edo60
ID: 37750115
withc one you test??
please
0
 

Author Comment

by:edo60
ID: 37750145
oohh man
think you got it
so genious
let me test
0
 
LVL 8

Expert Comment

by:d0ughb0y
ID: 37750146
I think that all you're asking for, edo60, is what micropc1 put up there last. That'd work just fine. The others are providing solutions that are more elegant and flexible, and they're cool. They check for other things, provide for contingencies, etc. But you said that all you wanted was a script that would:

Check to see whether pipo_tuberia.ini exists
If it does, change pipo.ini to pipo_electrico.ini, and then change pipo_tuberia.ini to pipo.ini.
If it doesn't, change pipo.ini to pipo_tuberia.ini, and then pipo_electrico.ini to pipo.ini.

micropc1's script does that, and it's simple.
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37750152
dragon-it

I leave it in your capable hands :)
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 37750169
Mine has a copy/paste error on line 10/11 which should be:

RENAME pipo.ini pipo_tuberia.ini
RENAME pipo_electrico.ini pipo.ini

But as has been said is just a different layout of the logic already put my micropc1 so go with what you feel comfortable reading.
0
 

Author Closing Comment

by:edo60
ID: 37750175
regards to all
tsm
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 37750330
Thankyou, perhaps splitting the points would have been fairer with the work the others did first/
0
 

Author Comment

by:edo60
ID: 37750396
Oh man,
sorry!!
Im in a hurry and cant' think in about it, but for the next im going to take on caunt,
Thank you a lot
that code do exactly what im looking for
so genious
regards
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

A short article about problems I had with the new location API and permissions in Marshmallow
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

726 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