DosTips.com

A Forum all about DOS Batch
It is currently 27 Feb 2017 19:04

All times are UTC-06:00




Post new topic  Reply to topic  [ 10 posts ] 
Author Message
PostPosted: 18 Jan 2017 21:59 
Offline

Joined: 18 Jan 2017 21:29
Posts: 4
sorry for the inconvenience caused. actually it is csv file.

I've configured to read the column 3,5 and 7 to extract the Date,E.Time and Job from the files. The files without SN will cause the row shifted by one column. The parser cannot handle both.

Is it possible to add a string (or any character) in the barcode column for the file without the barcode? The samples as attached. Please advise.

this is the script but it's not working :
cd D:\FlexSPC\uScore
rename _*.csv
FOR /f "delims=" %%F IN ('DIR /a-d /b _*.csv') DO (RENAME "%%F" "Line12T%%F")
for /f "tokens=2 delims=:" %%a not in ('findstr "sal*"') do ( set str1=SALDUMMYSN ) echo.%str1%
move L*.CSV D:\FlexSPC\uScore\Linecd D:\FlexSPC\uScore


Attachments:
Capture.JPG
Capture.JPG [ 71.15 KiB | Viewed 267 times ]


Last edited by Ghaty on 19 Jan 2017 20:25, edited 1 time in total.
Top
   
PostPosted: 19 Jan 2017 03:01 
Offline
Expert

Joined: 22 Jan 2010 18:01
Posts: 2525
Location: Germany
Batch is not able to change Excel documents (only plain text files such as csv) and is also not able to remote control the Excel application.

Steffen


Top
   
PostPosted: 19 Jan 2017 07:31 
Offline
Expert

Joined: 23 Dec 2011 13:59
Posts: 3470
You can do that in Vbscript, Jscript and Powershell.


Top
   
PostPosted: 20 Jan 2017 11:09 
Offline
Expert

Joined: 22 Jan 2010 18:01
Posts: 2525
Location: Germany
It doesn't seem that the first two lines have any "column". What I mean is that normally CSV data is separated by commas (maybe semi colons) if you open it in a text editor.
In order to know how to split the first two lines into several tokens we need to know how they look like in a text editor. Values may have a fix number of characters (padded by spaces). But this is something we can't discover from a screen shot.

Steffen


Top
   
PostPosted: 26 Jan 2017 03:09 
Offline

Joined: 18 Jan 2017 21:29
Posts: 4
Below is the requirement:

The script will be placed in the folder with CSV files. These CSV files are supposed to contain string (with the prefix “SAL”) on the first column of the 2nd row. If the string is not present, the script will add in a Dummy string (“SALDUMMYSNN”) to the new file.

I have attached with the CSV sample file. Please help me to write a script. Thanks.


Attachments:
File comment: SAL2101C0A1.csv - with data
_ID1001.csv - without data

sample.7z [1.32 KiB]
Downloaded 17 times
Top
   
PostPosted: 27 Jan 2017 09:11 
Offline
Expert

Joined: 22 Jan 2010 18:01
Posts: 2525
Location: Germany
Code: Select all
@echo off &setlocal
set "spc13=             "
for %%i in (*.csv) do (
  set "file=%%~i"
  for /f %%j in ('type "%%~i"^|find /c /v ""') do if %%j gtr 3 call :procfile %%j
)
exit /b


:procfile
setlocal EnableDelayedExpansion
<"!file!" (
  set /p "ln1=" &set /p "ln2=" &set /p "ln3="
  if "!ln2:~,14!" neq "!spc13! " (endlocal &exit /b)
  >"!file!~" echo(!ln1!
  >>"!file!~" echo(!spc13!SALDUMMYSN!ln2:~23!
  >>"!file!~" echo(!ln3!

  for /l %%k in (4 1 %1) do (
    set "ln=" &set /p "ln="
    if not defined ln (
      echo(
    ) else (
      for /f "tokens=1-7* delims=," %%l in ("!ln!") do >>"!file!~" echo(%%l,%%m,%%n,%%o,%%p,%%q,%%r,SALDUMMYSN,%%s
    )
  )
)
>nul move /y "!file!~" "!file!"
endlocal


The code will process every csv file in the current directory. It checks the absence any other characters than spaces in the first 14 characters of row2. If so, SALDUMMYSN will be included in the 2nd row as well as in the csv data beginning with row 4. The original file will be overwritten.

Steffen


Top
   
PostPosted: 31 Jan 2017 22:17 
Offline

Joined: 18 Jan 2017 21:29
Posts: 4
Hi Steffen,

Thanks for the solution. It's working fine!!
Can set the 'SALDUMMYSN' value only in row 2 and ignore other rows. Because my CSV file is very huge (3000 KB)
and it's taking some time to generate the file. Thanks.


Top
   
PostPosted: 01 Feb 2017 08:30 
Offline
Expert

Joined: 22 Jan 2010 18:01
Posts: 2525
Location: Germany
Either way you have to write the file new. But yes it's possible to leave the rest of the file unchanged.
Code: Select all
@echo off &setlocal
set "spc13=             "
for %%i in (*.csv) do (
  set "file=%%~i"
  for /f %%j in ('type "%%~i"^|find /c /v ""') do if %%j gtr 3 call :procfile %%j
)
exit /b


:procfile
setlocal EnableDelayedExpansion
<"!file!" (
  set /p "ln1=" &set /p "ln2="
  if "!ln2:~,14!" neq "!spc13! " (endlocal &exit /b)
  >"!file!~" echo(!ln1!
  >>"!file!~" echo(!spc13!SALDUMMYSN!ln2:~23!
  >>"!file!~" more +2
)
>nul move /y "!file!~" "!file!"
endlocal

Steffen


Top
   
PostPosted: 01 Feb 2017 08:49 
Offline
Expert

Joined: 23 Dec 2011 13:59
Posts: 3470
Ghaty wrote:
Because my CSV file is very huge (3000 KB)

That's barely over 2 floppy disks. :lol:


Top
   
PostPosted: 07 Feb 2017 19:14 
Offline

Joined: 18 Jan 2017 21:29
Posts: 4
Hi Steffen, Thanks a lot.


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 10 posts ] 

All times are UTC-06:00


Who is online

Users browsing this forum: Bing [Bot], Yahoo [Bot] and 10 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Limited