Stripping a filename upto a certain subfolder in batch
Posted on 2014-03-19
I have a input file containing the format:
"X:\subX\file1.pdf" ,"newfile1.pdf",<some string>,<some string>
"X:\subX\keepsub\file2.pdf","newfile2.pdf", <some string>, <some string>
I want to write a batch that strips the file path of X, keeping the keepsub subfolder when it occurs and replace it with Z:\file1.pdf and Z:\keepsub\file2.pdf respectively. The keepsub when it exists is the lowest depth subfolder.
I have something like the below but it only replaces X: with Z: and strips all the subfolders including the keepsub.
if exist "%TargetFile%" del "%TargetFile%"
echo Processing '%SourceFile%' ...
for /f "tokens=1* delims=," %%a in ('type "%SourceFile%"') do (
>>"%TargetFile%" echo "%NewFolder%\%%~nxa",%%b
echo ... done.