Check if files exists in PowerShell - powershell

I'm trying to copy some files from one diretory to another, check if exists and replace name if yes. And copy all files.
But it gives me the above message:
cmdlet Copy-Item at command pipeline position 1
Supply values for the following parameters:
Path[0]:
PS C:\Scripts\MetadataExport>
What i'm doing wrong?
My code:
Get-ChildItem -Path "C:\Scripts\MetadataExport\*\*" -Directory | ForEach-Object {
$_.FullName | ForEach-Object {Get-ChildItem $_ *.opex | ForEach-Object{If([System.IO.File]::Exists("C:\Scripts\MetadataExport\$Bundles")){While($true){$i=0
$i++
$_.Name= $_+$i}
}Else{Copy-Item -Destination "C:\Scripts\MetadataExport\$Bundles" }}
Get-ChildItem $_ -Recurse -Include *.xip | ForEach-Object{If([System.IO.File]::Exists("C:\Scripts\MetadataExport\$Bundles")){While($true){$i=0
$i++
$_.Name= $_+$i}
}Else{Copy-Item -Destination "C:\Scripts\MetadataExport\$Resources"}}
}
}
Thanks for any help on this

The syntax is Copy-Item -Path "yourpathhere" -Destination "yourdestinationhere"
You've not specified path.

Related

How to add content to file and remove it to another location

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
}

Combine subfolder files into one file (excluding current folder files)

I want to combine the content of all the files in my subfolders in one file. However, I want to exclude the root folder from this search.
I'm very close with the following command:
Get-ChildItem -include *.sql -rec | ForEach-Object {gc $_; ""} | out-file final.sql
The problem is that, as the foreach is recursive, it also finds out the output file (final.sql), which creates an infinite loop. This powershell commands never ends and the final.sql file gets larger and larger with time.
How can I exclude the current directory from my search?
Important: I don't want to explicitly mention the path, as different users will have a different file system.
Try this:
$mainPath = 'C:\files\test'
$directories = Get-ChildItem -Path $mainPath -Directory
$destFile = $mainPath + '\final.sql'
Remove-Item -Path $destFile -ErrorAction SilentlyContinue | Out-Null
foreach( $directory in $directories ) {
Get-ChildItem -Path $directory.FullName -include *.sql -rec | ForEach-Object {gc $_; ""} | Out-File $destFile -Append
}
The user f6a4 answered correctly, but if you don't want to use the fully qualified directory name, you better use this version:
$directories = Get-ChildItem -Path $mainPath -Directory
Remove-Item -Path final.sql -ErrorAction SilentlyContinue | Out-Null
foreach( $directory in $directories ) {
Get-ChildItem -Path $directory.FullName -include *.sql -rec | ForEach-Object {gc $_; ""} | Out-File final.sql -Append
}
You can run this file using powershell by creating the following .ps1 file:
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& './GlobalVersionPowershellBatch.ps1'"

Create CSV file in each subdirectory of target directory

I have folder with files as below:
D:\TESzz\Background\BG_Flash-Zootopia-HD-Desktop-Wallpapers.jpg
D:\TESzz\Background\BG_NimmHBD.jpg
D:\TESzz\Background\BG_Note5.jpg
D:\TESzz\Icons\150x150.jpg
D:\TESzz\Icons\Bathroom-gender-sign.png
D:\TESzz\Icons\brocoli.png
D:\TESzz\Icons\Carrot_Clipart_PNG_Image.png
D:\TESzz\Icons\File.txt
D:\TESzz\Icons\garlic.png
D:\TESzz\Icons\ICONS.txt
D:\TESzz\Icons\NoppNimm-1.jpg
D:\TESzz\Icons\NoppNimmIcon.jpg
D:\TESzz\Icons\NoppNimmIcon.png
D:\TESzz\Icons\NoppProfie.jpg
D:\TESzz\Icons\NoppProfileSerious.jpg
D:\TESzz\Icons\pork.png
D:\TESzz\Icons\Profile.jpg
D:\TESzz\Icons\Questionmark.png
D:\TESzz\Icons\sugar.png
D:\TESzz\Icons\Tree.png
D:\TESzz\Icons\wheel.png
I want to export list of file to each sub-folder under "D:\TeSzz" as below
D:\TESzz\Icons\Icons.csv
D:\TESzz\Background\Background.csv
I have my code as below. but it will create "FileList.csv" instead of "Icons.csv" or "Background.csv". :(
Get-ChildItem -path "D:\TESzz\" -directory | ForEach-Object {Get-ChildItem -file "$($_.fullname)" | Export-Csv "$(Join-path $_.fullname 'FileList.csv')"}
Could someone help me on this please?
I would introduce a variable for the current directory. You don't get your expected result because you are naming the output always as FileList.csv.
Get-ChildItem -path "D:\TESzz\" -directory |
ForEach-Object {
$directory = $_
Get-ChildItem -file $directory.FullName |
Export-Csv (Join-path $directory.FullName "$($directory.BaseName).csv") -Force
}
In the above, notice that where you had FileList, that's now $($directory.BaseName), and therefore different for each directory.
You can put the parent directory name in a variable, then use it to form the output file name:
$parentDir = "D:\TESzz";
Get-ChildItem -path $parentDir -directory | ForEach-Object {Get-ChildItem -name $parentDir\$_ | Export-Csv -path $(Join-path $parentDir\$_ ($_.Name+'.csv'))}
Here, we use $parentDir to create the export path.

Powershell copy files with specific name

I tried to copy files from one folder to another which have word HIGH at the end of name of files in their names but didn't get it. Any suggestion?
$dest = "C:\transform"
$source = "D:\result"
get-childitem $source - filter ".jpg" -recurse | Where-Object {$_.DirectoryName -match "HIGH" | ForEach-Object { Copy-Item $.fullname $dest}
$_.DirectoryName holds the folder name, $_.Name the file name :
$dest = "C:\transform"
$source = "D:\result"
Get-ChildItem $source -Filter ".jpg" -Recurse |
? { $_.BaseName -match "HIGH$" } |
% { Copy-Item $_.FullName $dest}
Or, as pointed by #Walter Mitty, a simpler :
Copy-Item -Path $source -Filter "*HIGH.jpg" -Destination $dest –Recurse
(in this case -Filter and -Include seem to behave the same)
The simplest way to copy files from one folder to another is the Copy-Item cmdlet.
Take a look at the -Path -Include -Destination and -Recurse parameters.
https://technet.microsoft.com/library/60a19812-67ab-4b58-a6f5-34640edafbb0(v=wps.630).aspx

powershell more than one operations using pipe

My script looks for all the files in folder1 and checks if this file exist in folder2. if it exists then I want to delete the file from folder2 and move the file from Folder1 to folder3.
$folder1 = "D:\folder1"
$folder2= "D:\folder2"
$folder3 = "D:\folder3"
$a = Get-ChildItem $folder1 | select -ExpandProperty basename
$a | foreach {
Get-ChildItem -Path $folder2 -filter *$_* -Recurse
}
Now if I use pipe at the end of for each loop I can either delete or move but not both. How do I handle this situation?
Neither move-item or remove-item output anything to the pipe. However both have a -PassThru switch parameter to allow further processing.
This will do the job:
$A | foreach { Get-ChildItem -Path $folder2 -filter $_ -Recurse} | foreach {Remove-Item $_.FullName; Copy-Item $folder1\$_.BaseName $Folder3}
Get-ChildItem -File -Include(Get-ChildItem -File $folder1) $folder2\* |
foreach { remove-item $_ ; move-item (join-path $folder1 $_.BaseName) -destination $folder3}