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?
 
Bill PrewCommented:
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
 
NVITCommented:
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
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Do you want to process all .ftr files and copy them, or only those containing the search string?
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
QlemoBatchelor, 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 PrewCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.