MBZ
MBZ

Reputation: 27592

Copy and then remove files

I want to create a simple batch script which:

  1. Copies all the files inside folder A to the current directory (which also contains some files)
  2. Runs some commands
  3. Remove all the copied files

An straight-forward solution is to loop through all the files inside A, copy them and keep a list of them. Then clean-up at the end.

But I'm wondering if there is a better solution.

Upvotes: 0

Views: 96

Answers (4)

foxidrive
foxidrive

Reputation: 41234

@echo off
copy "c:\A\*.*" .
rem run commands
for %%a in ("c:\a\*.*") do del "%%~nxa"

Upvotes: 1

Aacini
Aacini

Reputation: 67216

@echo off
attrib +R *.*
copy \A\*.*
rem run commands
del *.*
attrib -R *.*

Of course, this method works only if the commands does NOT modify anyone of the original files, but it is faster and also prevents to overwrite anyone of the original files.

Upvotes: 0

Endoro
Endoro

Reputation: 37569

@echo off &setlocal
set "folder=%userprofile%\A"
set "dirlist=my dirlist.txt"

dir /b "%folder%" > "%dirlist%"
copy "%folder%"

rem doit here

for /f "usebackq delims=" %%a in ("%dirlist%") do erase "%%~a"

It belongs to you to care for duplicate files.

Upvotes: 0

Blorgbeard
Blorgbeard

Reputation: 103467

Save a list of the files, copy them, do your stuff, then delete using your list:

dir /b A >list.txt
copy A\*.* .

rem do stuff here

for /F "delims=" %%i in (list.txt) do del %%i
del list.txt

Upvotes: 0

Related Questions