0

I need to add in a conditional statement to check for a folder so that it can exexute the below script. Lets say the script is placed in folder c:/temp/A but I want the script to excute and change the file extensions of files in a another directory d:/xxx/B. If directory B doesnt exists, throw an error and exit. I tried using if else statement but its says no else statement is allowed. Please help me out and let me know how it can be done.

@echo off
setlocal ENABLEDELAYEDEXPANSION
for %%F in (*.*_error* *.*error_*) do ( set "ext=%%~xF"
SET "ext=!ext:_error=!"
ren "%%F" "%%~nF!ext:error_=!"
)

2 Answers 2

1

You can do a condition like this.

IF EXIST "D:\xxx\b\" (goto thescript) else goto theerror

:thescript
#then whatever you want to run here. 
goto :EOF

:theerror
ECHO File does not exist
goto :EOF
Sign up to request clarification or add additional context in comments.

Comments

0
@echo off
setlocal ENABLEDELAYEDEXPANSION

PUSHD d:\xxx\B 2>nul
if errorlevel 1 (
 echo required destination not found
 goto :wherever
)

for %%F in (*.*_error* *.*error_*) do ( set "ext=%%~xF"
SET "ext=!ext:_error=!"
ren "%%F" "%%~nF!ext:error_=!"
)

POPD

Best idea to use the correct character for directoryname-separators. / is used for switches.

5 Comments

once again thanks. But can I get a understanding on why 2> nul is used please?
2>nul sends error messages to the nul device so they are not displayed. 1>nul or >nul sends ordinary messages to the null device. If the PUSHD can't find the directory in question, it fails, sets errorlevel to 1 and sends an error message to stderr, or device 2. 2>nul disposes of the message so it doesn't reach the console.
@echo off setlocal ENABLEDELAYEDEXPANSION PUSHD d:\xxx\B 2>nul if errorlevel 1 ( echo required destination not found goto :wherever ) for %%F in (.error* *.*error*) do ( set "ext=%%~xF" SET "ext=!ext:_error=!" ren "%%F" "%%~nF!ext:error_=!" ) POPD -----> using this script I have done it to redirect the modified file names into a log file name Renamed_logs. In the same script itself can you tell pls tell me how to purge log files for which is more than 30days
Hmm - sounds reasonably easy - but really needs a new question. The "delete after 30 days" has been aswered many times so avoid that in the new title. The issue is 'do you really want 30 days?' or is 'about 30 days' (like say, 28) more representative? Date-calculation is a real pain in batch - can be done, but easier to take a shortcut if the number of days isn't critical.
Then there's more mundane matters - like from what I recall, could these files be created one day and not processed for a few days - will that affect the time? Your file would contain 2 columns - check the date of the file that is in the second column? What precisely is the syntax of the logfile? Show a sample. And need to know your precise date format - for a date like Mar. 2nd - and specify that it's Mar 2nd, not Feb. 3rd. Answers on the back of a plain self-addressed envelope - er, in a new question, please. Costs nothing and saves confusion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.