我希望你能帮助我。 我有很多csv文件(90000) 我需要在文件中使用名称更改文件名。 在这个例子中,我想搜索我们找到的第二行的第13个分隔符" 020 / NOT-232032/2013"然后用这个名称重命名csv文件。 有没有办法用Powershell,VBS,cmd或其他方式来做到这一点? 请帮忙 thakx
示例File1.csv - > 020-NOT-232032-2013.doc :
File1.csv
线路1:
" statoDoc"" idDoc"" tipoCliente"" tipoDoc"" idUtente"&# 34; oraDoc"" indirizzo"" idTerminale"" ragioneSociale"" codiceFiscale"" idClienteSede&#34 ;," idCessionario"" nrDoc"" dataFirma"" LOCALITA"" partitaIva"&#34 ; firmatario"" idDestinatario"" dataDoc"
2号线
" C"" 232032"""" NOT"" 020"&# 34; 00:19"," CASTELLO DI FE"," 020"," PANI c"," 00624330221",&# 34; 200673",""," 020 / NOT-232032/2013"," 2013.08.20"," CASTE MME&#34 ;," IT00624330221"""" 102796"" 2013年8月20日"
答案 0 :(得分:1)
以下是使用PowerShell的两种可能的解决方案:
导入csv-data。更容易阅读。
Get-ChildItem -Filter "*.csv" -Path "c:\folderwithCSVs" -Recurse | % {
$filename = (Import-Csv $_.FullName)[0].nrDoc.Replace('"',"").Replace("/","-").Trim() + ".doc"
Rename-Item -Path $_.FullName -NewName $filename
}
使用文本解析。推荐用于大型csv文件。
Get-ChildItem -Filter "*.csv" -Path "c:\folderwithCSVs" -Recurse | % {
#Open file
$reader = New-Object System.IO.StreamReader $_.FullName
#Ignore first line
$reader.ReadLine() | out-null
#Get name
$filename = $reader.ReadLine().Split(",")[12].Replace('"',"").Replace("/","-").Trim() + ".doc"
#Close stream
$reader.Close()
#Rename file
Rename-Item -Path $_.FullName -NewName $filename
}
答案 1 :(得分:0)
你需要一个计划。
,"020/NOT-232032/2013",
图案化的RegExp),将字段转换为名称,关闭,重命名WTF
>> fld = """020/NOT-232032/2013"""
>> nam = Replace(Mid(fld, 2, Len(fld) - 2), "/", "-") & ".doc"
>> WScript.Echo fld, "=>", ">" & nam & "<"
>>
"020/NOT-232032/2013" => >020-NOT-232032-2013.doc<
答案 2 :(得分:0)
这是一个VBScript选项:
Option Explicit
Dim objFSO 'File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objDir
Set objDir = objFSO.GetFolder("C:\MyFiles")
Dim objFile
Const ForReading = 1
For Each objFile In objDir.Files
If(UCase(Mid(objFile.Name, InStrRev(objFile.Name, "."))) = ".CSV") Then
Dim objFileTS
Set objFileTS = objFSO.OpenTextFile(objFile.Path, ForReading, False)
objFileTS.SkipLine()
Dim strToken
'Read the second line, split it into an array on the "," char, and get the
'13th token in the array (array's are zero-indexed, so it's the 12th index).
strToken = Split(objFileTS.ReadLine(), ",")(12)
'remove quotes.
strToken = Replace(strToken, Chr(34), "")
'replace "/" with "-".
strToken = Replace(strToken, "/", "-")
objFileTS.Close()
Dim strNewPath
strNewPath = Left(objFile.Path, InStrRev(objFile.Path, "\")) & strToken & ".doc"
objFile.Move(strNewPath) 'move to the same place with a different name is the only way to rename.
End If
Next
答案 3 :(得分:0)
这是一个扩展@ roryap答案的VBS解决方案
Option Explicit
Const PATH = "C:\folder\where\csvs\sit"
Dim fso, file
Dim cn,rs
Dim val
Set cn = CreateObject("ADODB.Connection")
Set fso = CreateObject("Scripting.FilesystemObject")
'HDR = yes because your first row is data headers
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & PATH & ";" & _
"Extended Properties=""text;HDR=YES;FMT=Delimited"""
For Each file in fso.GetFolder(PATH).Files
Set rs = cn.Execute("Select * From [" & file.name & "]")
Do While Not rs.EOF
val = rs.Fields.Item("nrDoc").Value
If Not isNull(val) Then
val = Replace(val,"/","")
file.Move Left(file.Path, InStrRev(file.Path, "\")) & val & ".doc"
Exit Do
End If
rs.MoveNext
Loop
Next
WScript.Quit
当它在第13个插槽中找到一个值时,它将使用它然后忽略该CSV的其余部分,然后转到下一个CSV