I am trying to move a bunch of files in child directories into the parent directory. I have verified that the individual search works and that my concatenations are correct. However, the Move-Item does.. nothing.
I can't figure out why.
Get-ChildItem $pwd -name -recurse *.docx | foreach{ Move-Item (Join-Path . $_) .}
Any help would be appreciated.
Thanks.
Edit: Updated with the solution specific to my problem based on J.Drexler's explanation/answer.
Get-ChildItem $pwd -name -recurse *.docx | Move-Item -Destination .
It's done like this :
Get-ChildItem $path -Filter "*.docx" | move-item -Destination (split-path $path -Parent )
Note: You don't need a "foreach" after a "|".
If you use "|" after a listing-command, its the same like:
$items = GetList $path
foreach($item in $items){ DoSomething $item }
this is same as:
GetList $path | DoSomething
Related
I need to add some text to a file and remove file to another location after that.
I am using:
Get-ChildItem \\serverpath\tmp -Recurse -Filter *.txt| Foreach-Object{(add-Content $_.FullName -value ';;;')}
Get-ChildItem \\serverpath\tmp -Recurse -Filter *.txt| Foreach-Object{
Move-Item \\serverpath\tmp\$($_.Name) -Destination "\\serverpath\$($_.Name)"}
How to do this without having twice Get-ChildItem sentence? We have process which write data, so I would like to be sure that files are not transfered without adding ;;; at end of files.
You can do both within the same loop:
$destinationPath = '\\serverpath\NewLocation'
Get-ChildItem -Path '\\serverpath\tmp' -Recurse -Filter '*.txt'| Foreach-Object {
$_ | Add-Content -Value ';;;'
$_ | Move-Item -Destination $destinationPath
}
I've looked through various questions on SO but can't get the script to work. New to Powershell and I'm sure this easy - any help would be great
I have a directory structure of 170 folders:
G:\Data\
folder1\
DCIM1\*jpgs
DCIM2\*jpgs
folder2\
DCIM1\*jpgs
DCIM2\*jpgs
I would like to move all the jpgs from each DCIM subfolder into the parent folder one level up:
G:\Data\
folder1\*jpgs
folder2\*jpgs
Each parent folder has a different name, and potentially differently named DCIM subfolders, but all the jpgs are named with their folder prefix (e.g., DCIM1_001.jpg).
I have tried Powershell:
G:\> $files = Get-ChildItem "G:\data"
>> Get-ChildItem $files | Move-Item -Destination { $_.Directory.Parent.FullName }
>> $files | Remove-Item -Recurse
but get a destination is null error. I have tried a wildcard too:
G:\> $files = Get-ChildItem "G:\data\*"
>> Get-ChildItem $files | Move-Item -Destination { $_.Directory.Parent.FullName }
>> $files | Remove-Item -Recurse
But I take it I have that completely wrong. What am I missing?
You can use Split-Path to get the parent directory:
$JPGs = Get-ChildItem -Path "C:\brief\datefolder" -Recurse -Filter "*.jpg"
foreach ($JPG in $JPGs) {
$Parent_Directory = Split-Path -Path $JPG.FullName -Parent
$Destination_Path = Split-Path -Path $Parent_Directory -Parent
Move-Item -Path $JPG.FullName -Destination $Destination_Path
if ($null -eq (Get-ChildItem -Path $Parent_Directory)) {
Remove-Item -Path $Parent_Directory
}
}
It's just a means of assigning it to a variable and moving along the line.
I'm trying to move all items except a certain type of file. In this case *.msg. It does fine if the excluded file resides within the parent folder. However, the moment that same type of file is located within a subdirectory, it fails to leave the file in place and instead moves it to the new location.
username = Get-Content '.\users.txt'
foreach ($un in $username)
{
$destA = "c:\users\$un\redirectedfolders\mydocuments"
$destB = "c:\users\$un\redirectedfolders\desktop"
$sourceA = "C:\users\$un\mydocuments"
$sourceB = "C:\users\$un\desktop"
New-Item -ItemType Directory -Path $destA, $destB
Get-ChildItem $sourceA -Exclude '*.msg' -Recurse | Move-Item -Destination {Join-Path $destA $_.FullName.Substring($sourceA.length)}
Get-ChildItem $sourceB -Exclude '*.msg' -Recurse | Move-Item -Destination {Join-Path $destB $_.FullName.Substring($sourceB.length)}
}
This is due to the filtering done by the Get-ChildItem exclude filter. It's kind of a known issue, and if you really want I could probably dig up some reference documentation, but it may take some time. Regardless, GCI doesn't handle wildcards very well when it comes to filtering things. What you are probably better off doing is piping it to a Where command like this:
$username = Get-Content '.\users.txt'
foreach ($un in $username)
{
$destA = "c:\users\$un\redirectedfolders\documents"
$destB = "c:\users\$un\redirectedfolders\desktop"
$sourceA = "C:\users\$un\documents"
$sourceB = "C:\users\$un\desktop"
New-Item -ItemType Directory -Path $destA, $destB
GCI $sourceA -recurse | ?{$_.Extension -ne ".msg" -and !$_.PSIsContainer} | %{
$CurDest = Join-Path $destA $_.FullName.Substring($sourceA.length)
If(!(Test-Path $CurDest.SubString(0,$CurDest.LastIndexOf("\")))){New-Item -Path $CurDest -ItemType Directory|Out-Null}
$_ | Move-Item -Destination $CurDest
}
GCI $sourceB -recurse | ?{$_.Extension -ne ".msg" -and !$_.PSIsContainer} | %{
$CurDest = Join-Path $destB $_.FullName.Substring($sourceB.length)
If(!(Test-Path $CurDest.SubString(0,$CurDest.LastIndexOf("\")))){New-Item -Path $CurDest -ItemType Directory|Out-Null}
$_ | Move-Item -Destination $CurDest
}
}
Edit: Ok, now excludes folders, and also keeps folder structure.
Edit2: Re-designed to do a ForEach loop on the files, build the destination path as $CurDest, test to make sure it exists and make it if it doesn't, then move the files. Also changed mydocuments to documents which is the path to a user's My Documents folder.
Just wondering if it's possible to have a script move film files to a specific folder based on the alphabet?
Eg Scream 4 would get moved to e:\movies\s\
Avatar would get moved to e:\movies\a\
I start a script that looks like this :
But the result is not good!The script try to create a directory with files name...
$a = new-object -comobject wscript.shell
$b = Get-Location
foreach($file in (dir $b -file -recurse)) {
New-Item -Path $b -Name (Split-Path $file.fullname -Leaf).Replace($file.extension,"") -ItemType Directory -Confirm
Move-Item -Path $file.fullname -Destination "$b\$((Split-Path $file.fullname -Leaf).Replace($file.Extension,''))" -Confirm
}
An idea ?
Many thanks!
Kreg
Destination folders must exist before you run the command:
dir $b -file -recurse | Move-Item -Destination {"e:\movies\$($_.Name[0])"}
This will create the folders at run time:
dir $b -File -Recurse | foreach{
$folder = Join-Path e:\movies $_.Name[0]
md $folder -force | Out-Null
$_ | Move-Item -Destination $folder
}
I need to copy a large number of files to a backup folder but I want to maintain their relative paths. I only need specific files; i.e.
C:\scripts\folder\File.ext1
C:\scripts\folder2\file2.ext2
C:\scripts\file3.ext1
But I only need to copy the ext1 files like so:
C:\backup\folder\File.ext1.bak
C:\backup\file3.ext1.bak
The source paths are of multiple depths.
This is what I have to copy the files:
$files = gci -path C:\scripts\ -recurse -include *.ext1
$files | % { Copy-Item $_ "$($_).bak"; move-item $_ -destination C:\backup\ }
This just dumps all the files into C:\backup\ and does not appear to get any of the paths. Not sure how that part would be done.
Something like this could work:
gci -path C:\scripts\ -recurse -include *.ext1 |
% { Copy-Item $_.FullName "$($_.FullName).bak"
move-item $_.FullName -destination ($_.FullName -replace 'C:\\scripts\\','C:\backup\') }
It is not clever, but it's quick & dirty and works without a lot of effort.
get-childitem returns absolute paths, but you can make them relative to the current working directory as follows:
resolve-path -relative
So to copy a filtered set of files from the current directory recursively to a destination directory:
$dest = "c:\dest"
$filter = "*.txt"
get-childitem -recurse -include $filter | `
where-object { !$_.PSIsContainer } | `
resolve-path -relative | `
% { $destFile = join-path $dest $_; new-item -type f $destFile -force | out-null; copy-item $_ $destFile; get-item $destfile; }
new-item is needed to create the parent directories
get-item provides a display of all the new files it created
Of course robocopy does all this, but there will be times when you want to do more special filtering or filename mangling...
Use robocopy.
robocopy c:\scripts c:\backup *.ext1 /s
Oops. I failed to notice you wanted to add the .bak extension too. I still think it is a good idea to use robocopy to copy the files then:
dir c:\backup -recurse -include *.ext1 | % { ren $_ "$_.bak" }
You can try this
Clear-Host
$from = "'C:\scripts\"
$to = "'C:\backup\"
$inc = #('*.ext1', '*.extx')
$files = get-childItem -path $from -include $inc -Recurse
$files | % {$dest = (Join-Path $to $($_.FullName+".bak").SubString($from.length)); $dum = New-Item -ItemType file $dest -Force; Copy-Item -Path $_ -Destination $dest -Recurse -Force }
the new-item is there in order to force path creation.
Jean Paul