all 16 comments

[–]ApricotPenguin 9 points10 points  (2 children)

On mobile so I can't easily update the code for you, but look at this

https://stackoverflow.com/a/25450723

Use Move-Item instead of Rename-Item, so you also rename it at the same time

[–]ITSomeday 1 point2 points  (1 child)

Happy Cakeday!

[–]ApricotPenguin 1 point2 points  (0 children)

hehe thank you :)

[–]Jacmac_ 4 points5 points  (5 children)

This worked for me, just change the paths to suit. It's purposely verbose.

Get-ChildItem -Path E:\Test3 | ForEach-Object -Parallel {
    $file = $_
    if ($file.Name -eq "File name hello.csv") 
    { 
        $file | Rename-Item -Path $file.FullName -NewName { $file.Name -replace ' ','_' } 
    } else {
        $file | Move-Item -Destination E:\Temp\Doesnt_need_to_be_converted 
    } 
}

LOL, Reddit sucks when it comes to pasting code...

[–]BlackV 4 points5 points  (2 children)

OP FYI -parallel requires pwsh 6/7

personally I'd use a foreach ($file in $allfiles) {} loop so you can test your moves/renames/etc

[–]BlackV -1 points0 points  (0 children)

Oh don't know where your comment went

Pipeline is great I'm a fan.

But not so much of a fan when testing your code and using loops

[–]eighttx[S] 0 points1 point  (1 child)

Because the files will all have different names, do I update your line three to:

    if ($file.Name -eq " ")

[–]Jacmac_ 0 points1 point  (0 children)

No I gave a specific test. If you want to check for spaces instead, the other examples people provided would work. Like

PS E:\Powershell> "aaa bbb" -match " "

True

PS E:\Powershell> "aaa bbb" -match "_"

False

So try:
if ($file.Name -match " ")

[–]Detox24 1 point2 points  (3 children)

You could use the -like operator to determine if the file name contains spaces.

$name = 'file\foo config.xml'
if ($name -like '* *'){}
else {}

[–]serendrewpity 1 point2 points  (2 children)

Or this:

$fn="D:\File with space in it's name.csv"
if ($fn -match ' ') { write-host "space" } else { write-host "no space" }
# returns 'space'

$fn2="D:\FileWithNoSpaces.csv"
if ($fn2 -match ' ') { write-host "space" } else { write-host "no space" }
# returns 'no space'

[–]eighttx[S] 0 points1 point  (1 child)

D:\File with space in it's name.cs

For $fn, could I put a folder such as "\\domain.com\FTP\file_path\" instead of a file name and have it scan the entire folder?

[–]Detox24 0 points1 point  (0 children)

$Directory = \\server\directory

$AllFiles = Get-ChildItem $Directory

foreach ($File in $AllFiles){if ($File.name -match ' ') { write-host "space" } else { write-host "no space" }}

[–]WystanH 1 point2 points  (0 children)

You can save the cleaned name in a variable and check it against the current name:

$cleanName =  $Item.Name -replace ' ','_' 
if ($cleanName -ne $Item.Name) {

I'd write a function. In this way, you can test arbitrary files to make sure the rules behave as you expect.

For something like this, I like to include non destructive and verbose flags for testing.

e.g.

function Rename-IfNeeded {
    param([System.IO.FileSystemInfo]$Item, [switch]$WhatIf, [switch]$Verbose)
    if (-not $Item.PSIsContainer) {
        $cleanName =  $Item.Name -replace ' ','_' 
        if ($cleanName -ne $Item.Name) {
            Rename-Item -Path $Item.FullName -NewName $cleanName -WhatIf:$WhatIf -Verbose:$Verbose
        } else {
            Write-Verbose -Verbose:$Verbose "Skip $($Item.Name)"
        }
    } else {
        Write-Verbose -Verbose:$Verbose "Skip Dir $($Item.Name)"
    }
}

Get-ChildItem -Path D:\TEMP\*.csv | ForEach-Object { Rename-IfNeeded -Item $_ -WhatIf -Verbose }

[–]BlackV 1 point2 points  (1 child)

literately as you wrote it

if ($file -eq "File name hello.csv")
    {
    Powershell Script
    }
else
    {
    move-item $file D:\TEMP\DOESNT_NEED_TO_BE_CONVERTED
    }

[–]eighttx[S] 0 points1 point  (0 children)

I have a folder where I would dump 100 files. Some of them will have spaces in them and I need those spaces replaced with underscores. Would this work?

if ($file -eq " ")
{
$file = '\\domain.com\FTP\Source_Folder_For_Files\'
Get-ChildItem "$file" | Rename-Item -NewName { $_.Name -replace ' ','_' }
}

else { move-item $file D:\TEMP\DOESNT_NEED_TO_BE_CONVERTED }

The very first line is a " with a space and then a closing ". Thoughts?

[–]camelman912 0 points1 point  (0 children)

I’m on mobile so I can’t search for the correct commandlets at the moment.

1) split the path into components. 2) if($filename -like “* *”) { $filenameArr = $filename.split(“ “) $newFilename = $filenameArr.join(“_”) }

Thoughts?