Pollob
Pollob

Reputation: 29

Put batch file command DEL into a variable

Suppose I have a batch file extract.bat that in turn calls some other batch files such as parent.bat, operator.bat, contact.bat .... etc. Every batch file that I call from extract.bat is suppose to delete all files from a particular directory using the following command:

DEL "%OUTPUT_PATH%". /Q

Where "%OUTPUT_PATH%" is the directory from which I would delete all files and is different for the batch files parent.bat, operator.bat ... etc. so the variable is set in every batch file as follows:

SET OUTBOUND_PATH=./SAP Outbound Files/
SET OUTPUT_PATH=%OUTBOUND_PATH%Parents/

Obviously putting DEL "%OUTPUT_PATH%". /Q in every batch file will do what I was asking but I was wondering if there's a way to put DEL command in some kind of variable so that only calling that variable from the batch files will delete the files in the specified directory ? That will prevent writing the same code in every file.

I tried to set variable in extract.bat as follows

SET ALL_FILE_DELETE_CMD=DEL "%OUTPUT_PATH%". /Q

and called it in parent.bat as follows:

%ALL_FILE_DELETE_CMD%

But it didn't work. Is there any solution for this kind of scenario ?

If there's any I will appreciate that.

Thanks in advance

Upvotes: 0

Views: 1191

Answers (3)

Aacini
Aacini

Reputation: 67216

If you use setlocal EnableDelayedExpansion in your Batch files, then the solution is pretty simple:

Define ALL_FILE_DELETE_CMD variable in the first Batch file before enable Delayed Expansion and change % by !:

SET ALL_FILE_DELETE_CMD=DEL "!OUTPUT_PATH!". /Q
setlocal EnableDelayedExpansion

That is it! After that, you just use the variable as you shown above (with Delayed Expansion enabled):

setlocal EnableDelayedExpansion

. . . .

%ALL_FILE_DELETE_CMD%

Upvotes: 0

MC ND
MC ND

Reputation: 70923

You code

SET ALL_FILE_DELETE_CMD=DEL "%OUTPUT_PATH%". /Q

When this line is parsed, all variable reads are replaced with their values and then the line is executed. So

  • if %OUTPUT_PATH% has a initial value, it is replaced in the line and all the times you use the %ALL_FILE_DELETE%, the same folder will be deleted

  • if it has no value, there is nothing to replace, resulting in DEL "%OUTPUT_PATH%". /Q

This second case is what you need.

So, now we have

SET "OUTPUT_PATH="
SET "ALL_FILE_DELETE_CMD=DEL "%OUTPUT_PATH%" /Q

or using scape characters in the name of the variable to avoid variable expansion (in batch file, this will not work on command line)

SET "ALL_FILE_DELETE_CMD=DEL "%%OUTPUT_PATH%%" /Q

But now, the line that is executed is %ALL_FILE_DELETE_CMD%. When the parser processes the line, it will be converted into DEL "%OUTPUT_PATH%" /Q and then executed. BUT, %OUTPUT_PATH% will not be replaced with its value. The parser finished its work before it gets replaced. So the command is executed as is, failing to do what you need.

How to correct it?. Force a second pass of the parser. How to do it? Use CALL command.

CALL %ALL_FILE_DELETE_CMD%

The line is parsed as it is read, resulting in CALL DEL "%OUTPUT_PATH%" /Q. Then, when CALL command have to process the line, a second parser round is executed, so the final line executed is DEL "C:\whatever\folder\it\is" /q with %OUTPUT_PATH% replaced to the current value of the variable

Upvotes: 0

basin
basin

Reputation: 4190

SET ALL_FILE_DELETE_CMD=%ComSpec% /C DEL "%%OUTPUT_PATH%%". /Q

Upvotes: 1

Related Questions