I am trying to export files from a data dump and I am in dire need of some assistance. All of the files that I am trying to export are in pdf, doc, xlsx, jpg, and png format. Due to how the data dump was assembled, the files were renamed to f0.pdf, f0.doc, etc., In addition, the files are found in different subfolders (ex: Data\000\004\0000001212). Furthermore, within a subfolder, if there is a file in there it is accompanied by a m.xml file (for reference please see pic here). The m.xml file is important as it contains the original filename reflected by the "LDDOCUMENTNAME" field:
ex: <TextVar length="255" field="LDDOCUMENTNAME">ABC.pdf</TextVar>
I attempted to rename and export the files using PowerShell however some of the pdf files did not go through (I searched for all the pdf files in the subfolders and compared it to the number of exported pdf files).
This is what my script looks like:
$fsoFiles = Get-ChildItem -Path C:\Files -Filter *m.xml* -Recurse
ForEach($fsoFile in $fsoFiles)
{
$docM = Select-String $fsoFile -Pattern "LDDOCUMENTNAME"
$txtNewFile = $docM.Line.Substring(0,($docM.Line.Length-10))
$txtNewFile = $txtNewFile.Split(">")[-1]
$txtExtension = $txtNewFile.Split(".")[-1]
$txtOldFile = ([string]$fsoFile.Directory+"\"+"f0."+$txtExtension)
Copy-Item $txtOldFile C:\Extracted\$txtNewFile
}
Essentially I asked PowerShell to search through all the subfolders and filter out only the folders with a m.xml file. PowerShell is then supposed to rename the corresponding file back to its original filename using the value found in the "LDDOCUMENTNAME" field.
When I run my script I am presented with a bunch of these error messages:
You cannot call a method on a null-valued expression.
At line:6 char:5
+ $txtNewFile = $docM.Line.Substring(0,($docM.Line.Length-10))
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
I'm assuming this is the reason why PowerShell could not export some of the pdf files? Maybe the "LDDOCUMENTNAME" field in the corresponding m.xml files are blank?
I tried adding a IF statement inside of my FOR loop to see if I could get a location of the files that could not be exported but I was met with the same error messages:
If ($docM = $null)
{
Get-ChildItem -Path C:\Files -include !$docM -Recurse -Force -Name C:\Extracted\listofPaths.txt
}
else
Does anybody here know of a way to accomplish this? I am literally pulling my hair out. Any help would be much appreciated. Thanks!