Link to home
Start Free TrialLog in
Avatar of pkromer
pkromer

asked on

batch file to read csv files, replace text

I need to read all the csv files in a folder and get rid of some characters...

PO#
PO# (there is a space at the end)

I dont want to rename the files, just want to go through them and get rid of those characters. This is on a W2008 Server 64 bit. Thank you.
Avatar of knightEknight
knightEknight
Flag of United States of America image

You can use this command-line utility to replace (or remove) strings from multiple files in one command:

   http://www.paulslore.com/utils/chgstr.zip

example:

  chgstr.exe  "PO#"  ""  *.csv  /s
This would be easily scripted in autohotkeys.  Check out www.autohotkey.com.  I find it a lot more flexible and useful than batch file processing.  You would need a File reading loop and a regexreplace commmand to find and remove the undesired characters.  If you want to take this approach and need some help look me up here or ask on the AHK forums (after reading the command documentation first of course) :-D
Avatar of Bill Prew
Bill Prew

I highly recommend the Funduc Search & Replace tool, it can do pretty much anything you want from a replace tool.  Costs a few bucks, but well worth it.

http://www.funduc.com/

Two other tools I have used with sime success, but not as powerful as the above tool.

http://www.wingrep.com/
http://www.powergrep.com/grep.html

Hope this helps.

~bp
Avatar of pkromer

ASKER

The problem is i need to give this to someone that won't be able to use a seperate utility. It needs to be as simple as possible, like just hitting a batch file beore he opens the csv files. It's going to need to be used a few times a day, hence the need for a one-click operation.
Avatar of pkromer

ASKER

and, it's ALWAYS going to find those characters. The logic will never change.
Could certainly be done with a BAT file, can you provide a sample of the data?

~bp
Avatar of pkromer

ASKER

sure...

085856,,SO# 170545,PO# 30537,F/C,1,0000000
085674,,SO# 232455,PO# 4356,F/C,1,0000000

etc. What needs to go away is the SO# and the PO# , leaving just the numbers that follow. The folder will have multiple csv files in it, so the batch file will run through all of them and do this, leaving the names intact. Is that possible?
Avatar of pkromer

ASKER

itkamaraj:
I tried that and it works well, but it is meant for one file at a time. Is there any way to modify it so it goes through ALL the csv files in a folder?
Hope this will work for you
@echo off 
setLocal enabledelayedexpansion 
for %b in (*.csv) do
for /f "tokens=* delims=" %%a in (%b) do ( 
set tl=%%a 
Set tl=!tl:search=replace! 
echo !tl!>>tmp.rtf ) 
del %b & ren tmp.rtf %b

Open in new window

Give this a try.  Could probably tidy it up a bit more, and add some more error checking if needed but works for me.

@echo off  
Setlocal enabledelayedexpansion  
rem cd /d c:\startpath
:: %%f gets set to filename in a loop using output of dir command
:: Runs down each line of the files, setting %%L to each line in turn, replaces the two strings in each line
:: into a temporary CSV file and copies it back over the original filename

for /f "tokens=*" %%f in ('dir /b /a-d *.csv') do (
  echo Working on %%f
  del csv.tmp 2>NUL
  for /f "tokens=*" %%L in ('type "%%f"') do (  
    set line=%%L
    set line=!line:PO# =!  
    set line=!line:SO# =!  
    echo !line!
   )>>csv.tmp
   if exist csv.tmp (
     copy csv.tmp "%%f" /Y >NUL
   ) ELSE (
     echo No output found for "%%f"
   )
)
del csv.tmp 2>NUL

Open in new window

Avatar of pkromer

ASKER

That just blinks a cmd window. I tried a pause on the next line after the (*.csv) do, but it won't. It looks like it's not acceptiong the wildcard, maybe.
is that my one or the other? I tested mine on a sample of your data. as it stands run it from the same dir as the csv files - will check the code posted ok shortly.  Do you get any output?

The other batch file posted has a couple of errors in it as it stands ( % instead of %% etc) so maybe that is the one you ran?

Steve
ASKER CERTIFIED SOLUTION
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pkromer

ASKER

That is friggin beautiful. Thanks very much, its perfect.
No problem, glad you liked it :-)

Steve
Avatar of pkromer

ASKER

I have another question about this, totally new direction, will post seperate.