Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7196543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:47:50+00:00 2026-05-28T20:47:50+00:00

I have a command line which copies files from folder A to folder B:

  • 0

I have a command line which copies files from folder A to folder B:

copy A\* B\

I would now like to delete all files in B that are present in A, non-recursively. I can list the files in A like this:

dir /b /a-d A

With the output being:

f0.txt
f1.txt
f2.txt

Here is the pseudocode for what I would like to do:

foreach $1 in <dir /b /a-d A output>:
  del B\$1

Is there a windows command-line syntax that will execute a command, using the output of another command as an input? I am aware of the piping operator ( | ) but do not know of a way that this could be used to accomplish this task. Any help would be appreciate.

Restriction: Only commands available by default in Windows 7.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T20:47:51+00:00Added an answer on May 28, 2026 at 8:47 pm

    You can iterate over files with

    for %x in (*) do ...
    

    which is also a lot more robust than trying to iterate over the output of a command for this use case.

    So

    for %f in (A\*) do del "B\%~nxf"
    

    or, if you need this in a batch file instead of the command line:

    for %%f in (A\*) do del "B\%%~nxf"
    

    %~nxf returns only the file name and extension of each file since it will be prefixed with A\ and you want to delete it in B.

    Add > nul 2>&1 to suppress any output (error messages may appear when you try deleting files that don’t exist).


    Just for completeness, you can in fact iterate over the output of a command in almost the same way:

    for /f %x in ('some command') do ...
    

    but there are several problems with doing this and in the case of iterating over dir output it’s rarely necessary, hence I don’t recommend it.


    And since you are on Windows 7, you have PowerShell as well:

    Get-ChildItem A\* | ForEach-Object { Remove-Item ('B\' + $_.Name) }
    

    or shorter:

    ls A\* | % { rm B\$($_.Name) }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application which is built from command line (ANT) using J2ME Polish.
I have a project which builds correctly from the command line. On one of
I have a command-line program which takes input from stdin. What's the best way
I have a command-line executable which I need to run from Java on Windows
I have a tiny command line utility which reads user input and adds all
Say I have a command line C program which is currently executing, and I
I have a command line program, which outputs logging to the screen. I want
I have some Ruby code which takes dates on the command line in the
I have a program which runs an external, command line utility and reads the
I have a command line executable that alters some bits in a file that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.