Similar Posts

Subscribe
Notify of
13 Answers
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
AntiVegetarier1
7 months ago

chatgpt asked?

You:

Batch file: I want to copy all new files with the name 1234 to another folder from different folders, where already copied files are not to be copied a second time (even if they have been deleted from the target folder)

ChatGPT:

To convert this into a batch file, you can use a combination of xcopy and a tracking mechanism. A list of all already copied files is run so that only new files are copied.

Here is an example of a batch file that converts this:

batch

 @echo off setlocal REM Zielordner festlegen set "targetDir=C:\Pfad\zum\Zielordner" REM Liste der bereits kopierten Dateien set "logFile=C:\Pfad\zum\Zielordner\copied_files.log" REM Suchmuster für die Dateien set "filePattern=1234*" REM Wurzelordner, in dem gesucht wird set "rootDir=C:\Pfad\zum\RootOrdner" REM Sicherstellen, dass die Log-Datei existiert if not exist "%logFile%" echo. > "%logFile%" REM Alle Dateien finden und kopieren for /r "%rootDir%" %%f in (%filePattern%) do ( REM Überprüfen, ob die Datei bereits kopiert wurde findstr /x /c:"%%~ff" "%logFile%" >nul if errorlevel 1 ( REM Datei kopieren echo Kopiere %%~ff nach %targetDir% xcopy "%%~ff" "%targetDir%" /y /i if errorlevel 0 ( REM Erfolgreich kopierte Datei in die Log-Datei schreiben echo %%~ff >> "%logFile%" ) ) else ( echo Datei %%~ff wurde bereits kopiert. ) ) echo Fertig!

Declaration:

targetDir: Specifies the target folder in which the files are to be copied.

logFile: This file stores the paths of the already copied files.

filePattern: The search pattern for the files (eg 1234* for all files that start with 1234).

rootDir: The root folder where the files are searched.

findstr: Checks if the path of the file already exists in the log file.

xcopy: Copy the file to the target folder.

echo %%~ff >> "%logFile%": Inserts the path of the successfully copied file into the log file.

Notes:

Replace the paths (targetDir, logFile, rootDir) and the search pattern (filePattern) with the specific values ​​for your application.

The log file ensures that files that have already been copied are not copied again, even when deleted in the destination folder.

This batch file can now be executed to copy all new files with the name 1234 (or another pattern) from the different folders to the destination folder without creating duplicate copies.

AntiVegetarier1
7 months ago
Reply to  Jugsy

take this part:

 REM Suchmuster für die Dateien set "filePattern=1234*"

by this one:

 REM Suchmuster für die Dateien set "filePattern=1234.mkv"
AntiVegetarier1
7 months ago

you can try this powershell script:

# Quell- und Zielordner definieren
$sourceFolder = "C:\Pfad\Zum\Quellordner"
$destinationFolder = "C:\Pfad\Zum\Zielordner"

# Datei, um bereits kopierte Dateien zu speichern
$logFile = "$destinationFolder\kopierte_dateien.txt"

# Sicherstellen, dass die Log-Datei existiert
if (-not (Test-Path -Path $logFile)) {
    New-Item -ItemType File -Path $logFile -Force
}

# Alle Dateien mit dem Muster 1234*.mkv im Quellordner finden
$files = Get-ChildItem -Path $sourceFolder -Filter "1234*.mkv"

# Bereit kopierte Dateien laden
$copiedFiles = Get-Content -Path $logFile

foreach ($file in $files) {
    $destinationFile = Join-Path -Path $destinationFolder -ChildPath $file.Name

    # Überprüfen, ob die Datei bereits kopiert wurde
    if (-not ($copiedFiles -contains $file.Name)) {
        # Datei kopieren
        Copy-Item -Path $file.FullName -Destination $destinationFile

        # Dateinamen zur Log-Datei hinzufügen
        Add-Content -Path $logFile -Value $file.Name

        Write-Output "Datei $($file.Name) wurde kopiert."
    } else {
        Write-Output "Datei $($file.Name) wurde bereits kopiert, überspringen."
    }
}

do not call .bat but .ps1, right click on it and click on “Run with powershell”.

norbertk62
7 months ago

You seem to be on Win – there would be the means of choosing the command “robocopy”. But he really has a huge list of command line parameters – that’s what you need to find out for your application.

However, you will hardly be able to do that with the “even if they have been deleted from the target folder” because it re-enters both directories on each run. I don’t know another program.

norbertk62
7 months ago
Reply to  norbertk62

What would work naturally – but you would have to get into programming:

  • you take PERL (the CMD or PowerShell might not be so good)
  • which passes through each of the files inand searches them in the (which you need to carry in the program run)
  • if is not yet in , then copy and log in .
  • if in do not copy
  • About that.

that roughly corresponds to the batch file of AntiVegetarier1

jort93
7 months ago

The “even if they were deleted from the target folder” is relatively complex to implement. If I understand that correctly, it would probably have to create a file that contains a list with all the data that were copied once.