Extraer el nombre de archivo de una ruta
Quiero extraer el nombre del archivo de la ruta de abajo:
D:\Server\User\CUST\MEA\Data\In\Files\CORRECTED\CUST_MEAFile.csv
Ahora escribí este código para obtener el nombre del archivo. Esto funciona bien siempre y cuando el nivel de carpeta no cambie. Pero en caso de que se haya cambiado el nivel de carpeta, este código debe reescribirse. Estoy buscando una manera de hacerlo más flexible, como el código siempre puede extraer el nombre del archivo independientemente del nivel de carpeta.
($outputFile).split('\')[9].substring(0)
6 answers
Si está de acuerdo con incluir la extensión, esto debería hacer lo que quiera.
$outputPath = "D:\Server\User\CUST\MEA\Data\In\Files\CORRECTED\CUST_MEAFile.csv"
$outputFile = Split-Path $outputPath -leaf
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-05 11:12:08
Use. NET:
[System.IO.Path]::GetFileName("c:\foo.txt")
devuelve foo.txt
.
[System.IO.Path]::GetFileNameWithoutExtension("c:\foo.txt")
devuelve foo
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
2017-06-04 22:31:37
Usando el nombre base en Get-ChildItem muestra el nombre del archivo y y usando Nombre muestra el nombre del archivo con la extensión.
$filepath = Get-ChildItem "E:\Test\Basic-English-Grammar-1.pdf"
$filepath.BaseName
Basic-English-Grammar-1
$filepath.Name
Basic-English-Grammar-1.pdf
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
2018-01-13 18:19:35
Usted podría obtener el resultado que desea de esta manera.
$file = "D:\Server\User\CUST\MEA\Data\In\Files\CORRECTED\CUST_MEAFile.csv"
$a = $file.Split("\")
$index = $a.count - 1
$a.GetValue($index)
Si usa "Get-ChildItem" para obtener el "nombre completo", también podría usar "name" para obtener el nombre del archivo.
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-05 11:05:49
$(Split-Path "D:\Server\User\CUST\MEA\Data\In\Files\CORRECTED\CUST_MEAFile.csv" -hoja)
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
2017-10-02 18:29:00
Get-ChildItem "D:\Server\User\CUST\MEA\Data\In\Files\CORRECTED\CUST_MEAFile.csv" |Select-Object -ExpandProperty 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
2018-06-08 09:44:46