Link to home
Start Free TrialLog in
Avatar of Luis Diaz
Luis DiazFlag for Colombia

asked on

Batch: remove directories & delete files based on a csv file

Hello experts,
I am looking for two batches that
1-delete /f based on a csv file.
2-rmdir /q /s based on a csv file.

-Full path of files to delete and remove directory will be reported in column a
-Two different batches or CMD and CSV files one for rmdir.csv and the other del.csv.

Logs files should be generated in order to check what has been removed.
Rmdir just remove reported folder and not parent folder.
/S option should be set up to remove subdirectories of reported folders.
 If you have questions please contact me.
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Luis Diaz

ASKER

Thank you very much for this proposal Bill.
BAT should be located in the same folders as rmdir.csv and del.csv  files?
If I want to report full path of rmdir.csv and del.csv should I change variables?

Thank you.
Tested both bat and they work. Thank you very much for your help!
Bill, just a small remark. I noticed when I applied this in my vm which use UNC \\path procedure doesn't applied properly. However When I perform the test in C:\ it works perfectly.
Is there a way to put a workaround for \\path such as pushd & popd?

Thank you very much for your help.
Avatar of Bill Prew
Bill Prew

Can you share the code with the UNC based paths in it, that should work...


»bp
Unable to perform the test right now I will keep you inform.
Thank you again for your help.
Bill, please find attached UNC message related to UNC.
User generated imageMessage generated when I launch the following script:

@echo off
setlocal
set ListFile=del.csv
set LogFile=del.log

if exist "%LogFile%" del "%LogFIle%"

for /f "usebackq tokens=*" %%A in ("%ListFile%") do (
    echo Deleting file: "%%~A">>"%LogFile%"
    del /f "%%~A"
)

Pause

Open in new window


Is there a workaround through pushd or cd commands?
Thank you very much for your help.
Hello Bill,
I was able to get the solution at: https://stackoverflow.com/questions/9013941/how-to-run-batch-file-from-network-share-without-unc-path-are-not-supported-me
(Comment 7)

The following works for me:

@echo off
setlocal

cls
@pushd %~dp0

set ListFile=del.csv
set LogFile=del.log

if exist "%LogFile%" del "%LogFIle%"

for /f "usebackq tokens=*" %%A in ("%ListFile%") do (
    echo Deleting file: "%%~A">>"%LogFile%"
    del /f "%%~A"
)

@popd

Pause

Open in new window


Could you please edit accepted solution by adding

cls
@pushd %~dp0
:::::::::::::::::::

:::::::::::::::::::
@popd

Open in new window


Thank you very much for your help.
I really hate using PUSHD and POPD for things like this, far too many things can go wrong, and it maps a virtual drive with a drive letter you have no control over.

If you fully qualify the ListFile and LogFile SET commands to reference the full path, and then use a local directory as the working directory when you launch the BAT script you can avoid the error.


»bp
Noted.
Thank you very much for your help.