Batch script which removes text from within files then copies files to an output folder.

Hi

I am looking at trying to write a batch script that searches files in a folder to remove lines and then copy the edited file to an output folder.
The contents of the files are as follows;

TW16   930.50
3C1H    1522
4C2       22
R10090035                       154  0.600    1.7
R10090045                        26  2.400    2.3
R10140035                         1  0.300    4.2
R10140035                         1  0.900    4.2

At the moment I can do this with findstr by specifying the file to be searched/edited but but i would like to ditch this and do this to all files in a folder if they are there and output them to each file once scrubbed.

Current batch script so far;

@echo off
setlocal
SET "EXPORTPATH=C:\export"
SET "DESTPATH=Y:\"
cls

:PROMPT
set /P JOBNO=Please enter the job number?

if EXIST %EXPORTPATH%\%JOBNO%.ftr (

echo Exporting Files
findstr /v /c:"R10140035" %EXPORTPATH%\%JOBNO%.ftr >"%DESTPATH%%JOBNO%.ftr"
del /q %EXPORTPATH%\*.ftr
for /d %%x in (%EXPORTPATH%\*.ftr) do @rd /s /q "%%x"

) ELSE (

echo %JOBNO%.ftr missing.
echo Please try and re-export the Prostix job file from PrydaBuild.
GOTO PROMPT

)
endlocal
PAUSE
:END

Could the FOR command be used? If so how would that be structured?
Thanks in advance.
Fabian RAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NVITEnd-user supportCommented:
Please clarify...

The contents of the files are as follows;

TW16   930.50
3C1H    1522
4C2       22
R10090035                       154  0.600    1.7
R10090045                        26  2.400    2.3
R10140035                         1  0.300    4.2
R10140035                         1  0.900    4.2

Then, your findstr searches for R10140035 as in:
findstr /v /c:"R10140035"

Open in new window


Example file: jobno1.ftr
What text does an example like jobno1.ftr contain?
Do you want  to extract all the text lines from jobno1.ftr that DOES NOT contain any text in the above list?
1
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Do you want to process all .ftr files and copy them, or only those containing the search string?
0
Bill PrewIT / Software Engineering ConsultantCommented:
If you are looking to process all the files in that export folder, like you are currently, then try something like this:

@echo off
setlocal

set "EXPORTPATH=C:\export"
set "DESTPATH=Y:\"

for %%A in ("%EXPORTPATH%\*.ftr") do (
    echo Exporting "%%~A"
    findstr /v /c:"R10140035" "%%~A" >"%DESTPATH%%%~nxA"
    del /q "%%~A"
)

for /d %%x in ("%EXPORTPATH%\*.ftr") do rd /s /q "%%~x"

endlocal
pause

Open in new window


»bp
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
i really wonder what for /d %%x in (%EXPORTPATH%\*.ftr) do @rd /s /q "%%x" should be for ... This will only act on folders having an extension of .ftr, which doesn't look correct or useful.
1
Fabian RAuthor Commented:
Thank you very much for the help with this. Exactly what I was looking for.
I was almost there but had a problem with the output path\file.

Where can I learn more about how to extract certain information like just the file name from the "%%A" variable like you have with "%%~nxA"?

Towards the end that is about where I was becoming stuck, did not know how or even if that could be done.
0
Bill PrewIT / Software Engineering ConsultantCommented:
You can do FOR /? at a command prompt to get info on the loop variables.  Toward the bottom you will see the following.  If you want more than this let me know...

In addition, substitution of FOR variable references has been enhanced.
You can now use the following optional syntax:

    %~I         - expands %I removing any surrounding quotes (")
    %~fI        - expands %I to a fully qualified path name
    %~dI        - expands %I to a drive letter only
    %~pI        - expands %I to a path only
    %~nI        - expands %I to a file name only
    %~xI        - expands %I to a file extension only
    %~sI        - expanded path contains short names only
    %~aI        - expands %I to file attributes of file
    %~tI        - expands %I to date/time of file
    %~zI        - expands %I to size of file
    %~$PATH:I   - searches the directories listed in the PATH
                   environment variable and expands %I to the
                   fully qualified name of the first one found.
                   If the environment variable name is not
                   defined or the file is not found by the
                   search, then this modifier expands to the
                   empty string

The modifiers can be combined to get compound results:

    %~dpI       - expands %I to a drive letter and path only
    %~nxI       - expands %I to a file name and extension only
    %~fsI       - expands %I to a full path name with short names only
    %~dp$PATH:I - searches the directories listed in the PATH
                   environment variable for %I and expands to the
                   drive letter and path of the first one found.
    %~ftzaI     - expands %I to a DIR like output line

Open in new window


»bp
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.