Powershell是否从路径名中选择字符串?
我有一个带有目录路径的CSV文件,我需要从中提取一些信息来生成日志文件名。不知道怎么做? 选择字符串 CSV文件如下所示Powershell是否从路径名中选择字符串?,powershell,csv,Powershell,Csv,我有一个带有目录路径的CSV文件,我需要从中提取一些信息来生成日志文件名。不知道怎么做? 选择字符串 CSV文件如下所示 User,Computer,Directory john.doe,CAD-12,C:\Cimatron\CimatronE11_64bit\Program\IT\var\post john.doe,CAD-12,C:\Cimatron\CimatronE11_64bit\Program\IT\var\post2 john.doe,CAD-12,C:\Cimatron\Cima
User,Computer,Directory
john.doe,CAD-12,C:\Cimatron\CimatronE11_64bit\Program\IT\var\post
john.doe,CAD-12,C:\Cimatron\CimatronE11_64bit\Program\IT\var\post2
john.doe,CAD-12,C:\Cimatron\CimatronE11_64bit\Data\templates
john.doe,CAD-12,C:\Program Files\CGTech\VERICUT 7.3.3\library
john.doe,CAD-12,C:\Program Files\CGTech\VERICUT 7.4\library
我知道我必须导入CSV文件,以下是我所拥有的
$UsersCSV = import-csv C:\test.csv
foreach($Computer in $UsersCSV)
{$Logfile = Select-String "not sure how to search the string for what I want"
$LogFile
}
需要有关选择字符串的帮助。下面是我想要结束的内容,或者是给$Logfile一个唯一名称的最简单的内容
CimE11_post
CimE11_post2
CimE11_templates
VERI_7.3.3_lib
VERI_7.4_lib
感谢您的帮助-replace是您的朋友,下面是一个示例,说明如何从您提供的场景中获得您想要的字符串 希望这有帮助,祝你好运 $file=导入csv$filename $file.directory中的foreach$dir{ 如果$dir-匹配VERI{ $dir.replace'C:\Program Files\CGTech\,.replace'rary',.replace'CUT',.replace'\, } 如果$dir-匹配Cima{ $dir.replace'C:\Cimatron\,.replace'atron',.replace'64bit\Data\,.replace'64bit\Program\IT\var\, } }在对以下问题的评论中提出了一个实用的解决方案:将输入路径中的实例替换为文件名中合法的字符,以获得用作文件名的每个路径的表示形式: 这将产生以下文件名:
C_Cimatron_CimatronE11_64bit_Program_IT_var_post.log
C_Cimatron_CimatronE11_64bit_Program_IT_var_post2.log
C_Cimatron_CimatronE11_64bit_Data_templates.log
C_Program_Files_CGTech_VERICUT_7.3.3_library.log
C_Program_Files_CGTech_VERICUT_7.4_library.log
正如在评论中所说的,在你想要的文件名背后没有明确的算法逻辑
这里有一个解决方案,可以生成所需的文件名,并尽量保持通用性。
这在现实生活中可能不值得做,但也许可以激发现实生活中的解决方案:
# Helper function that takes a directory path and converts it to a log filename
# representing that dir.
function pathToLogFilename([string] $path) {
# Ordered hashtable defining regex-based transformations (replacements).
# Entries are processed in order, and processing stops as soon as
# a match is found and the transformation has been performed.
# Entry format: <regex> = <replacement>, to be passed to the -replace operator.
$ohtTansform = [ordered] @{
'^(.{3}).+?(E\d+).*$' = '$1$2' # 'CimatronE11_64bit' -> 'CimE11', for instance.
'^(.{4}).+? (\d.*)$' = '$1_$2' # 'VERICUT 7.3.3' -> 'VER_7.3.3', for instance
'^(lib)rary$' = '$1' # 'library' -> 'lib'
'^(post.*|templates)$' = '$&' # preserve, if prefixed with 'post' or equal to 'templates'
'.+' = '' # remove all other tokens
}
$logFilename = ''; $i = 0
# Split the .Directory path into tokens (path components) and synthesize
# the log filename from a subset of the tokens, with transformations applied.
foreach($token in ($path -split '\\')) {
# Apply transformation.
$transformedToken = $token
foreach($regex in $ohtTansform.Keys) {
if ($token -match $regex) {
$transformedToken = $token -replace $regex, $ohtTansform[$regex]
break
}
}
# Add transformed token to filename, separated with "_".
if ($transformedToken) {
$logFilename += $(if ($i++) { '_' } else { '' }) + $transformedToken
}
}
# Output the synthesized filename.
$logFilename
}
Import-Csv C:\test.csv | % {
# Call the helper function to transform the directory path to a log filename.
$LogFile = pathToLogFilename $_.Directory
}
为日志文件指定唯一名称的最简单方法是计数器1.log、2.log、3.log、4.log。。。n、 日志。这样行吗?你所描述的看起来不可思议的复杂-脚本怎么知道“VERICUT”很重要,而“CGTech”不重要?如果将“VERICUT”缩短为“VERI”,那么“VERICUT”是有意义的,但是“templates”不应该缩短吗?如果您只是交换\\and:for-并获取C程序文件CGTech VERICUT 7.4-library.log呢?@tessellingheckler您可能是对的。我只是希望缩短日志名。我可能会使用这个方法,并保持它的简单。请。一个只使用示例输入的硬编码解决方案是没有帮助的。
# Helper function that takes a directory path and converts it to a log filename
# representing that dir.
function pathToLogFilename([string] $path) {
# Ordered hashtable defining regex-based transformations (replacements).
# Entries are processed in order, and processing stops as soon as
# a match is found and the transformation has been performed.
# Entry format: <regex> = <replacement>, to be passed to the -replace operator.
$ohtTansform = [ordered] @{
'^(.{3}).+?(E\d+).*$' = '$1$2' # 'CimatronE11_64bit' -> 'CimE11', for instance.
'^(.{4}).+? (\d.*)$' = '$1_$2' # 'VERICUT 7.3.3' -> 'VER_7.3.3', for instance
'^(lib)rary$' = '$1' # 'library' -> 'lib'
'^(post.*|templates)$' = '$&' # preserve, if prefixed with 'post' or equal to 'templates'
'.+' = '' # remove all other tokens
}
$logFilename = ''; $i = 0
# Split the .Directory path into tokens (path components) and synthesize
# the log filename from a subset of the tokens, with transformations applied.
foreach($token in ($path -split '\\')) {
# Apply transformation.
$transformedToken = $token
foreach($regex in $ohtTansform.Keys) {
if ($token -match $regex) {
$transformedToken = $token -replace $regex, $ohtTansform[$regex]
break
}
}
# Add transformed token to filename, separated with "_".
if ($transformedToken) {
$logFilename += $(if ($i++) { '_' } else { '' }) + $transformedToken
}
}
# Output the synthesized filename.
$logFilename
}
Import-Csv C:\test.csv | % {
# Call the helper function to transform the directory path to a log filename.
$LogFile = pathToLogFilename $_.Directory
}