How to Copy Files

How to copy file from one location to another location?

You can use this (or any variant):

Files.copy(src, dst, StandardCopyOption.REPLACE_EXISTING);

Also, I'd recommend using File.separator or / instead of \\ to make it compliant across multiple OS, question/answer on this available here.

Since you're not sure how to temporarily store files, take a look at ArrayList:

List<File> files = new ArrayList();
files.add(foundFile);

To move a List of files into a single directory:

List<File> files = ...;
String path = "C:/destination/";
for(File file : files) {
Files.copy(file.toPath(),
(new File(path + file.getName())).toPath(),
StandardCopyOption.REPLACE_EXISTING);
}

How to copy selected files from one folder to another in Batch file?

I used call for 2nd command as well

call "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvarsall.bat" x86_amd64 
call fastddsgen.bat -replace -example CMake aac.idl
xcopy /y /d C:\A\afile.h C:\B
xcopy /y /d C:\A\afile.cpp C:\B
xcopy /y /d C:\A\bfile.h C:\B
xcopy /y /d C:\A\bfile.cpp C:\B

Reason to use CALL --> When not using CALL, the current batch file stops and the called batch file starts executing.

how to copy files with extension .txt?

OS.walk() generates the file names in a directory tree by walking the tree either top-down or bottom-up. For each directory in the tree rooted at the directory top (including top itself), it yields a 3-tuple (dirpath, dirnames, filenames).

you can simply use this:

import os
from datetime import date
from shutil import copyfile


date_backup = date.today()
str_date_backup = str(date_backup).replace('-','.')

path_input = 'D:\\2 PERSONAL'
for root, dirs, files in os.walk(path_input):#dir_path
for file in files:
# change the extension from '.txt' to
# the one of your choice.
if file.endswith('.txt'):
path_current = root+'/'+str(file)
path_out = r'D:\Backup' + '\\' + str_date_backup + " - " + str(file)
copyfile(path_current, path_out)

How to copy files based on their name in Powershell?

You can keep things simple by using wildcards in the Copy-Item command:

$files=Get-Content nametofind.txt
ForEach($file in $files){
Copy-Item "source\*$file*.xml" -Destination "selected"
}

use the -whatif flag for the Copy-Item command to verify that what would occur is the desired behavior.

Powershell - Copy files only from the current day and if they don't exist in the target directory

Using just PowerShell, you could do this:

$SourceDirItemsMITEMAvail = "\\wipfs02\Data\webshop\test\ecomcloud_import\carhartt\product"
$SourceDirCaproBPRIC = "\\wipfs02\Data\webshop\test\hybris_import\carhartt\product"
$TargetDirB2B = "\\wipfs02\Data\webshop\test\ecomcloud_import\carhartt\b2b\product"
$ExcludeSubDir = "archive", "IDISC Test", "save", "archive_B2B", "archive_tmp", "Errors"

# create a regex of the folders to exclude
# each folder will be Regex Escaped and joined together with the OR symbol '|'
$notThese = ($ExcludeSubDir | ForEach-Object { [Regex]::Escape($_) }) -join '|'

# Copy only the specific files
##############################
# if all files are .txt files, change the patterns below to include that:
# like "avail*.txt", "items*.txt", "MITEM*.txt","BPRIC*.txt", "CAPRO*.txt"
##############################
$FileNamePattern = "avail*", "items*", "MITEM*","BPRIC*", "CAPRO*"

# Get the current date
$today = (Get-Date).Date

Get-ChildItem -Path $SourceDirItemsMITEMAvail, $SourceDirCaproBPRIC -Include $FileNamePattern -File -Recurse |
Where-Object { $_.DirectoryName -notmatch $notThese -and $_.LastWriteTime.Date -eq $today } |
ForEach-Object {
$targetFile = Join-Path -Path $TargetDirB2B -ChildPath $_.Name
if (Test-Path -Path $targetFile -PathType Leaf) {
Write-Host "File '$targetFile' already exists. Skipping"
}
else {
$_ | Copy-Item -Destination $TargetDirB2B -Force
}
}

Copy files with specific text at the end of their (file) names from one folder to other folder

you are trying to copy files to a single filename again and again, so to fix it you will have to add the filename with outpath for every file

import os
import shutil
path = "D:/Snow_new/test"
outpath = "D:/Snow_new/testout"

if not os.path.isdir(outpath):
os.mkdir(outpath)
else:
shutil.rmtree(outpath)
os.mkdir(outpath)

for f in os.listdir(path):
f2, ext = os.path.splitext(f)
if f2.endswith("clip_2"):
shutil.copyfile(os.path.join(path, f), os.path.join(outpath, f))

before running the above code make sure that you remove D:/Snow_new/testout completly

Copy files of different formats in different folders based using Azure Data Factory

If you only want to copy files, there is no need to use different format. You can just use Binary format. Something like this:

Step:

1.Use Get Metadata to extract childitems
Sample Image

2.Use For each activity that contains a Copy activity copy to different folder.

expression:@activity('Get Metadata1').output.childItems
Sample Image

Copy activity source:
Sample Image

Source dataset:
Sample Image

Copy activity sink:
Sample Image

Sink dataset:

expression:@concat(split(item().name,'.')[1],'/',split(split(item().name,'_')[1],'-')[0],'/',split(split(item().name,'_')[1],'-')[1],'/',split(split(split(item().name,'_')[1],'-')[2],'.')[0])(this works for your eg file name: addresses_2020-11-01.csv)
Sample Image

Files in Source folder:

Sample Image

Result:

Sample Image



Related Topics



Leave a reply



Submit