Solved Batch script to edit file contents then copy to new folder.

February 13, 2018 at 13:17:00
Specs: Windows 7
Hi All
Looking for some assistance. Very new to this and wondering where I am going wrong.
I am looking at editing out specific lines of text from a file then copying it to a new folder.

The content of the file is as follows;

TW16 930.50
3C1H 1522
4C2 22
R10090045 331 0.600 2.3
R10090045 83 4.800 2.3
R10090035 154 0.600 1.7
R10140035 1 0.300 4.2
R10140035 1 0.900 4.2
R10140035 1 2.100 4.2


I am using findstr to remove the lines containing R10140035 which works fine on its own but I also want to move/copy the file to a new folder.
I can do this all separately which is fine but unable to do it all at once efficiently. I have been playing around with the FOR command but don't believe I am getting the syntax correct.
Tried separators and have not had much luck. The closest I have come is a new blank file in the copy to directory.

The basic script I have gone back to is the following;


@echo off
setlocal
SET "EXPORTPATH=C:\export"
SET "DESTPATH=C:\export\output\"
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 >%JOBNO%.ftr
copy /Y "%EXPORTPATH%\%JOBNO%.ftr" "%DESTPATH%%JOBNO%.ftr" > nul
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 file.
GOTO PROMPT

)
endlocal
PAUSE
:END


Any suggestions welcome!


See More: Batch script to edit file contents then copy to new folder.

Report •

✔ Best Answer
February 15, 2018 at 21:41:50
for %%a in (%exportpath%\*.ftr) do (
findstr /v /c:"R10140035" %%a >"%DESTPATH%\%%a"
)

not sure, not tried. be sure to backup %destpath% since the output IS destructive as written.

message edited by nbrane



#1
February 13, 2018 at 20:31:30
I think I'm missing something, but here's this:
change:
findstr /v /c:"R10140035" %EXPORTPATH%\%JOBNO%.ftr >%JOBNO%.ftr
copy /Y "%EXPORTPATH%\%JOBNO%.ftr" "%DESTPATH%%JOBNO%.ftr" > nul

to:
findstr /v /c:"R10140035" %EXPORTPATH%\%JOBNO%.ftr >"%DESTPATH%%JOBNO%.ftr"

Still not sure if you want to MOVE or COPY. For 'move' just add a delete.


Report •

#2
February 14, 2018 at 12:26:33
Thanks, that's great!
For some reason it never really dawned on me that copy wasn't required.

Report •

#3
February 15, 2018 at 14:36:13
Would there be a way to search and remove the string in all files in the specified folder using a FOR command?
I'm a little confused with setting the required parameters.

Report •

Related Solutions

#4
February 15, 2018 at 21:41:50
✔ Best Answer
for %%a in (%exportpath%\*.ftr) do (
findstr /v /c:"R10140035" %%a >"%DESTPATH%\%%a"
)

not sure, not tried. be sure to backup %destpath% since the output IS destructive as written.

message edited by nbrane


Report •

#5
February 24, 2018 at 16:19:43
Thank you that's great!
Set me on the right path.

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


Report •

Ask Question