I want to copy file
base.txt
To Other Folders And I want his name change To The Folder Name.
cd c:\Program\Levels
copy-item *.txt c:\Books -force -recurse
Get-ChildItem C:\Books -Filter *.txt -Recurse | Rename-Item -NewName { $_.Directory.Name}
So he's copying *.txt for the folder 'Books' and renaming the file to 'books.txt' but, he did not copy the file to subfolders within 'Books'
I want it to copy the .txt for subfolders in 'Books' and rename the .txt to the folder name.
Obs: the .txt file is unique
So the file base.txt which is presumably some sort of template will be copied to each subfolder in C:\Books? I think I can understand your logic approach but I offer this as a solution instead.
$templateFile = "C:\temp\base.txt"
Get-ChildItem "C:\Books" | Where-Object {$_.PSIsContainer} |ForEach-Object{
$newName = "{0}\{1}.txt" -f $_.FullName, $_.Name
Copy-Item -Path $templateFile -Destination $newName
}
Gather each subdirectory of "C:\Books" and in each of those directories copy the file base.txt to its new location where its name is the name of the parent folder.
Related
The folder named "z:\original" has hundreds of sub-folders containing multiple copies of the same .jpg files. I wanted to copy all .jpg files into a folder named "z:\dump" WITHOUT the folder structure and hopefully overwrite most of the copies. I used
copy-Item -path "Z:\original" -filter "*.jpg" -Destination "Z:\dump" -recurse -verbose
but this recreated the structure with the .jpg files. How can I dump all files into a single folder, using PowerShell?
Use combination of dir/Get-ChildItem and Copy-Item with pipeline.
$_.FullName - Full path to jpg file
$_.Name is file name only
Get-ChildItem Z:\original\*.jpg -Recurse | %{Copy-Item $_.FullName "Z:\dump\$($_.Name)"}
You might need to add -Force to overwrite same files
Hi I have a folder Called "A" and folder "A" has files and sub folders within it. I also have another folder directory called "Exclusion" with some copied files and folders from "A" within it. I'm looking for a Powershell script or Command Line option that will COPY & MOVE all the objects from A that are NOT found in the Exclusion directory to a new folder directory called "Output".
Thanks,
-B
Use Get-ChildItem to get a list of files in your exclusion directory, then take only the names of the files and hold those in an array.
Optionally use New-Item with the -Force parameter to ensure that your output directory exists before sending files there.
Next use Get-ChildItem to iterate through all files in our source (A) directory, use Where-Object and the -notin operator to exclude any files which have the same names as those gathered from your exclusion directory, then use Move-Item to move the files to your destination (Output) directory.
[string[]]$filenamesToExclude = Get-ChildItem -Path 'c:\somewhere\exclusion' -Recurse | Select-Object -ExpandProperty Name
New-Item -Path 'c:\somewhere\output\' -ItemType 'Directory' -Force | Out-Null #ensure the target directory exists / don't output this command's return value to the pipeline
Get-ChildItem -Path 'c:\somewhere\A' -Recurse | Where-Object {$_.Name -notin $filenamesToExclude} | Move-Item -Destination 'c:\somewhere\output\'
My folder C:\Downloads\Files\ has 3 .zip files.
test1.zip
test2.zip
test3.zip
I need to copy these files into same location and append another extension .dat to the file.
The total files in my folder C:\Downloads\Files\ should have 6 files now (3 .zip and 3.dat)
test1.zip
test2.zip
test3.zip
test1.zip.dat
test2.zip.dat
test3.zip.dat
Can anyone help me get this done?
Use the Get-ChildItem cmdlet to retrieve all zip files and copy them using the Copy-Item cmdlet:
Get-ChildItem -Path 'C:\Downloads\Files\' -Filter '*.zip' |
Copy-Item -Destination { "$($_.Name).dat" }
The following copies only files from within the $path directory with extension .dat.
Get-ChildItem -Path $path -File | ForEach-Object {Copy-Item -Path $_.FullName -Destination "$($_.FullName).dat"}
I am trying to copy a whole bunch of files using Powershell, from one directory to another on my computer.
I used Get-ChildItem C:\Users\Tom\Google Drive\My Files\*\Assessment 1\* to identify that this was the path that I wanted to copy too, and I know about Copy-Item, but I want to maintain parts of the path name when copied.
Example:
If I copy from C:\Users\Tom\Google Drive\My Files\Cool Stuff\Assessment 1\*
I want the files to go to a folder that is created called C:\Users\Tom\Archive\Cool Stuff\Assessment 1
Whereas if I copy from C:\Users\Tom\Google Drive\My Files\New Stuff\Assessment 1\*
I want the files to go to a folder that is created called C:\Users\Tom\Archive\New Stuff\Assessment 1
You could use the Get-ChildItem cmdlet to recursively find all Assessment 1 folders within your base directory and then remove the base path using -replace to finally copy the items using the Copy-Item cmdlet:
$baseDir = 'C:\Users\Tom\Google Drive\My Files\'
$destination = 'C:\Users\Tom\Archive\'
Get-ChildItem $baseDir -directory -Filter 'Assessment 1' -Recurse | ForEach-Object {
$newPath = Join-Path $destination ($_.FullName -replace [regex]::Escape($baseDir))
Copy-Item $_.FullName $newPath -Force -Recurse
}
Copy-Item -Path D:\DB-DNLD\ -Filter *.csv -Destination D:\Dump\CSV -Recurse
I used this command, but it copied all .csv files along with existing folder structures, but my intention is just to copy all CSV to new single destination folder excluding source folder structures.
The Copy-Item cmdlet will maintain the tree structure. If you want to flatten the structure, you can locate the items with Get-ChildItem cmdlet then pipe that to a loop where each file it copied to the destinition folder individually.
dir -Path D:\DB-DNLD\ -Filter *.csv -Recurse | ForEach-Object { Copy-Item $_.FullName D:\Dump\CSV }