Solved

Batch to edit text file

Posted on 2012-04-13
16
358 Views
Last Modified: 2012-07-28
I have the following batch code below to scan the attached file to remove hours within the file.  This is a music log with songs scheduled for a 24 hour period.  The problem is that the batch command worked when the lines had quotes around the hours.  This new log does not use quotes.

So I need the below edited to scan for and remove hour lines 05:00:00 - 10:00:00 from the attached.  Right now it looks for the quotes, which do not exist in the new music log files (attached). I also need the code to output the final file as the same filename, but with the .asc extension.

OutFile=%~dpn0-filtered.txt
if exist "%OutFile%" del "%OutFile%"
for /f "delims=" %%a in ('type "%InFile%"') do (
  set Print=0
  set Time=%%a
  set /a Hour=1!Time:~1,2!
  if "!Hour!" LSS "150" set Print=1
  if "!Hour!" GTR "111" set Print=1
  if !Print!==1 (
    echo %%a
    >>"%OutFile%" echo %%a
120415.txt
0
Comment
Question by:murryc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 3
  • 2
16 Comments
 
LVL 11

Expert Comment

by:paultomasi
ID: 37841450
Try this:

NOTE 2: You will need to add the name of your input file to Line 2.
NOTE 2: How is the output filename passed to your batch file? Shuldn't Line 3 be:

    set OutFile=%~dpn1-filtered.asc

where the filename is passed as a command line parameter when running the batch file?  
@echo off
set InFile=%~f1
set OutFile=%~dpn1-filtered.asc

del "%OutFile%" 2>nul
for /f "tokens=1 usebackq delims=:" %%a in ("%InFile%") do (
  set Hour=%%a
  if "!Hour:~0,1!" equ "0" set Hour=!Hour:~1,1!
  if !Hour! geq 5 (
    if !Hour! leq 10 (
      echo %%a>nul
    ) else (
      echo %%a
      echo %%a>>"%OutFile%"
    )
  )
)

Open in new window

0
 

Author Comment

by:murryc
ID: 37841488
The batch outputs a text file with just the following for each hour...

11
11
11
11
11
11
11
11
11
11
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37841503
@echo off
setlocal enabledelayedexpansion

set InFile=%~1
set OutFile=%~dpn1-filtered.asc

del "%OutFile%" 2>nul
for /f "tokens=1* usebackq delims=:" %%a in ("%InFile%") do (
  set Hour=%%a
  if "!Hour:~0,1!" equ "0" set Hour=!Hour:~1,1!
  if !Hour! geq 5 (
    if !Hour! leq 10 (
      echo %%a>nul
    ) else (
      echo %%a:%%b
      echo %%a:%%b>>"%OutFile%"
    )
  )
)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Expert Comment

by:paultomasi
ID: 37841504
Yes, apologies... I was editing the abvove comment while you were testing it.
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37841516
Apologies.... Give me a few moments to actually test the code before submitting....
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37841528
Here, please try this:
@echo off
setlocal enabledelayedexpansion

set InFile=%~1
set OutFile=%~dpn1-filtered.asc

del "%OutFile%" 2>nul
for /f "tokens=1* usebackq delims=:" %%a in ("%InFile%") do (
  set Hour=%%a
  if "!Hour:~0,1!" equ "0" set Hour=!Hour:~1,1!

  if !Hour! lss 5 (
    echo %%a:%%b
    echo %%a:%%b>>"%OutFile%"
  ) else (
    if !Hour! geq 10 (
      echo %%a:%%b
      echo %%a:%%b>>"%OutFile%"
    )
  )
)

Open in new window

0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37841568
This code also works okay.
@echo off
setlocal enabledelayedexpansion

set InFile=%~1
set OutFile=%~dpn1-filtered.asc

del "%OutFile%" 2>nul
for /f "tokens=* usebackq" %%a in ("%InFile%") do (
  set Line=%%a
  if "1!Line:~0,2!" lss "105" (
    echo %%a
    echo %%a>>"%OutFile%"
  ) else (
    if "1!Line:~0,2!" geq "110" (
      echo %%a
      echo %%a>>"%OutFile%"
    )
  )
)

Open in new window

0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37841595
You might want to change 'GEQ' to 'GTR' in Line 14 above if you DO NOT want times between 10:00..10:59. This will then give you times 00:00..04:59 and 11:00 onwards.
0
 
LVL 56

Expert Comment

by:Bill Prew
ID: 37841995
In your original code change:

  set /a Hour=1!Time:~1,2!

to:

  set /a Hour=1!Time:~0,2!

~bp
0
 
LVL 56

Expert Comment

by:Bill Prew
ID: 37841998
Yikes, a lot of updates while coffee was brewing there...

~bp
0
 

Author Comment

by:murryc
ID: 37853624
that works great Paul.

2 more small tweaks if I may (tell me if I need to open a new question for these). The input file will always be dropped into the same folder and the output file will need to be placed in a different location since the filename will end up being the same.

So let's say the input needs to execute against all files in \\server\share\ that have the *.asc extension and then output the changed files to \\server\changed\*.asc.
0
 

Author Comment

by:murryc
ID: 37853641
Paul, anyway to have it make these changes to a tmp file and then rename it back to the original file after it is done?  I like what you did here...

http://www.experts-exchange.com/Programming/Misc/Q_27674015.html

It scans, changes, writes to a temp file then looks like maybe it deletes the original and renames the temp as the original name?  Not sure, but the end result is to combine both of these solutions from both questions.
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37854806
Apologies... Just received my mail (was absent for 24 hours due to no network connection). Will reply in a mo...
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37855732
Yes, the program is designed and written to replace the contents of the original file with the new data, but this can easily be changed.

Where you write: "2 more small tweaks..." in http:#37853624, which program do you want me tweak: is it http:#37841528 or http:#37841568 ?

Assuming it's http:#37841568, the following program reads all .ASC files in \\server\share\ and writes it's outputs to \\server\changed\ retaining the original filenames.
@echo off
setlocal enabledelayedexpansion

set "source=\\server\share"
set "destination=\\server\changed"

(for %%F in ("%source%\*.asc") do (
  echo %%~fF >con
  call :processfile "%%~fF"
))>"%destination%\%~nxF"
exit /b 0


rem ---------------------------------------------------
rem PROCESS FILE
rem ---------------------------------------------------
:processfile
  for /f "tokens=* usebackq" %%L in ("%~1") do (
    set "Line=%%L"
    if "1!Line:~0,2!" lss "105" (
      echo %%L
    ) else (
      if "1!Line:~0,2!" geq "110" echo %%L
    )
  )
goto :eof

Open in new window

If you do not require output to the screen then simply remove line 8.

(Untested, so please let me know if it requires further work)
0
 
LVL 11

Accepted Solution

by:
paultomasi earned 500 total points
ID: 37855879
Here's a shorter (and faster) version of the above program:
@echo off
setlocal enabledelayedexpansion

set "source=\\server\share"
set "destination=\\server\changed"

for %%a in ("%source%\*.asc") do (
  echo %%~fa >con
  (for /f "tokens=* usebackq" %%b in ("%%~fa") do (
    set "Line=%%b"
    if "1!Line:~0,2!" lss "105" (
      echo %%b
    ) else (
      if "1!Line:~0,2!" geq "110" echo %%b
    )
  ))>"%destination%\%~nxa"
)
exit /b 0

Open in new window

As before, remove line 8 if you don't require output to the screen.
0

Featured Post

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Progress

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question