Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I remove Blank Space from File Names

I am trying to remove blank spaces from many file names using PowerShell 3.0. Here is the code that I am working with:

$Files = Get-ChildItem -Path "C:\PowershellTests\With_Space"
Copy-Item $Files.FullName -Destination C:\PowershellTests\Without_Space
Set-Location -Path C:\PowershellTests\Without_Space
Get-ChildItem *.txt | Rename-Item -NewName { $_.Name -replace ' ','' }

For example: the With_Space directory has these files:

Cable Report 3413109.pdf
Control List 3.txt
Test Result Phase 2.doc

The Without_Space directory will need the above file name to be:

CableReport3413109.pdf
ControlList3.txt
TestResultPhase 2.doc

Currently, the script shows no error but it only copies the source files to the destination folder, but doesn't remove the spaces in file names.

like image 524
Sohel Avatar asked Apr 27 '16 15:04

Sohel


People also ask

Are spaces in file names a problem?

Avoid spaces Spaces are not supported by all operating systems or by command line applications. A space in a filename can cause errors when loading a file or when transferring files between computers. Common replacements for spaces in a filenames are dashes (-) or underscores (_).

How do you rename files with spaces?

If you want to rename a file name containing spaces to a new file name with spaces, place quotation marks around both file names, as in the following example. The same examples above can be applied to the copy, rename, delete, or other commands in the Windows command line that involve a file name with a space.

Can file names contain spaces?

Don't start or end your filename with a space, period, hyphen, or underline. Keep your filenames to a reasonable length and be sure they are under 31 characters. Most operating systems are case sensitive; always use lowercase. Avoid using spaces and underscores; use a hyphen instead.


3 Answers

Your code should work just fine, but since Get-ChildItem *.txt lists only .txt files the last statement should remove the spaces from just the text files, giving you a result like this:

Cable Report 3413109.pdf
ControlList3.txt
Test Result Phase 2.doc

This should remove spaces from the names of all files in the folder:

Get-ChildItem -File | Rename-Item -NewName { $_.Name -replace ' ','' }

Prior to PowerShell v3 use this to restrict processing to just files:

Get-ChildItem | Where-Object { -not $_.PSIsContainer } |
    Rename-Item -NewName { $_.Name -replace ' ','' }
like image 160
Ansgar Wiechers Avatar answered Sep 30 '22 13:09

Ansgar Wiechers


something like this could work

$source = 'C:\temp\new'
$dest = 'C:\temp\new1'
Get-ChildItem $source | % {copy $_.FullName $(join-path $dest ($_.name -replace ' '))}
like image 26
Anthony Stringer Avatar answered Sep 30 '22 15:09

Anthony Stringer


I think your script should almost work, except $_ isn't going to be defined as anything. By using the for-each cmdlet (%), you assign it and then can use it.

Get-ChildItem *.txt | %{Rename-Item -NewName ( $_.Name -replace ' ','' )}

EDIT: That interpretation was totally wrong. Some people seem to have found it useful, but as soon as you have something being piped, it appears that $_ references the object currently in the pipe. My bad.

like image 40
Jared Windover-Kroes Avatar answered Sep 30 '22 15:09

Jared Windover-Kroes