Create Account

Username
Password
Remember me
Email
 
1

Batch Script Help Needed (Windows 7)

4 comments, 204 views, posted 10:51 am 12/06/2012 in Requests by mohit_117
mohit_117 has 7170 posts, 2305 threads, 851 points, location: India
Good Man di Lantern

Hi!

I'm trying to create a TOGGLE batch file (.bat) and haven't been able to get it to work perfectly...

I have a file in my folder, test: "abcd.cbca" ... I need to toggle the extension of that file from .cbca to .__cbca.
So whenever I execute the batch, it should :
rename abcd.cbca to abcd.__cbca
Or if it had been renamed yesterday, it should rename it to abcd.cbca
ie
a toggle between the file name whenever the batch file is exeuted.

Here's the code I'm trying out, but it doesn't work...

Code:
@ECHO OFF

CD "E:\test"

IF EXIST abcd.__cbca (
ren abcd.__cbca abcd.cbca
) ELSE (
ren abcd.cbca abcd.__cbca
)

PAUSE
CLS
EXIT


I'm getting an error that the ELSE command is not a dos command...

-Mohit.

Comments

2
11:56 am 12/06/2012

waints

Put the entire if/else on one line and see if it works
IF EXIST abcd.__cbca (ren abcd.__cbca abcd.cbca) ELSE (ren abcd.cbca abcd.__cbca)

1
12:02 pm 12/06/2012

mohit_117

It's worked!!
I had kept the file in E drive, and somehow it wasn't renaming that file...

Just changed the file location to C drive, and it worked!!!

Thanks for the help waints.. I guess the dos mode required to change drive before entering the directory via C: or E: ... and that's why it was showing an error of "file not found"

0
12:18 pm 12/06/2012

mohit_117

Code:
@ECHO OFF

CD "C:\test_data"

IF EXIST abcd.__cbca (ren abcd.__cbca abcd.cbca
ECHO "File was off, now Turned ON") ELSE (ren abcd.cbca abcd.__cbca
ECHO "File was on, now Turned OFF")

PAUSE
CLS
EXIT


EDIT: Working perfectly now...!!

1
3:41 pm 12/06/2012

bytehead

pushd e:\test_data

Changes the subdirectory AND the drive in one go.  And then a popd puts you back where you were at.

Add Comment

Log in via teoti, or register to add a comment!