Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using "CON" as filename

I was copying a huge number of png and txt files using Copy-Item cmdlet, and sadly I discovered that a funny programmer decided to use "CON" as file name to recap connection information.

Given that "con" is a reserved word and Copy-Item returns:

Copy-Item : Cannot process path 'xxx\con.txt' because the target represents a reserved device name.

and given that this name can't be changed and it's used in every folder I need to copy, Is there a way to copy all these "con.cfg" and "con.txt" files using Powershell?

I googled but I found only advice like "Don't use con!" or "Don't use Powershell to copy these files".

like image 467
Naigel Avatar asked Oct 21 '22 14:10

Naigel


2 Answers

I haven't been able to find a solution for PowerShell yet, but you should be able to rename the files via command prompt using something like this:

ren \\.\<absolute path> <new name>

So for example:

ren \\.\C:\stuff\con.cfg stuff.cfg

You could invoke the command prompt through PowerShell, of course:

cmd /c "ren \\.\C:\stuff\con.cfg stuff.cfg"

And obviously you could use PowerShell variables in there if you wanted

$dir = "C:\stuff"
cmd /c "ren \\.\$dir\con.cfg stuff.cfg"
like image 199
Nacimota Avatar answered Nov 15 '22 05:11

Nacimota


You could try referring to them using a wildcard: *on ?

Example:

ls | ? {$_.Name -match "*on.cfg"} | del

Regex example:

ls | ? {$_.Name -match "^\won\.cfg"} | del
like image 27
Vasili Syrakis Avatar answered Nov 15 '22 06:11

Vasili Syrakis