views:

60

answers:

3

I need help with writing a batch script for

if file newfile.txt exists
then  del "InDesignData.txt"
ren "newfile.txt" "InDesignData.txt"

Thanks in advance,
Joe

+4  A: 
if not exist newfile.txt goto skip
del "InDesignData.txt"
ren "newfile.txt" "InDesignData.txt"
:skip
stacker
Thanks for the fast response!Works Great!
jmituzas
yours was all I needed nothing more
jmituzas
+2  A: 

You can use simple curved brackets (also supporting else!)

@echo off
IF EXIST newfile.txt (
del "InDesignData.txt"
ren "newfile.txt" "InDesignData.txt"
)

With else:

@echo off
IF EXIST newfile.txt (
del "InDesignData.txt"
ren "newfile.txt" "InDesignData.txt"
) else (
echo Making tea
)
Rudu
A: 

Hi Joe,

One way you can do this is to is to have if statements and labels for both error and success and use ECHO to debug it... my example here has to do with directories, but surely you can adapt for you file deletion/creation needs:

    ECHO Cleaning up folders...
    if exist %myDir% goto deleteFolder
    goto createNewDir

    :deleteFolder
    echo Deleting %myDir%
    RMDIR %myDir% /S /Q
    IF ERRORLEVEL 1 GOTO Error


    :createNewDir
    mkdir %myDir%
    IF ERRORLEVEL 1 GOTO Error
    goto Done

    :error
    echo something went wrong
    goto End

   :Done
    echo Success!

   :End

HTH, EB

echobravo