1

I have lots of .csv files in a folder by names like TOP_QUERIES-filename.com.csv, TOP_QUERIES-www.filename.com.csv Is it possible to compare all the file names except TOP_QUERIES-www. to find if TOP_QUERIES-www.filename.com.csv exists then delete it otherwise keep them.

I know its possible in php but i want to get it done using batch file

Thankyou

2
  • you want to delete all files except the ones that have www.?or you want all files deleted? Commented Jan 5, 2015 at 10:17
  • nope ! i have some files which are double with names for example,TOP_QUERIES-filename.com.csv and TOP_QUERIES-www.filename.com.csv . so those double files i want to get rid of where il keep the ones having www in it Commented Jan 5, 2015 at 10:22

1 Answer 1

2
@echo off
    setlocal enableextensions disabledelayedexpansion

    pushd "c:\target\folder" && (
        for /f "tokens=1,* delims=." %%a in ('
            dir /a-d /b "TOP_QUERIES-www.*.csv"
        ') do echo del /q "TOP_QUERIES-%%b" 2>nul
        popd
    )

This changes the current active directory to the one containing the files and for each www prefixed one, tries to delete the corresponding paired file. If it does not exist, the error is discarded. At the end, the current active directory is restored

To detemine the name of the file to delete, a for /f is used to tokenize the file name using dots as delimiters. We request two tokens (tokens=1,*), the first token precedes the first delimiter and is stored in the %%a replaceabe parameter while the second token requested is the rest of the file name and is stored in the %%b (the next one alphabetically) replaceable parameter.

Delete operations are only echoed to console. If the output is correct, remove the echo command before the del

Sign up to request clarification or add additional context in comments.

Comments

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.