How to stop delayedexpansion from changing "free text"
Posted: 24 Oct 2011 15:26
This script does what I want unless the text file it processes contains exclamation marks.
This is a tiny portion of the input file ~TESTx.CSV
The desired output TEST.CSV should be exactly the same excepting that the index number at the start of each line should be in sequence starting at 201
(Firefox downloads history is broken and I wish to join the pieces.)
As a sanity test the file _TEST.CSV does the same operations on the file, but prepends the text with the original index count,
and this shows how much is removed from lines 17 and 18
A crude solution is to avoid delayed expansion and replace
with
and create a function
But I am hoping for something more elegant,
I still remember DOS making the floppy disc read the entire script to get back to where it was at ! !
Regards
Alan
Code: Select all
@ECHO OFF
setlocal enabledelayedexpansion
SET FILE=TESTx.CSV
IF EXIST %FILE% DEL %FILE%
IF EXIST _%FILE% DEL _%FILE%
SET N=200
for /f "tokens=1* delims=," %%d in ('type ~%FILE%') do (
ECHO %%d,%%e>> _%FILE%
SET /A N+=1
ECHO !N!,%%e>> %FILE%
)
FC ~%FILE% _%FILE%
PAUSE
GOTO :EOF
This is a tiny portion of the input file ~TESTx.CSV
Code: Select all
16,"sysexp-x64.zip","http://www.nirsoft.net/utils/sysexp-x64.zip","file:///E:/Downloads/sysexp-x64.zip","",1315585017060000,1315585020958000,1,,"",52836,52836,"application/zip","",0,0
17,"!Default_v200.zip","http://www.blackviper.com/!Default_v200.zip","file:///E:/Downloads/!Default_v200.zip","",1315655804198000,1315655821601000,1,,"",1263,1263,"application/zip","",0,0
18,"!Safe_v200.zip","http://www.blackviper.com/!Safe_v200.zip","file:///E:/Downloads/!Safe_v200.zip","",1315655839792000,1315655852243000,1,,"",1295,1295,"application/zip","",0,0
19,"PDF.zip","http://c1236872.com/PDF.zip","file:///E:/Downloads/PDFVManual.zip","",1315684437750000,1315684455821000,1,"http://www.tracker/pdf","",7002554,7002554,"application/x-zip","",0,0
20,"Port.zip","http://c1236872.com/Port.zip","file:///E:/Downloads/Port.zip","",1315684645421000,1315684664289000,1,"http://www.tracker/pdf-view","",7304310,7304310,"application/x-zip","",0,0
The desired output TEST.CSV should be exactly the same excepting that the index number at the start of each line should be in sequence starting at 201
(Firefox downloads history is broken and I wish to join the pieces.)
As a sanity test the file _TEST.CSV does the same operations on the file, but prepends the text with the original index count,
and this shows how much is removed from lines 17 and 18
Code: Select all
16,"sysexp-x64.zip","http://www.nirsoft.net/utils/sysexp-x64.zip","file:///E:/Downloads/sysexp-x64.zip","",1315585017060000,1315585020958000,1,,"",52836,52836,"application/zip","",0,0
17,"//www.blackviper.com////E:/Downloads/Default_v200.zip","",1315655804198000,1315655821601000,1,,"",1263,1263,"application/zip","",0,0
18,"//www.blackviper.com////E:/Downloads/Safe_v200.zip","",1315655839792000,1315655852243000,1,,"",1295,1295,"application/zip","",0,0
19,"PDF.zip","http://c1236872.com/PDF.zip","file:///E:/Downloads/PDFVManual.zip","",1315684437750000,1315684455821000,1,"http://www.tracker/pdf","",7002554,7002554,"application/x-zip","",0,0
20,"Port.zip","http://c1236872.com/Port.zip","file:///E:/Downloads/Port.zip","",1315684645421000,1315684664289000,1,"http://www.tracker/pdf-view","",7304310,7304310,"application/x-zip","",0,0
A crude solution is to avoid delayed expansion and replace
SET /A N+=1
ECHO !N!,%%e>> %FILE%
with
CALL :New_Index %%e
and create a function
Code: Select all
:New_Index
SET /A N+=1
ECHO %N%,%1>> %FILE%
GOTO :EOF
But I am hoping for something more elegant,
I still remember DOS making the floppy disc read the entire script to get back to where it was at ! !
Regards
Alan