JCSAPH
JCSAPH

Reputation: 13

Create a Batch to delete a file using the name of another file

I have a folder which contains only one file (file name and extension will be different every time the batch will run), I need to delete the file from another folder that have the same name as file from the first folder.

Example:

Folder1 have only one file called: 123456.123

Folder2 have lots of files including 123456.123

Locations: c:\Test\Folder1\

c:\Test\Folder2\


Any Help is Appreciated. Thanks in advance

Upvotes: 1

Views: 28

Answers (2)

MC ND
MC ND

Reputation: 70923

for %%a in ("c:\test\folder1\*") do del /q/f/a "c:\test\folder2\%%~nxa" 2>nul 

Upvotes: 1

Monacraft
Monacraft

Reputation: 6630

Easy:

:: Put path to mother folder below
cd "C:\test\"
:: Put Folder1's name below
pushd "Folder1"

for "delims=" /f %%a in ('dir /b *.*') do (set name=%%a)
popd
:: Put Folder2's Name below
pushd "Folder2"

:: Remove "Echo" from below line to delete the file
Echo del %name%
popd

And that will do what you want. Notice the Echo in front of del %name% will prevent it from actually deleting the files. This is only for testing it. If there are no problems, remove the `Echo and it will function fine.

Mona

Upvotes: 0

Related Questions