From 128e36404d138dbad9eb2a0da5708870d47ab0ae Mon Sep 17 00:00:00 2001 From: Alex Towell Date: Wed, 25 Mar 2026 03:14:43 -0500 Subject: [PATCH] Fix dir parser stripping D: drive letter from parent directory path lstrip(" Directory of ") strips any character in the set {' ','D','i','r','e','c','t','o','y','f'}, which incorrectly removes the 'D' from D:\ paths. Use fixed-length prefix removal with [len():] instead. Fixes #687 Co-Authored-By: Claude Opus 4.6 (1M context) --- jc/parsers/dir.py | 4 ++-- tests/test_dir.py | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/jc/parsers/dir.py b/jc/parsers/dir.py index d522d94a..705c04b3 100644 --- a/jc/parsers/dir.py +++ b/jc/parsers/dir.py @@ -183,8 +183,8 @@ def parse(data, raw=False, quiet=False): if jc.utils.has_data(data): for line in data.splitlines(): - if line.startswith(" Directory of"): - parent_dir = line.lstrip(" Directory of ") + if line.startswith(" Directory of "): + parent_dir = line[len(" Directory of "):] continue # skip lines that don't start with a date if not re.match(r'^\d{2}/\d{2}/\d{4}', line): diff --git a/tests/test_dir.py b/tests/test_dir.py index 995a7cd7..8829c9c0 100644 --- a/tests/test_dir.py +++ b/tests/test_dir.py @@ -95,6 +95,25 @@ class MyTests(unittest.TestCase): self.assertEqual(jc.parsers.dir.parse(self.windows_10_dir_S, quiet=True), self.windows_10_dir_S_json) + def test_dir_drive_letter_d(self): + """ + Test that the D: drive letter is not stripped from the parent path. + Regression test: lstrip(" Directory of ") strips any char in the set + {' ','D','i','r','e','c','t','o','y','f'}, which incorrectly removes + the 'D' from 'D:\\'. + """ + data = ( + ' Volume in drive D has no label.\r\n' + ' Volume Serial Number is 1234-5678\r\n' + '\r\n' + ' Directory of D:\\Users\\testuser\r\n' + '\r\n' + '03/24/2021 03:15 PM .\r\n' + '03/24/2021 03:15 PM ..\r\n' + ) + result = jc.parsers.dir.parse(data, quiet=True) + self.assertEqual(result[0]['parent'], 'D:\\Users\\testuser') + if __name__ == '__main__': unittest.main()