Create a text file (copied.txt) which saves the name of the copied files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Crazylife87
Posts: 3
Joined: 24 Jan 2021 02:45

Create a text file (copied.txt) which saves the name of the copied files

#1 Post by Crazylife87 » 24 Jan 2021 03:02

Good morning,
I should copy the files from directory A to directory B.
But the files of directory B will be removed after some time.
I only need to copy the new files from directory A that have not yet been copied to directory B.
So I should create a text file (copied.txt) which saves the name of the copied files.
At the end it has to copy in directory B the files of directory A that the name are not in the list of copied files (copied.txt)

aGerman
Expert
Posts: 4196
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Create a text file (copied.txt) which saves the name of the copied files

#2 Post by aGerman » 24 Jan 2021 04:45

This is a terrible approach. The log file will grow and searching will take longer and longer. Why don't you move files into an archive folder once they have been copied?

Steffen

Crazylife87
Posts: 3
Joined: 24 Jan 2021 02:45

Re: Create a text file (copied.txt) which saves the name of the copied files

#3 Post by Crazylife87 » 24 Jan 2021 09:01

So what could be a possible alternative to solve this problem?
As explained previously, the files in directory B will be removed after a certain time. (.Pdf)
So you should be sure to copy only new files from directory A that have never been copied to directory B.

Thanks

aGerman
Expert
Posts: 4196
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Create a text file (copied.txt) which saves the name of the copied files

#4 Post by aGerman » 24 Jan 2021 09:10

Crazylife87 wrote:
24 Jan 2021 09:01
So what could be a possible alternative to solve this problem?
Well, I already told you an alternative. And you didn't answer my question yet.
aGerman wrote:
24 Jan 2021 04:45
Why don't you move files into an archive folder once they have been copied?
Steffen

miskox
Posts: 397
Joined: 28 Jun 2010 03:46

Re: Create a text file (copied.txt) which saves the name of the copied files

#5 Post by miskox » 25 Jan 2021 03:27

Code: Select all

@crazylife:

I only need to copy the new files from directory A that have not yet been copied to directory B.
Can you just skip the files that are already in directory B (do not overwrite)?

Or am I missing something.

Saso

aGerman
Expert
Posts: 4196
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Create a text file (copied.txt) which saves the name of the copied files

#6 Post by aGerman » 25 Jan 2021 06:41

The files will be deleted from folder B, which means they will be copied again. My proposal is to move the files from folder A once they have been copied (e.g. into A\archive or something). The question is whether there is something which prevents to move the files. As an example - the OP isn't authorized, or other users still rely on finding the files in folder A.
In those cases there would be a possibility to do both the logging and the exclusion using a Robocopy job. However, that shouldn't be the preferred solution from my perspective.

Setffen

miskox
Posts: 397
Joined: 28 Jun 2010 03:46

Re: Create a text file (copied.txt) which saves the name of the copied files

#7 Post by miskox » 25 Jan 2021 07:14

Yes, now I see (first I only thought that *some* files will be removed):

Code: Select all

But the files of directory B will be removed after some time.
I only need to copy the new files from directory A that have not yet been copied to directory B.
As you proposed: move files to archive folder and then copy files from folder A to folder B based on their presence in the archive folder (if not exist archive\%%f copy folder_A\%%f folder_B\).

Saso

Crazylife87
Posts: 3
Joined: 24 Jan 2021 02:45

Re: Create a text file (copied.txt) which saves the name of the copied files

#8 Post by Crazylife87 » 25 Jan 2021 07:39

Good morning!

Update:
Basically I should copy the .pdf files from directory A to directory B.
The .pdf files of directory B will be transformed into .csv files (the .pdf one is deleted).

Practically in directory B the file name remains the same with .csv extension.
So I should make a copy based solely on the file name.

Thanks again everyone

aGerman
Expert
Posts: 4196
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Create a text file (copied.txt) which saves the name of the copied files

#9 Post by aGerman » 25 Jan 2021 08:42

My thinking:
1) copy files *.pdf from A to B
2) move files *.pdf from A to A\archive

Next time all pdf files in A are new files because the old files are moved into the archive folder.

Steffen

aGerman
Expert
Posts: 4196
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Create a text file (copied.txt) which saves the name of the copied files

#10 Post by aGerman » 25 Jan 2021 13:39

OK, found some time to write 2 examples.

If I was you, that's what should be preferred. Although you never gave any feedback if this makes sense to you, or if this is something which isn't possible for whatever reason.

Code: Select all

@echo off &setlocal DisableDelayedExpansion

REM create the archive folder if it doesn't exist
if not exist "A\archive\" md "A\archive"

REM pick up file by file
for %%i in ("A\*.pdf") do (
  REM check if the file size is greater than 0 to mitigate the risk of copying files which are still about to get written
  if %%~zi geq 1 (
    REM copy the file to B, then move the file into archive if the copying was successful
    >nul copy "%%~i" "B\" &&>nul move "%%~i" "A\archive\"
  )
)

pause

Following you original request, robocopy would probably be the tool of choice.

Code: Select all

@echo off &setlocal DisableDelayedExpansion
set "logfile=copied.txt"
set "jobfile=rcpy.rcj"

setlocal EnableDelayedExpansion

REM create a job file for robocopy (run ROBOCOPY /? and see what these options are for)
>"!jobfile!" (
  echo /xx
  echo /xj
  echo /min:1
  echo /r:1
  echo /w:1
  echo /ns
  echo /nc
  echo /ndl
  echo /np
  echo /njh
  echo /njs
  echo /log+:!logfile!
  echo /xf
)

REM append names of all files that have been already copied to the /xf option in the job file
>nul copy /b "!jobfile!" + "!logfile!"

REM execute robocopy
robocopy "A" "B" *.pdf /job:"!jobfile!"

REM remove the job file
del "!jobfile!"

REM sort the log file and remove blank lines which have been written for every new job (Win 10 only)
sort /uniq "!logfile!" /o "!logfile!"

pause
Steffen

Post Reply