1.

Solve : Batch file: find and delete?

Answer»

Hi, I need to WRITE a batch FILE that READS a txt file that contains a number of file PATHS, then it should remove the chosen string from each line and copy into a new file e.g.

List.txt contains:-
C:\Projects\file1.txt
C:\Projects\file2.c
etc...

New_list.txt contains:-
file1.txt
file2.c
etc...

I apologize if this question has been answered before, I've tried looking at some code for find/replace but I could not follow it.
Any help will be greatly appreciated.
Thank you
If you just want to isolate the filename and extension from a string which represents the full path you can use FOR with the ~n and ~x modifiers.

Code: [Select]If exist New_list.txt del New_list.txt
for /f "delims=" %%A in (list.txt) do @echo %%~nxA >>New_list.txtThank you for the response, I need to keep certain subfolders ALONG with the filename and extension. I also need to specify the string to remove but it will be the same string throughout the file e.g.

C:\Users\Projects\Monday\file1.txt -->Monday\file1.txt
C:\Users\Projects\Monday\Last\New\file2.c --> Monday\Last\New\file2.c

or

H:\Projects\Monday\file1.txt -->Monday\file1.txt


You can read in each file path and name in turn using FOR and then use the replace in string function to remove the unwanted part of the string.

Code: [Select]@echo off
setlocal enabledelayedexpansion
set RemoveString=C:\Users\Projects\

echo %RemoveString%Cat.txt > list.txt
echo %RemoveString%Dog.txt >> list.txt
echo %RemoveString%Hat.txt >> list.txt
echo %RemoveString%Cow.txt >> list.txt

echo String to remove: %RemoveString%
echo.

echo Input file:
type list.txt
echo.

If exist New_list.txt del New_list.txt
for /f "delims=" %%A in (list.txt) do (
set inputline=%%A
set outputline=!inputline:%RemoveString%=!
echo !outputline! >> New_list.txt
)

echo Output file:
type New_list.txt
echo.
Output

Code: [Select]String to remove: C:\Users\Projects\

Input file:
C:\Users\Projects\Cat.txt
C:\Users\Projects\Dog.txt
C:\Users\Projects\Hat.txt
C:\Users\Projects\Cow.txt

Output file:
Cat.txt
Dog.txt
Hat.txt
Cow.txtThat's Perfect, Thank you



Discussion

No Comment Found