Batch to edit text file

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
murrycAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Paul TomasiConnect With a Mentor Commented:
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
 
Paul TomasiCommented:
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
 
murrycAuthor Commented:
The batch outputs a text file with just the following for each hour...

11
11
11
11
11
11
11
11
11
11
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Paul TomasiCommented:
@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
 
Paul TomasiCommented:
Yes, apologies... I was editing the abvove comment while you were testing it.
0
 
Paul TomasiCommented:
Apologies.... Give me a few moments to actually test the code before submitting....
0
 
Paul TomasiCommented:
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
 
Paul TomasiCommented:
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
 
Paul TomasiCommented:
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
 
Bill PrewCommented:
In your original code change:

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

to:

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

~bp
0
 
Bill PrewCommented:
Yikes, a lot of updates while coffee was brewing there...

~bp
0
 
murrycAuthor Commented:
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
 
murrycAuthor Commented:
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
 
Paul TomasiCommented:
Apologies... Just received my mail (was absent for 24 hours due to no network connection). Will reply in a mo...
0
 
Paul TomasiCommented:
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
All Courses

From novice to tech pro — start learning today.