create folder name base on partial file name - powershell

i have file names that have a "-" (hyphen) in the filename. I am trying to create a folder for each filename and move the file to the folder.
here is what I am trying to use:
dir | %{
$id = $_.Name.SubString(0,5);
if(-not (Test-Path $id)) {mkdir $id};
mv $_ "$id\$_";}
The issue is, some of the file names have less than 5 characters before the hyphen, so for those folders the hyphen is being added to the folder name. I've tried to use the Split verbiage, but I am stuck on syntax.
a couple of filenames examples are below:
A1909-6628.txt
A963-6634.txt
thanks in advance for your help

try this
#split return an array
$id = ($_.Name -split '-')[0];

You're pretty much on the right track, and you've mentioned the -split operator which is all you really need.
Get-ChildItem | ForEach-Object -Process {
$id = $_.BaseName.split('-')[0];
if((Test-Path $id) -eq $false){Mkdir $id}
Move-Item -Path $_.FullName -Destination $id
}
Without modifying your code too much, i added the split at the hyphen then selectin the first value after the split.

try this:
Get-ChildItem -file -filter "*-*" | %{
#extract first part of file
$Part1=($_.BaseName -split '-')[0].Trim()
#create directory if exist
$NewFolderName="{0}\{1}" -f $_.DirectoryName, $Part1
New-Item -ItemType Directory -Path $NewFolderName -Force -ErrorAction SilentlyContinue
#prepare new filename
$NewPathFile="{0}\{1}" -f $NewFolderName, $_.Name
#move file into new directory
Move-Item $_.FullName $NewPathFile
}

Related

If testfile1.log exists, create testfile2.log, and so on.. - PowerShell

My PowerShell script creates a log file, but when I run the script for the second time, it tells me that the testfile1.log file already exists.
How do I make the script if it finds testfile1.log, it creates testfile2.log, and if this also exists, it creates testfile3.log, and so on..
New-Item -Path $path -Name "testfile1.log" -ItemType "file"
You could do it this way, first get all the files in the desired path and sort them by the ending digits on their name. If no files are found create the testfile1.log, if there were files found, get the last sorted file (the one with the highest ending digit) extract the ending digits and add +1 to the count and use it to create the new file.
$files = Get-ChildItem $path -Filter testfile*.log | Sort-Object {
$_.BaseName -replace '\D' -as [int]
}
if(-not $files)
{
New-Item -Path $path -Name "testfile1.log" -ItemType File
}
else
{
[int]$number = $files[-1].BaseName -replace '\D'
$number++
New-Item -Path $path -Name "testfile$number.log" -ItemType File
}
An alternative method, based on this answer could be
$path = 'D:\Test'
$log = 'testfile'
$index = ((Get-ChildItem -Path $path -Filter "$log*.log" -File |
Where-Object { $_.BaseName -match "$log\d+$" } |
Select-Object #{Name = 'index'; Expression = {[int]($_.BaseName -replace '\D')}}).index |
Measure-Object -Maximum).Maximum + 1
# create the new file
New-Item -Path (Join-Path -Path $path -ChildPath "$log${index}.log") -ItemType File
A concise solution that also builds on this answer (see there for an explanation of the core technique):
$path = '.' # Output dir.
$nameTemplate = 'testfile{0}.log' # {0} is the sequence-number placeholder
New-Item -ItemType File -Path $path -Name (
$nameTemplate -f (1 + (
# Find all existing log files
Get-ChildItem (Join-Path $path $nameTemplate.Replace('{0}', '*')) |
Measure-Object -Maximum {
# Extract the embedded sequence number.
$_.Name -replace [regex]::Escape($nameTemplate).Replace('\{0}', '(\d+)'), '$1'
}
).Maximum)
) -WhatIf
Note: The -WhatIf common parameter in the command above previews the operation. Remove -WhatIf once you're sure the operation will do what you want.
Note:
The above uses a complex -replace operation to reliably extract the sequence number from existing file names; if you know that that only one number is present in each given file name, $_.BaseName -replace '\D' (removing all non-digit characters) will do in the Measure-Object call above.
If you wanted to use zero-padded, fixed-width sequence numbers, you can adjust (all occurrences of) the {0} placeholder accordingly; e.g, to create sequence numbers 01, 02, ... 99, use {0:00} - see the Composite formatting help topic, which describes the string formatting language also used by PowerShell's -foperator

How to copy files using a txt list to define beginning of file names

Hello awesome community :)
I have a list containing a bunch of SKU's. All the filenames of the files, that I need to copy to a new location, starts with the corresponding SKU like so
B6BC004-022_10_300_f.jpg
In this case "B6BC004" is the SKU and my txt list contains "B6BC004" along with many other SKU's.
Somewhere in the code below I know I have to define that it should search for files beginning with the SKU's from the txt file but I have no idea how to define it.
Get-Content .\photostocopy.txt | Foreach-Object { copy-item -Path $_ -Destination "Z:\Photosdestination\"}
Thanks in advance :)
If all files start with one of the SKU's, followed by a dash like in your example, this should work:
$sourceFolder = 'ENTER THE PATH WHERE THE FILES TO COPY ARE'
$destination = 'Z:\Photosdestination'
# get an array of all SKU's
$sku = Get-Content .\photostocopy.txt | Select-Object -Unique
# loop through the list of files in the source folder and copy all that have a name beginning with one of the SKU's
Get-ChildItem -Path $sourceFolder -File -Recurse |
Where-Object { $sku -contains ($_.Name -split '\s*-')[0] } |
ForEach-Object { $_ | Copy-Item -Destination $destination }
I haven't tested this so please proceed with caution!
What is does it loops through all the items in your photostocopy.txt file, searches the $source location for a file(s) with a name like the current item from your file. It then checks if any were found before outputting something to the console and possibly moving the file(s).
$source = '#PATH_TO_SOURCE'
$destination = '#PATH_TO_DESTINATION'
$photosToCopy = Get-Content -Path '#PATH_TO_TXT_FILE'
$photosToCopy | ForEach-Object{
$filesToCopy = Get-ChildItem -Path $source -File | Where-Object {$_.Name -like "$_*"}
if ($fileToCopy.Count -le 0){
Write-Host "No files could be found for: " $_
}else{
$filesToCopy | ForEach-Object{
Write-Host "Moving: " $_.Name
Copy-Item -Path $_.FullName -Destination $destination
}
}
}
Let me know how if this helps you :)

PowerShell - Loop through files and rename

newbie here. I am trying to write a PowerShell script to:
loop through all files in directory
List item
Get all .pdf files ONLY
Rename them-the file names are long - over 30 chars
-They contain 2 numbers which I need to extract
-Example:
Cumulative Update 11 for Microsoft Dynamics NAV 2018 (Build 25480).pdf ->
RESULT : = 18CU11.pdf
I tried examples from bunch of sites and I can't seem to even loop successfully.
Either get an error - that path doesn't exist or that can't rename files as somehow loop gets a filepath and that I can't rename
Get-ChildItem "C:\Users\******\Desktop\PowerShell Practice" -Filter *.pdf | #create list of files
ForEach-Object{
$oldname = $_.FullName;
$newname = $_.FullName.Remove(0,17);
#$newname = $_.FullName.Insert(0,"CU")
Rename-Item $oldname $newname;
$oldname;
$newname; #for testing
}
That's just latest attempt, but any other ways of doing it will be fine - as long as it does the job.
Try this logic:
[string]$rootPathForFiles = Join-Path -Path $env:USERPROFILE -ChildPath 'Desktop\PowerShell Practice'
[string[]]$listOfFilesToRename = Get-ChildItem -Path $rootPathForFiles -Filter '*.PDF' | Select-Object -ExpandProperty FullName
$listOfFilesToRename | ForEach-Object {
#get the filename wihtout the directory
[string]$newName = Split-Path -Path $_ -Leaf
#use regex replace to apply the new format
$newName = $newName -replace '^Cumulative Update (\d+) .*NAV 20(\d+).*$', '$2CU$1.pdf' # Assumes a certain format; if the update doesn't match this expectation the original filename is maintained
#Perform the rename
Write-Verbose "Renaming '$_' to '$newName'" -Verbose #added the verbose switch here so you'll see the output without worrying about the verbose preference
Rename-Item -Path $_ -NewName $newName
}
Check the Help for Rename-Item. The Parameter -NewName requires the name of the file only, not the full path.
Try out this:
Get-ChildItem "C:\Users\******\Desktop\PowerShell Practice-Filter" -Filter *.pdf | #create list of files
ForEach-Object{
$oldname = $_.FullName
$newname = $_.Name.Remove(0,17)
Rename-Item -Path $oldname -NewName $newname
$oldname
$newname #for testing
}
Please try this
Get-ChildItem -Path "C:\Users\******\Desktop\PowerShell Practice-Filter" -Filter *.pdf | Rename-Item -NewName $newname

In power shell, how to replicate the action I do in a folder to other folders?

The action I am supposed to do:
Read a file from a folder
Extract a date from the 1st line
Change it to 'yyyymmdd' format
Rename all the file by removing first 4 characters and place this format date at the beginning.
Example FILE.FILE will be changed to 20180725.FILE (The date will be in the file)
I am able to accomplish this to a single folder in Newpath by the following code:
$path="\\Data\DEV\DevDat\Arun\DXSL\Newpath\20170601"
$files=Get-ChildItem $path | Select-Object -first 1
$data = Get-Content "$path\$files" -first 1
foreach($line in $data)
{
$arr = $line.substring(52,4)+$line.substring(46,2)+$line.substring(49,2)
}
get-childitem $path | rename-item -newname { $arr +[string]($_.name).substring(4)}
However, I am not able to replicate this action to the other folders in a loop. The Newpath folder has several sub-folders. I need to rename the files inside each sub-folder inside Newpath. Is there any way to achieve this?
FYI I'm using Version 4 of powershell.
A PowerShell script as suggested in my comment.
It does not check if the file to rename to already exists.
The Rename-Item has the parameer -WhatIf appended, so it only shows what would be done.
If there are more numbers in the first line, the script will match the first pattern.
The script also does not check if the file already is renamed to the pattern.
## Q:\Test\2018\07\25\SO_51522205.ps1
#Requires -Version 3.0
$BasePath = "\\Data\DEV\DevDat\Arun\DXSL\Newpath"
$RE = [RegEx]'(?<Month>\d{2}).(?<Day>\d{2}).(?<Year>\d{4})'
ForEach ($Folder in (Get-ChildItem -Path "$BasePath\*" -Directory)){
ForEach ($File in (Get-ChildItem -Path "$($Folder.FullName)\*" -File)){
$Line1 = (Get-Content $File.FullName | Select-Object -First 1)
If ($File.BaseName.Length -gt 4){
$BaseName = $File.BaseName.SubString(4)
} else {
$BaseName = ''
}
If ($Line1 -match $RE){
$NewName = ("{0}{1}{2}{3}{4}" -f `
$Matches.Year,
$Matches.Month,
$Matches.Day,
$BaseName,
$File.Extension)
$File | Rename-Item -NewName $NewName -WhatIf
} Else {
"{0} doesn't have a proper date in 1st line" -f $File.FullName
}
}
}
Sample tree before,
> tree /F
└───Newpath
└───20170601
blahblah.txt
FILE.FILE
after running the script.
> tree /F
└───Newpath
└───20170601
20180724blah.txt
20180725.FILE

Script to move folders with specific names and characters

I am looking to write a script that will move existing folders with a "-" hyphen in the name into a parent folder with part of the same name.
Example:
I would like to move c:\12345-01 into c:\12345\ to look like this c:\12345\12345-01.
The problem is the number of characters before the hyphen is not the same, they very from 3-6 characters before the hyphen. I have found a couple of snipets here and elsewhere that are close, but they won't work as they specify the number of characters in the file name
For Each objFile in objFolder.Files
strName = Left(objFile.Name, 3)
this would work if all filenames were 3 characters.
Does that makes sense?
cd 'C:\Directory\of\folder\where\number\files\located'
gci | ? {$_.PSIsContainer -eq $true} | % { `
$file = $_.name
$loc = $file.IndexOf('-')
$folder = $file.substring(0,$loc)
New-Item -Path . -ItemType Directory -Name $folder -ErrorAction SilentlyContinue
move-item $file $folder
}
You need to split the names on the delimiter. If you run the following from the root of the C drive, you'll get the structure you're looking for.
Get-ChildItem | where-object {
($_.PSIsContainer) -and ($_.Name.contains("-"))
} | foreach-object {
new-item -itemtype Directory -path $($_.name.split("-")[0])
move-item $_ -Destination $($_.name.split("-")[0])
}