0

I'm writting a script in Windows, a .BAT file, to replace:

Note that there is an ENTER, I mean the string is in two lines. And looks more complicated now: after the 2 double quotes there are 2 blank spaces and then the "ENTER"

""  
"2014

So, something like: (double quote)(double quote)(space)(space)(newline)(double quote)2014

I'm having problems with this. This is how the script looks like:

@echo off
setlocal enabledelayedexpansion
set INTEXTFILE=FILE_IN_CSV.csv
set OUTTEXTFILE=222.csv
set SEARCHTEXT=^"^"^"2014
set REPLACETEXT=^"^"^"4
set OUTPUTLINE=

for /f "tokens=1,* delims=¶" %%A in ( '"type %INTEXTFILE%"') do (
    SET string=%%A
    SET modified=!string:%SEARCHTEXT%=%REPLACETEXT%!

    echo !modified! >> %OUTTEXTFILE%
)
del %INTEXTFILE%
rename %OUTTEXTFILE% %INTEXTFILE%

Any ideas?

Thanks!

EDIT: adding source text example. See that there are 3 (not 2!) black spaces at the end of each line.

"2014091810000076";"Question about folder sizes";"2014-09-18 12:10:20";"2014-09-25 17:33:15";"Resolt Sistemes";"jbgsfnam";"Sistemes::Altres";"STFD";"peticions";""   
"2014091910000109";"Error opening word files with office 2013";"2014-09-19 09:50:07";"2014-09-25 14:27:59";"Resolt TRO";"yerttom";"Qualitat::Altres";"STFD";"peticions";""   

I just need to delete the 20 at the beginning of each line, but not the others 20 in the middle of lines.

user3139207
  • 121
  • 13

2 Answers2

0

Your approach can't work ever.

  1. Command for processes text always line by line. Therefore it is not possible to get 2 lines as string into an environment variable and modify this string.

  2. Paragraph symbol (code value B6 hexadecimal) is just a placeholder when viewing a text file with showing line endings. The CSV file contains carriage return (0D) and line-feed (0A) which command for interprets always as delimiter character independent on what you specify.

It would be better to use a text editor like UltraEdit or Notepad++ or Sublime Text to do the replace in the file. It is not even necessary to run a regular expression replace in those editors. But using as search string the Perl regular expression ""\s+"2014 and as replace string """4 would result in the modification you want.

There are also console applications written for replacing strings in files. Take a look at How can you find and replace text in a file using the Windows command-line environment?

Searching for "" \r\n"2014 and replacing it with """4 in the file using only standard commands is nevertheless possible using the code below.

@echo off
setlocal EnableDelayedExpansion
set "INTEXTFILE=FILE_IN_CSV.csv"

if not exist "%INTEXTFILE%" goto EndBatch

set "OUTTEXTFILE=%TEMP%\csv_replace.tmp"
if exist "%OUTTEXTFILE%" del "%OUTTEXTFILE%"

set "FirstLine="
set "EndFirstLine=""  "
set "BeginSecondLine="2014"

for /f "usebackq delims=" %%A in ("%INTEXTFILE%") do (
    set "Line=%%A"
    if not "!FirstLine!"=="" (
        if "!Line:~0,5!"=="!BeginSecondLine!" (
            set "FirstLine=!FirstLine!"!Line:~4!"
            echo !FirstLine!>>"%OUTTEXTFILE%"
            set "Line="
        ) else (
            echo !FirstLine!  >>"%OUTTEXTFILE%"
        )
        set "FirstLine="
    )
    if not "!Line!"=="" (
        if "!Line:~-4!"=="!EndFirstLine!" (
            set "FirstLine=!Line:~0,-2!"
        ) else (
            echo !Line!>>"%OUTTEXTFILE%"
        )
    )
)

if exist "%OUTTEXTFILE%" (
    copy "%OUTTEXTFILE%" "%INTEXTFILE%" >nul
    del "%OUTTEXTFILE%"
)

:EndBatch
endlocal

Empty lines are removed by this batch file, too. This cannot be avoided as command for ignores empty lines on parsing the file.

Community
  • 1
  • 1
Mofi
  • 46,139
  • 17
  • 80
  • 143
  • This script is not working for me. I edit the initial post with a code example. Thanks! – user3139207 Oct 15 '14 at 07:06
  • So you just want to trim trailing spaces and remove 2nd and 3rd character (century) from all lines. That is something completely different than what was described originally. I thought, there are line breaks within a field value in the CSV file and therefore it is necessary to concatenate two lines to a single line under certain conditions. So I wrote a completely wrong code for what is really needed. An input and output example is always best to describe such tasks. – Mofi Oct 15 '14 at 16:01
  • Yes, you are right sorry. I forget to post an example of source text. – user3139207 Oct 16 '14 at 06:09
0
@ECHO OFF
SETLOCAL
(
 FOR /f "delims=" %%a IN (q6288377.txt) DO (
 SET line=%%a
 SETLOCAL ENABLEDELAYEDEXPANSION
 ECHO(^"!line:~3!
 endlocal
 )
)>newfile.txt
(
 FOR /f "delims=" %%a IN (q6288377.txt) DO (
 SET line=%%a
 SETLOCAL ENABLEDELAYEDEXPANSION
 IF "!line:~1,2!"=="20" (ECHO(^"!line:~3!) else (echo(%%a)
 endlocal
 )
)>newfile2.txt

GOTO :EOF

It's incredibly difficult to solve a problem stated as "my code doesn't do what I want it to do" without an clear indication of desired outcome.

Your problem appears to be to delete the "20" in columns 2 and 3 of the text file (I changed the name to suit my system.)

The code above assumes that every line of your file starts ". The first part arbitrarily deletes whatever characters are in columns 2 and 3, creating newfile.txt. The second selects only those lines which have 20 in columns 2 and 3 and deletes those columns, leaving other lines alone and creates newfile2.txt.

You have changed the requirement from ""SpaceSpaceNewline"2014 to 3 terminal spaces, No indication of whether the first line is an exception (it wouldn't be preceded by ""SpaceSpaceNewline) and so the above may not be applicable. No idea whether the presence of the sequences is significant or not - and in all probability, a utility like sed is more suited to the task (Google is your friend)


[edit for extended question]

@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
PUSHD "%sourcedir%"
FOR %%u IN (q6288377*.txt) DO (
(
 FOR /f "usebackqdelims=" %%a IN ("%%u") DO (
 SET line=%%a
 SETLOCAL ENABLEDELAYEDEXPANSION
 ECHO(^"!line:~3!
 endlocal
 )
)>%%~nu.xtx
)
popd

GOTO :EOF

You would need to change the setting of sourcedir to suit your circumstances. File produced would be samename.xtx. Don't try to overwrite the existing files directly.

You could also create new files in a different directory by prefixing %%~nu.xtx with a directory name, eg "c:\some random\directory\%%~nu.xtx" - "enclose in quotes" if the directory or filenames contain separators (like spaces). The ~nu means "the name of %%u - see for /? from the prompt for more information.

It's always easier to solve an entire problem than to approach it piecemeal. A full description may influence the method used.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • This code is working perfectly, thanks! Let me ask you 1 last question: is it possible to modify this script to apply this to all the CSV files in a folder? – user3139207 Oct 15 '14 at 08:56