.bat sintax

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
ErnestoAsked:
Who is Participating?
 
Steve KnightConnect With a Mentor IT ConsultancyCommented:
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
 
micropc1Commented:
.
0
 
d0ughb0yPresident / CEOCommented:
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
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
micropc1Commented:
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
 
ErnestoAuthor Commented:
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
 
d0ughb0yPresident / CEOCommented:
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
 
micropc1Commented:
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
 
d0ughb0yPresident / CEOCommented:
Yes. That looks correct.
0
 
Paul TomasiCommented:
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
 
d0ughb0yPresident / CEOCommented:
Cool! I didn't realize you could do all that () stuff in a batch file. I tested that out and it works nicely.
0
 
Steve KnightIT ConsultancyCommented:
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
 
ErnestoAuthor Commented:
withc one you test??
please
0
 
ErnestoAuthor Commented:
oohh man
think you got it
so genious
let me test
0
 
d0ughb0yPresident / CEOCommented:
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
 
Paul TomasiCommented:
dragon-it

I leave it in your capable hands :)
0
 
Steve KnightIT ConsultancyCommented:
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
 
ErnestoAuthor Commented:
regards to all
tsm
0
 
Steve KnightIT ConsultancyCommented:
Thankyou, perhaps splitting the points would have been fairer with the work the others did first/
0
 
ErnestoAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.