Link to home
Start Free TrialLog in
Avatar of Fabian R
Fabian R

asked on

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.
Avatar of NVIT
NVIT
Flag of United States of America image

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?
Do you want to process all .ftr files and copy them, or only those containing the search string?
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Fabian R
Fabian R

ASKER

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.
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