Delete file with name and path in other file. bat

Asked

Viewed 585 times

2

I need to develop a batch that has to read the data in a.txt file, and according to the path and name of the file I have in txt a batch has to go to the folder and delete files.

The data I have in this file are results of an sql query. To illustrate what I said:

file_name, file_path
abcd, D:/user/desktop/teste123
efgh, D:/user/desktop/folder789

My batch needs to read the file_name (to know what to delete) and file_path (to know where to delete).

2 answers

3

Using FOR /F

@echo off
for /f "skip=1 tokens=1,2 delims=," %%a in (in.txt) do echo del "%%b/%%a"
Pause

I put the remote echo in front of the command del, to test the output of the command.

See if this okay, then just remove echo in this part echo del "%%b/%%a", being like this:

del "%%b/%%a"

1

• In a looping for using the variables %%i & %%j

Applied in: name path

For the following layout of your file:

abcd, D:/user/desktop/teste123
efgh, D:/user/desktop/folder789

delete path/name = %j/%%i

@echo off & for /f "delims=,tokens=1,2" %%i in ('type query_sql.log')do if /i not "/.%%i"=="/." del /q /f "%%j\%%i"

# You can also use the reply of Operator @Gerhard Barnard to the same question posted on SatackOverFlow /EN, since you want to jump the first line.

for /f "tokens=1,2 delims=," %i in ('more +1 my_file.txt ^| findstr /VI /C:"rows affected"')do del "%%j\%%i"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.