Solved

Batch to edit text file

Posted on 2012-04-13
16
353 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
  • 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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 51

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 51

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

744 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now