We could try 'cmd /c ren $8.3string $newname' inside a powershell but the problem then becomes how to get the correct 8.3 filename without using Get-Item.
We also need to make sure we're not renaming to a directory name that already exists.
Anywho, here ya go...
if ($strFullName -match "\s+$") {
$strParent = $strFullName.Substring(0,$strFullName.LastIndexOf("\"))
$strNewFileName = $strFullName.Split("\")[-1].TrimEnd()
while (Test-Path "$strParent\$strNewFileName") {$strNewFileName+="_"}
$strMatchLine = ((cmd.exe /c dir "\\?\$strFullName") | where {$_ -match "<DIR>"})[0].Split(".")[0]+$strNewFileName.Substring(0,6).ToUpper()
$strShortName = ((cmd.exe /c dir "\\?\$strParent" /x | where {$_ -match "$strMatchLine"}) -replace "\s+","`t").Split("`t")[4]
cmd.exe /c ren "$strParent\$strShortName" "$strNewFileName"
}
This utilizes output from the cmd.exe dir command to figure out the 8.3 notation while at the same time making sure the directory has the same timestamp in case there are several dirs that match (~1,~2,...).
Also adds trailing underscores if there's already a directories with the new name(s).
Hope this helps somebody out.
Paul Harrington wrote:
Rename Files and Folders with Trailing space
24-Feb-09
can anyone confirm which method worked for renaming files and folders with a trailing space?
Thanks
Paul
Previous Posts In This Thread:
On Tuesday, September 30, 2008 4:29 PM
Tim wrote:
Rename Files and Folders with Trailing space
This should be simple - I need to write a script to go thru our fileservers
looking for files where the last character is a space or folders where the
last character is a space. For instance "Market Photos 15-19 "
When I attempt to rename one of these I am told the item does not exist so
rename and move fail. I can identify them using dir -force -recurse |
?{$_.Name -match '\s$'}, and I notice that the attrib on the folders is
always DARHS which I cannot change.
I know I can go out to the Windows Command Prompt and use the 8.3 to rename
the folders, but at last count there were over 2500 files and folders like
this - all created by our Mac users - apparently they think a space is a nice
feature at the end of a file or folder name. I've tried VBScript but because
the files and folders are hidden I can't seem to get them to be in the
subFodlers collection.
Any ideas?
On Tuesday, September 30, 2008 5:01 PM
Tim wrote:
Re: Rename Files and Folders with Trailing space
No it doesn't - here is the text I get:
7> dir -force | ?{$_.Name -match '\s$'}
Directory: Microsoft.PowerShell.Core\FileSystem::O:\Marketing
Graphics\TARGET PACKAGE SHOTS JPG FILES\Files
Mode LastWriteTime Length Name
---- ------------- ------ ----
darhs 12/31/1600 6:00 PM <DIR> Files for Boards 15-19
8> rename-item "Files for Boards 15-19 " "Files for Board 15-19X"
Rename-Item : Cannot rename because item at 'Files for Boards 15-19 ' does
not exist.
At line:1 char:12
+ rename-item <<<< "Files for Boards 15-19 " "Files for Board 15-19X"
9> rename-item "Files for Boards 15-19 " "Files for Board 15-19X" -force
Rename-Item : Cannot rename because item at 'Files for Boards 15-19 ' does
not exist.
At line:1 char:12
+ rename-item <<<< "Files for Boards 15-19 " "Files for Board 15-19X" -force
Sorry about the bad formatting.
As to recreating I've just created a folder from DOS using md Test+ALT255.
The alt+255 creates the space for me and recreates the issue.
Thanks!
"Keith Hill [MVP]" wrote:
On Wednesday, October 01, 2008 10:57 PM
Kiron wrote:
V2hhdCBkb2VzIHRoaXMgcmV0dXJuPw0KDQpkaXIgLWZvcmNlIC1yZWN1cnNlIHwgPyB7JF8uTmFtZSA
V2hhdCBkb2VzIHRoaXMgcmV0dXJuPw0KDQpkaXIgLWZvcmNlIC1yZWN1cnNlIHwgPyB7JF8uTmFt
ZSAtbWF0Y2ggJ1xzJCd9IHwgJSB7W2ludF0kXy5uYW1lWy0xXX0NCg0KLS0gDQpLaXJvbg==
On Thursday, October 02, 2008 11:48 AM
Tim wrote:
It returns 32, which is the ascii equiv of the space.
It returns 32, which is the ascii equiv of the space. So I guess that
confirms the unprintable character is a space.
"Kiron" wrote:
On Thursday, October 02, 2008 1:27 PM
Tim wrote:
I really thought this was going to work because I know I can use cmd and using
I really thought this was going to work because I know I can use cmd and
using the shortname's rename these folders, but it appears the Mac creators
of these files and folders have an additional twist as mentioned in the
original post. These Folders are marked DARHS - so not only hidden but
System as well. Move cannot work on them it appears and I can't figure out
how to remove those attributes.
I like your solution and I think it's on the right track now how to deal
with those attributes. I'm really starting to dislike Macs.
"Joel (Jaykul) Bennett" wrote:
On Thursday, October 02, 2008 4:46 PM
Tim wrote:
Just a little extra information I just found, when I look at attributes rather
Just a little extra information I just found, when I look at attributes
rather than mode this is what I get
70> dir -force | fl Name, Attributes
Name : Files for Boards 1-10
Attributes : Directory
Name : Files for Boards 11-14
Attributes : Directory
Name : Files for Boards 15-19
Attributes : -1
Name : LOGO OPTIONS_1-2
Attributes : Directory
Name : PDFs for HPs
Attributes : Directory
Name : .DS_Store
Attributes : Archive
Name : ._Files for Boards 1-10
Attributes : Archive
Name : ._Files for Boards 11-14
Attributes : Archive
I notice that the folder with the space at the end does not have a valid
entry for attributes. Perhaps this is causing some of the issue.
On Friday, October 03, 2008 6:21 AM
Joel (Jaykul) Bennett wrote:
Well, alt+255 isn't a plain old space, that's for sure.
Well, alt+255 isn't a plain old space, that's for sure. But I *can*
duplicate your error that way:
[113]: echo "foo" > "This is a test"
[114]: cmd /c move "This is a test" "This is a test=A0" # using ALT+255
at the end
[115]: dir -force -recurse | ? {$_.Name -match '\s$'} | % { "'$
($_.Name)'" }
'This is a test=A0'
I admire the problem :) My first thought was to just pipe the
FileInfo in:
[116]: dir -force -recurse | ? {$_.Name -match '\s$'} | rename-item -
new {$_.Name.TrimEnd()}
Rename-Item : Cannot rename because item at 'Microsoft.PowerShell.Core
\FileSystem::C:\Documents and Settings\us321339\My Documents
\WindowsPowershell\This is a test=A0' does not exist.
At line:1 char:61
+ dir -force -recurse | ? {$_.Name -match '\s$'} | rename-item <<<< -
new {$_.Name.TrimEnd()}
[117]: dir -force -recurse | ? {$_.Name -match '\s$'} | move-item -
dest {$_.Name.TrimEnd()}
Move-Item : Cannot move item because item at 'Microsoft.PowerShell.Core
\FileSystem::C:\Documents and Settings\us321339\My Documents
\WindowsPowershell\This is a test=A0' does not exist.
At line:1 char:59
+ dir -force -recurse | ? {$_.Name -match '\s$'} | move-item <<<< -
dest {$_.Name.TrimEnd()}
Then I thought I'd try wildcards:
[118]: dir -force -recurse | ? {$_.Name -match '\s$'} | % { move-item
"$($_.Name.TrimEnd())*" $_.Name.TrimEnd() }
Move-Item : Cannot move item because item at 'C:\Documents and Settings
\us321339\My Documents\WindowsPowershell\This is a test=A0' does not
exist.
At line:1 char:63
+ dir -force -recurse | ? {$_.Name -match '\s$'} | % { move-item <<<<
"$($_.Name.TrimEnd())*" $_.Name.TrimEnd() }
Finally, I thought, well, if I created this in CMD, I can uncreate it
in CMD:
[119]: dir -force -recurse | ? {$_.Name -match '\s$'} | % { cmd /c
move "$($_.Name)" "$($_.Name.TrimEnd())" }
Sure enough, that worked.
--
Joel "Jaykul" Bennett
On Friday, October 03, 2008 6:22 AM
RickB wrote:
Re: Rename Files and Folders with Trailing space
On Oct 2, 3:46=A0pm, TimA <T...@discussions.microsoft.com> wrote:
There is a parellel thread that has almost exactly this problem.
"Can't rename files with certain characters in them."
The answer was this.
(dir ".\$srcName" -literalpath).moveto(".\$destName")
Perhaps it works here too??
On Friday, October 03, 2008 8:43 AM
Kiron wrote:
QWx0KzI1NSBwcm9kdWNlcyBhIE5CU1AuDQpPbiBteSBzeXN0ZW0gKFZpc3RhIFNQMSkgdGhlIGNvbW1
QWx0KzI1NSBwcm9kdWNlcyBhIE5CU1AuDQpPbiBteSBzeXN0ZW0gKFZpc3RhIFNQMSkgdGhlIGNv
bW1hbmQuLi4NCg0KbHMgLWZvIC1yIHwgPyB7JF8ubmFtZSAtbWF0Y2ggJ1xzJCd9IHwgJSB7W2lu
dF0kXy5uYW1lWy0xXX0NCg0KIyAuLi5yZXR1cm5zIDE2MCwgYW5kLi4uDQoNCmxzIC1mbyB8ID8g
eyRfLm5hbWUgLW1hdGNoICdcdTAwYTAkJ30NCg0KIyAuLi5saXN0cyB0aGUgaXRlbXMgd2hvc2Ug
bmFtZSBlbmQgaW4gYSBOQlNQDQoNCiMgc2V0IHRoZSBBcmNoaXZlL0RpcmVjdG9yeSBhdHRyaWJ1
dGUgYW5kIGNsZWFyIGFsbCBvdGhlcnMgd2l0aCB0aGlzOg0KIA0KbHMgLWZvIHwgPyB7JF8ubmF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Friday, October 03, 2008 11:02 AM
Tim wrote:
Unfortunately same issue - PowerShell cannot locate the file -28> ls -fo | ?
Unfortunately same issue - PowerShell cannot locate the file -
28> ls -fo | ? {$_.name -match '\s$'} | % { if (!$_.psIsContainer)
{$_.attributes = 32} else {$_.attributes = 16}}
Exception setting "Attributes": "Could not find file 'Files for Boards 15-19
'."
At line:1 char:94
+ ls -fo | ? {$_.name -match '\s$'} | % { if (!$_.psIsContainer)
{$_.attributes = 32} else {$_.a <<<< ttributes = 16}}
That space is giving PS fits. Since -1 is not a valid attribute I am not
sure how I can change that unless I can grab a different file handle in PS.
When I tried
CMD seems to strip the space off of the file before doing anything as here
is the output
O:\Marketing Graphics\TARGET PACKAGE SHOTS JPG FILES\Files>ren "O:\Marketing
Gra
phics\TARGET PACKAGE SHOTS JPG FILES\Files\Files for Boards 15-19" Files for
Boards 15-19
The syntax of the command is incorrect.
I notice there is no space between the 19 and the quote.
I've tried the PS community extensions and the GetShortPath command cannot
latch onto the file to get the short path. I thought if I could script like
this cmd /c ren $ShortName "$LongName" I might be able to get it to work but
I can't get the Shortname in PS.
I feel like I am missing something very obvious here.
On Friday, October 03, 2008 12:23 PM
Tim wrote:
Re: Rename Files and Folders with Trailing space
"RickB" wrote:
Thought it looked odd :)
Unfortunately no luck. Nothing seems to allow PS to grab the space and
treat it as part of the filename -
49> (dir -literalpath ".\$srcName").moveto(".\$destName")
Get-ChildItem : Cannot find path 'O:\Marketing Graphics\TARGET PACKAGE SHOTS
JPG FILES\Files\Files for Boards 15-19 ' because it does not exist.
At line:1 char:5
+ (dir <<<< -literalpath ".\$srcName").moveto(".\$destName")
You cannot call a method on a null-valued expression.
At line:1 char:39
+ (dir -literalpath ".\$srcName").moveto( <<<< ".\$destName")
I can't even figure out how the Mac clients did this to duplicate. It
seems to have been 10.4 or earlier clients because none of the 10.5 clients
can do this. Unfortunately the one 10.4 we have left is in Germany at the
moment so I can't confirm with that one.
On Friday, October 03, 2008 1:16 PM
David Trimboli wrote:
Re: Rename Files and Folders with Trailing space
TimA wrote:
I don't know how to deal with this in PowerShell, but I know how to fix
it with CMD: http://support.microsoft.com/kb/315226
Instead of "C:\Folder\File with space at end.txt ", use
"\\?\C:\Folder\File with space at end.txt ". You should be able to
delete bad file names this way.
--
David
Stardate 8757.6
On Friday, October 03, 2008 1:21 PM
Kiron wrote:
SSBjYW4ndCBleHBsYWluIHdoeSB5b3UgY2FuJ3Qgc2V0IGF0dHJpYnV0ZXMgdGhyb3VnaCBQb3dlclN
SSBjYW4ndCBleHBsYWluIHdoeSB5b3UgY2FuJ3Qgc2V0IGF0dHJpYnV0ZXMgdGhyb3VnaCBQb3dl
clNoZWxsLCBjb3VsZCBiZSBiZWNhdXNlIG9mIGRpZmZlcmVudCBPUy4gSXQncyBhbHNvIHdlaXJk
LCB0aGF0IGluIG15IHRlc3RzIHRoZSBOQlNQIGlzIG5vdCBzdHJpcHBlZCBmcm9tIHRoZSBmaXJz
dCBhcmcgaWYgaXQncyBxdW90ZWQsIGJ1dCBJIGRpZG4ndCB0ZXN0IGFnYWluc3QgZmlsZXMgd2l0
aCBzcGFjZXMgLW5vcm1hbCBzcGFjZXMtIGluIHRoZW0uIE5vdCBxdW90aW5nIHRoZSBzZWNvbmQg
YXJnIC1uZXcgbmFtZS0gc3RyaXBzIHRoZSBOQlNQLCBidXQgaXQgd29uJ3Qgd29yayB3aXRoIG5h
bWVzIHRoYXQgaW5jbHVkZSBzcGFjZXMsIGl0IGNhdXNlcyB0aGUgd3Jvbmcgc3ludGF4IGVycm9y
IHVubGVzcyB0aGUgc2Vjb25kIGFyZyBpcyBxdW90ZWQsIGhvd2V2ZXIsIGlmIHRoZSBzZWNvbmQg
YXJnIGlzIHF1b3RlZCB0aGUgTkJTUCBpcyBpbmNsdWRlZCwgYWxvbmcgd2l0aCB0aGUgc3BhY2Vz
IGJldHdlZW4gd29yZHMsIGFuZCB0aGUgcHVycG9zZSBpcyBkZWZlYXRlZC4gVGhlIHNlY29uZCBh
cmcgY2F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WMvaGVscC1yZW5hbWluZy1hZnRlci1hbHQyNTUN
Cg0KLS0gDQpLaXJvbg==
On Friday, October 03, 2008 1:33 PM
Tim wrote:
Re: Rename Files and Folders with Trailing space
"David Trimboli" wrote:
Yeah I stumbled on that too and it does work but I couldn't get it to work
in a powershel script. With 2500 files and the possibility that the users
will continue to do it, I need to figure out a scriptable method.
On Friday, October 03, 2008 4:38 PM
David Trimboli wrote:
Re: Rename Files and Folders with Trailing space
TimA wrote:
Oh, you can script that in CMD. Mind you, I haven't tested this. X:\ is
the root directory of a recursive search for files with names or
extensions ending in a single space. The script doesn't check for
multiple trailing spaces.
setlocal enabledelayedexpansion enableextensions
for /r X:\ %%i in (*) do (
set rename=no
set name=%~ni
set extension=%~xi
set filename=!name!!extension!
if "!name~-1!" equ " " (
set name=!name~0,-1!
set rename=yes
)
if "!extension~-1!" equ " " (
set extension=!extension~0,-1!
set rename=yes
)
if "!rename!" equ "yes" ren "\\?\%%i" "!name!!extension!"
)
endlocal
--
David
Stardate 8757.9
On Sunday, October 05, 2008 11:09 AM
RickB wrote:
Re: Rename Files and Folders with Trailing space
d
I suppose it helps to have the syntax correct.
(dir -literalpath ".\$srcName").moveto(".\$destName")
On Tuesday, February 24, 2009 9:50 AM
Paul Harrington wrote:
Rename Files and Folders with Trailing space
can anyone confirm which method worked for renaming files and folders with a trailing space?
Thanks
Paul
Submitted via EggHeadCafe - Software Developer Portal of Choice
Sending SMTP email from within BizTalk Orchestration
http://www.eggheadcafe.com/tutorials/aspnet/9dd0f346-baf9-4674-a50f-1716445b26bc/sending-smtp-email-from-w.aspx