I have a scanario where I need to copy file called "abc.txt" from its locations which is not known to destination location "C:\Temp".
We need to search "abc.txt" file and then copy the file to "C:\Temp"
Here source location is unknown and only the file name is known which is to be copied. Can we make use of current directory for this? If yes how can we ?
If I have understand the requirements correctly, you need to find a file in the current folder with the name "abc.txt" and, if the file exist
$AllFiles = Get-ChildItem -Recurse -Name "abc.txt"
foreach ($File in $AllFiles)
{
Write-Host $File
Copy-Item -Path $File -Destination "C:\Temp\"
}
If you want to look for the file in the current Folder AND the subfolders, use the "-Recurse" parameter, see Documentation of Get-Childitem:
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-childitem?view=powershell-7
Related
i'm trying to copy file from source to destination any verify if file copied or not.
But the problem is if i make changes inside the source file which was copied earlier then destination file not getting override when i execute the code again. Also i want log file each time files are copied.
Files in folder:- .csv, .log, .png, .html
$source="C:\52DWM93"
$destination="C:\Temp\"
Copy-Item -Path $source -Destination $destination -Force
$ver=(Get-ChildItem -file -path $destination -Recurse).FullName | foreach {get-filehash $_ -Algorithm md5}
If($ver)
{Write-Host "ALL file copied"}
else
{Write-Host "ALL file not copied"}
If you copy directories like this you need the -Recurse switch for Copy-Item. Without it you're not going to copy anything except the directory itself.
You can of course also use Get-ChildItem with whatever filter or Recurse flag you care about and pipe that into Copy-Item.
Use the *-FileCatalog cmdlets for verification.
Here is a section of code from a larger script. The goal is to recurse through a source directory, then copy all the files it finds into a destination directory, sorted into subdirectories by file extension. It works great the first time I run it. If I run it again, instead of overwriting existing files, it fails with this error on each file that already exists in the destination:
Copy-Item : Cannot overwrite the item with itself
I try, whenever possible, to write scripts that are idempotent but I havn't been able to figure this one out. I would prefer not to add a timestamp to the destination file's name; I'd hate to end up with thirty versions of the exact same file. Is there a way to do this without extra logic to check for a file's existance and delete it if it's already there?
## Parameters for source and destination directories.
$Source = "C:\Temp"
$Destination = "C:\Temp\Sorted"
# Build list of files to sort.
$Files = Get-ChildItem -Path $Source -Recurse | Where-Object { !$_.PSIsContainer }
# Copy the files in the list to destination folder, sorted in subfolders by extension.
foreach ($File in $Files) {
$Extension = $File.Extension.Replace(".","")
$ExtDestDir = "$Destination\$Extension"
# Check to see if the folder exists, if not create it
$Exists = Test-Path $ExtDestDir
if (!$Exists) {
# Create the directory because it doesn't exist
New-Item -Path $ExtDestDir -ItemType "Directory" | Out-Null
}
# Copy the file
Write-Host "Copying $File to $ExtDestDir"
Copy-Item -Path $File.FullName -Destination $ExtDestDir -Force
}
$Source = "C:\Temp"
$Destination = "C:\Temp\Sorted"
You are trying to copy files from a source directory to a sub directory of that source directory. The first time it works because that directory is empty. The second time it doesn't because you are enumerating files of that sub directory too and thus attempt to copy files over themselves.
If you really need to copy the files into a sub directory of the source directory, you have to exclude the destination directory from enumeration like this:
$Files = Get-ChildItem -Path $Source -Directory |
Where-Object { $_.FullName -ne $Destination } |
Get-ChildItem -File -Recurse
Using a second Get-ChildItem call at the beginning, which only enumerates first-level directories, is much faster than filtering the output of the Get-ChildItem -Recurse call, which would needlessly process each file of the destination directory.
Good afternoon all,
I'm guessing this is super easy but really annoying for me; I have a text file with a list of files, in the same folders there are LOTS of other files but I only need specific ones.
$Filelocs = get-content "C:\Users\me\Desktop\tomove\Code\locations.txt"
Foreach ($Loc in $Filelocs){xcopy.exe $loc C:\Redacted\output /s }
I figured this would go through the list which is like
"C:\redacted\Policies\IT\Retracted Documents\Policy_Control0.docx"
and then move and create the folder structure in a new place and then copy the file, it doesn't.
Any help would be appreciated.
Thanks
RGE
xcopy can't know the folder structure when you explicitly pass source file path instead of a source directory. In a path like C:\foo\bar\baz.txt the base directory could be any of C:\, C:\foo\ or C:\foo\bar\.
When working with a path list, you have to build the destination directory structure yourself. Resolve paths from text file to relative paths, join with destination directory, create parent directory of file and finally use PowerShell's own Copy-Item command to copy the file.
$Filelocs = Get-Content 'locations.txt'
# Base directory common to all paths specified in "locations.txt"
$CommonInputDir = 'C:\redacted\Policies'
# Where files shall be copied to
$Destination = 'C:\Redacted\output'
# Temporarily change current directory -> base directory for Resolve-Path -Relative
Push-Location $CommonInputDir
Foreach ($Loc in $Filelocs) {
# Resolve input path relative to $CommonInputDir (current directory)
$RelativePath = Resolve-Path $Loc -Relative
# Resolve full target file path and directory
$TargetPath = Join-Path $Destination $RelativePath
$TargetDir = Split-Path $TargetPath -Parent
# Create target dir if not already exists (-Force) because Copy-Item fails
# if directory does not exist.
$null = New-Item $TargetDir -ItemType Directory -Force
# Well, copy the file
Copy-Item -Path $loc -Destination $TargetPath
}
# Restore current directory that has been changed by Push-Location
Pop-Location
Possible improvements, left as an exercise:
Automatically determine common base directory of files specified in "locations.txt". Not trivial but not too difficult.
Make the code exception-safe. Wrap everything between Push-Location and Pop-Location in a try{} block and move Pop-Location into the finally{} block so the current directory will be restored even when a script-terminating error occurs. See about_Try Catch_Finally.
I would like to move all .txt files present in a source directory (including .txt present in subfolders) to a destination directory.
For eg:
Source directory: D:\OFFICE work\robtest\test1
Above directory also contains many subfolders.
Destination directory: D:\OFFICE work\robtest\test2
I would like to move only .txt files in source dir to the above mentioned destination directory, in such a way that only 3 .txt files must be present per sub folder (includes random folder creation) in the destination directory.
Below is the code I tried, but PowerShell says
The filename, directory name, or volume label syntax .(D:\OFFICE work\robtest\test1\testtest\testtest2\
New folder\test01112.txt\ ) is incorrect.
I am not sure why there is extra '/' after the above path in robocopy.
$fileperfolder = 3
$source = "D:\OFFICE work\robtest\test1";
$dest = "D:\OFFICE work\robtest\test2";
$folderName = [System.IO.Path]::GetRandomFileName();
$fileList = Get-ChildItem -Path $source -Filter *.txt -Recurse
Write-Host $fileList
$i = 0;
foreach ($file in $fileList) {
$destiny = $dest + "\" + $folderName
Write-Host $file.FullName;
Write-Host $destiny;
New-Item $destiny -Type Directory -Force
robocopy $file.FullName $destiny /mov
$i++;
if ($i -eq $fileperfolder) {
$folderName = [System.IO.Path]::GetRandomFileName();
$i = 0;
}
}
You're getting the error (and the "directory" in the output) because robocopy expects two folders as the first and second argument, optionally followed by a list of filename specs, but you're providing the path to a file as the source.
From the documentation (emphasis mine):
Syntax
robocopy <Source> <Destination> [<File>[ ...]] [<Options>]
Parameters
<Source> Specifies the path to the source directory.
<Destination> Specifies the path to the destination directory.
<File> Specifies the file or files to be copied. You can use wildcard characters (* or ?), if you want. If the File parameter is not specified, *.* is used as the default value.
<Options> Specifies options to be used with the robocopy command.
Since you're moving one file at a time and apparently don't want to maintain the directory structure I'd say robocopy isn't the right tool for what you're doing anyway.
Replace
robocopy $file.FullName $destiny /mov
with
Move-Item $file.FullName $destiny
and the code should do what you want.
robocopy $file.Directory $destiny $file /mov
solved this issue.
I would like to use a loop for my program that grabs the file names of only the .dll in the folder and subfolders of that directory. It then searches a specified location/path for a .dll with the same file name and if it exists it replaces it. So far my program copies all files from one location to the other and once they are copied I need the above stated to be worked out.
My biggest issue is how do you search by filenames in a loop at a specified location and if it exists, replace it? Below code is locally in random places before I put the correct paths using servers and other drives.
#sets source user can edit path to be more precise
$source = "C:\Users\Public\Music\Sample Music\*"
#sets destination
$1stdest = "C:\Users\User\Music\Sample Music Location"
#copies source to destination
Get-ChildItem $source -recurse | Copy-Item -destination $1stdest
#takes 1stdest and finds only dlls to variable
#not sure if this is right but it takes the .dlls only, can you do that in the foreach()?
Get-ChildItem $1stdest -recurse -include "*.dll"
Here you go, you'll need to edit your paths back in. Also, note that $1stDest was changed to enumerate the list of files in the destination folder.
The logic goes through all of the files in $source, and looks for a match in $1stDest. if it finds some, it stores them in $OverWriteMe. The code then steps through each file to be overwritten and copies it.
As written, it uses -WhatIf, so you'll have a preview of what would happen before running it. If you like what you see, remove the -WhatIf on line 15.
$source = "c:\temp\stack\source\"
#sets destination
$1stdest = get-childitem C:\temp\stack\Dest -Recurse
#copies source to destination
ForEach ($file in (Get-ChildItem $source -recurse) ){
If ($file.BaseName -in $1stdest.BaseName){
$overwriteMe = $1stdest | Where BaseName -eq $file.BaseName
Write-Output "$($file.baseName) already exists # $($overwriteMe.FullName)"
$overwriteMe | ForEach-Object {
copy-item $file.FullName -Destination $overwriteMe.FullName -WhatIf
#End of ForEach $overwriteme
}
#End Of ForEach $file in ...
}
}
OutPut
1 already exists # C:\temp\stack\Dest\1.txt
What if: Performing the operation "Copy File" on target "Item: C:\temp\stack\source\1.txt Destination: C:\temp\stack\Dest\1.txt".
5 already exists # C:\temp\stack\Dest\5.txt
What if: Performing the operation "Copy File" on target "Item: C:\temp\stack\source\5.txt Destination: C:\temp\stack\Dest\5.txt".