Obtener la ruta completa de los archivos en PowerShell
Necesito obtener todos los archivos, incluidos los archivos presentes en las subcarpetas que pertenecen a un tipo particular.
Estoy haciendo algo como esto, usando Get-ChildItem :
Get-ChildItem "C:\windows\System32" -Recurse | where {$_.extension -eq ".txt"}
Sin embargo, solo me devuelve los nombres de los archivos y no toda la ruta.
10 answers
Añade | select FullName
al final de la línea anterior. Si realmente necesitas hacer algo con eso después, es posible que tengas que conectarlo a un bucle foreach, así:
get-childitem "C:\windows\System32" -recurse | where {$_.extension -eq ".txt"} | % {
Write-Host $_.FullName
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-10-29 17:01:42
Esto debería funcionar mucho más rápido que usar el filtrado tardío:
Get-ChildItem C:\WINDOWS\System32 -Filter *.txt -Recurse | % { $_.FullName }
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-09-26 19:17:40
También puedes usar Select-Object así:
Get-ChildItem "C:\WINDOWS\System32" *.txt -Recurse | Select-Object FullName
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-09-26 19:20:03
Aquí hay una más corta:
(Get-ChildItem C:\MYDIRECTORY -Recurse).fullname > filename.txt
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-09-26 19:18:08
Si las rutas relativas son lo que desea, puede usar la bandera -Name
.
Get-ChildItem "C:\windows\System32" -Recurse -Filter *.txt -Name
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-04-06 08:30:47
Get-ChildItem -Recurse *.txt | Format-Table FullName
Eso es lo que usé. Siento que es más comprensible, ya que no contiene ninguna sintaxis de bucle.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-03-23 19:05:49
Prueba esto:
Get-ChildItem C:\windows\System32 -Include *.txt -Recurse | select -ExpandProperty FullName
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-08-10 12:36:29
gci "C:\WINDOWS\System32" -r -include .txt | select fullname
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-09-26 19:21:39
[sintaxis alternativa]
Para algunas personas, los operadores de tuberías direccionales no son su gusto, pero prefieren el encadenamiento. Vea algunas opiniones interesantes sobre este tema compartidas en roslyn issue tracker: dotnet/roslyn#5445.
Basado en el caso y el contexto, uno de estos enfoques puede ser considerado implícito (o indirecto). Por ejemplo, en este caso, usar una tubería contra enumerable requiere un token especial $_
(también conocido como PowerShell's "THIS" token
) podría parecer desagradable para algunos.
Para tales tipos, aquí hay una forma más concisa y directa de hacerlo con encadenamiento de puntos :
(gci . -re -fi *.txt).FullName
(-recursive
; -recursiv
, -recursi
, -recurs
, -recur
, -recu
, -rec
y -re
aceptó, pero por desgracia no -r
.. que es la única opción correcta que tiene sentido con un solo carácter -
(si vamos por las convenciones POSIXy UNIXy)! )
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-11-22 10:56:06
Esto funcionó para mí, y produce una lista de nombres:
$Thisfile=(get-childitem -path 10* -include '*.JPG' -recurse).fullname
Lo encontré usando get-member -membertype properties
, un comando increíblemente útil. la mayoría de las opciones que le da se adjuntan con un .<thing>
, como fullname
está aquí. Puedes pegar el mismo comando;
| get-member -membertype properties
Al final de cualquier comando para obtener más información sobre las cosas que puede hacer con ellos y cómo acceder a ellos:
get-childitem -path 10* -include '*.JPG' -recurse | get-member -membertype properties
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-01-28 11:08:24