Windows Batch How to delete specific words from txt file - batch-file

I can't figure out how to remove a piece of text in a line.
I have a batch file that creates a list of files of interest to me in the selected folder.
dir D:\pool\template_test>U:\Desktop\list.txt
findstr "Work_T" U:\Desktop\list.txt > U:\Desktop\tamplates.txt
Output:
2013-03-13 17:24 622 Work_T_tamplate1.fdf
In final file, as you can see, presents the date, time, size, but I only need the name. How can I do this?
I tried to use the example from the other post, but it does not work:
for /f "tokens=3" %%A in (U:\Desktop\tamplates.txt) do findstr /C".dot" /C".pdf" /C".fdf" %%A
final file after using proposed code

So you already got an answer in comments by Stephan
These will include the search for you, the following only returns the filename:
cd /d "D:\pool\template"
(#for /f %%i in ('dir /b "Work_T*.dot" "Work_T*.pdf" "Work_T*.fdf" ') do (
#echo "%%i"
)
)>"U:\Desktop\tamplates.txt"
You could however also want the full path, so this would include the full path of the file:
cd /d "D:\pool\template"
(#for /f %%i in ('dir /b "Work_T*.dot" "Work_T*.pdf" "Work_T*.fdf" ') do (
#echo "%%~fi"
)
)>"U:\Desktop\tamplates.txt"
add /s to recurse through subfolders if needed.
Edit
for UNC paths:
(#for /f %i in ('dir /b /a-d "\\tuesrna02\pool\template" ^| findstr /i "Work_T"') do (
#echo %%i
)
)>"U:\Desktop\tamplates.txt"

Related

batch with a lot of for loops

So i made this batch to psarc extract all *.pak files, which will always give *.mbin files that often have the same name. it's then supposed to look for all extracted subfolders and then look for the last created *.mbin file from all of the subfolders and rename it to its current name plus a number. but i get very weird results.
setlocal ENABLEDELAYEDEXPANSION
set number=0
cd /d "C:\Users\storm\Desktop\No Man's Sky\GAMEDATA\PCBANKS\MODS"
for /r %%f in (*.pak=) do (
psarcMBIN.exe extract %%~nxf
set /A number=!number!+1
FOR /F "tokens=" %%a in ('dir /a:d /s') do (
cd /d "%%a"
FOR /F "tokens=" %%G in ('dir /b /a:-d /o:d "*.mbin"') do set newest=%%G
rename "!newest!" "!newest!!number!.mbin"
)
)
pause
The result basically is just:
1. psarc extracted
2. psarc tried to extract next file but cannot overwrite
The other parts of the main for loop are not even shown

Rename file with different text position in batch

I am trying to rename text files with a different text position.
Example :
20170811191008_marie.txt --> marie_txt_20170811191008
I have a very basic rename batch command to rename files in loop but I am trying to find out how I can change the position of the text within the filename.
Command:
for /f "delims=" %%i in ('dir /b /a-d *.txt') do ren "%%~i" "%%~ni txt%%~xi"
Per your wish
for /f "tokens=1* delims=_" %%i in (
'dir /b /a-d *_*.txt ^|findstr /i "^20[0-9]*_.*\.txt$"'
) do echo ren "%%i_%%j" "%%~nj_txt_%%i"
> SO_45673483.cmd
ren "20170811191008_marie.txt" "marie_txt_20170811191008"
EDIT forgot to mention to remove the echo in front of ren to really execute the rename once you are shure it does want you want.
For multiple file extensions, you could always use something like this:
#Echo Off
SetLocal EnableDelayedExpansion
For /F "EOL=_ Tokens=1* Delims=_" %%A In ('Where .:??????????????_*.*') Do (
Set "fx=%%~xB"
Ren "%%A_%%B" "%%~nB_!fx:~1!_%%A" 2>Nul)
You can of course still specify .txt instead of .* but there would be no need to hard code _txt_ into the code following it.

Batch script to delete files in folder that does not contain certain word

I am new to batch scripting . I need to delete all files in a folder that DOES NOT contains some word in the file
found this code
#echo off
setlocal
pushd C:\Users\admin\Desktop\bat
findstr /ip /c:"importantWord" *.txt > results.txt
popd
endlocal
So how i can WHITE list this files, and delete all other?
Or i think there is easy way with just check if !contains and delete
but i don`t know how?
Supposedly, this problem could be solved in a very simple way combining these findstr switches: /V that show results when the search string is not found, and /M that show just the name of the files; that is:
#echo off
setlocal
cd C:\Users\admin\Desktop\bat
for /F "delims=" %%a in ('findstr /ipvm /c:"importantWord" *.txt') do del "%%a"
Unfortunately, the combination of /V and /M switches don't properly work: the result of /V is based on lines (not files), so a modification in the method is needed:
#echo off
setlocal
cd C:\Users\admin\Desktop\bat
rem Create an array with all files
for %%a in (*.txt) do set "file[%%a]=1"
rem Remove files to preserve from the array
for /F "delims=" %%a in ('findstr /ipm /c:"importantWord" *.txt') do set "file[%%a]="
rem Delete remaining files
for /F "tokens=2 delims=[]" %%a in ('set file[') do del "%%a"
This method is efficient, particularly with big files, because findstr command report just the name of the files and stop searching after the first string match.
#echo off
setlocal
set "targetdir=C:\Users\admin\Desktop\bat"
pushd %targetdir%
for /f "delims=" %%a in ('dir /b /a-d *.txt') do (
findstr /i /p /v /c:"importantWord" "%%a" >nul
if not errorlevel 1 echo del "%%a"
)
popd
endlocal
Not really sure what you want to do with /pfiles - files containing non-ansi characters appear to return errorlevel 1for these. if not errorlevel 1 will echo the files that do not contain the required string - remove the echo to actually delete the file(s)
This should work:
#ECHO OFF
SETLOCAL EnableDelayedExpansion
SET "pathToFolder=C:\FolderToEmpty"
SET "wordToSearch=ImportantWord"
FOR /F "tokens=*" %%F IN ('dir %pathToFolder% /b *.txt') DO (
findstr /IP %wordToSearch% "%pathToFolder%\%%F">nul
IF !ERRORLEVEL!==1 (
DEL /Q "%pathToFolder%\%%F"
)
)
You will have to set the proper path to the folder you want to delete the files from and to replace ImportantWord with the substring you are looking for.

How to remove all files having 10 characters in name with bat?

I have thousands of generated php files in a folder that are not necessary. They all having the same naming structure of 10 characters each example "rk9qiaLOaf.php".
But I don't want to delete all because the folder contains index.php,main.php such important files and I don't want them to be deleted.
Please, how can it be done to delete the files within a folder they all are having name count of 10 by a batch script. Thank you for reading my problem.
You could just use this from the Command prompt:
For /F "Delims=" %A In ('Where/F "C:\Users\Aung\Documents:??????????.php"') Do #Del %A
Just change the folder path as appropriate.
Edit
A recursive version, (could take a while).
For /F "Delims=" %A In ('Where/F /R "C:\Users\Aung" "??????????.php"') Do #Del %A
Once again just change the root folder, (use . for the current directory) as necessary.
You could use dir together with findstr to filter for the correct files:
dir /B /A:-D "*.php" | findstr /I "^..........\.php$"
Every dot . matches a single character. ^ and $ ensure to match the whole file name.
To delete the returned files, use this in command prompt cmd:
for /F "delims=" %F in ('dir /B /A:-D "*.php" ^| findstr /I "^..........\.php$"') do #del "%F"
Or this in a batch file:
for /F "delims=" %%F in ('dir /B /A:-D "*.php" ^| findstr /I "^..........\.php$"') do del "%%F"
And here is an alternative batch file approach relying on sub-string expansion:
#echo off
setlocal DisableDelayedExpansion
for /F "delims=" %%F in ('dir /B /A:-D "*.php"') do (
set "NAME=%%~nF" & set "EXT=%%~xF"
setlocal EnableDelayedExpansion
rem // Check whether file name is not longer than 10 characters:
if "!NAME!"=="!NAME:~,10!" (
rem // Check whether file name is longer than 9 characters:
if not "!NAME:~9!"=="" (
del "!NAME!!EXT!"
)
)
endlocal
)
endlocal

Looping through directories to extract pages from multiple pdfs

I have multiple directories that contain one pdf in each. Im trying to extract page 1 from each pdf and keep it in the directory that the original pdf was in so i end up with the multiple directories now containing two pdfs.
I currently have this code using ghost scripts which works, but only if the pdfs are in the same directory as the batch file
echo on
for %%I in ("D:\files\input\" *.pdf) do "C:\Program Files\gs\gs9.20\bin\gswin64c.exe" -dSAFER -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile="%%~nI_p1.pdf" -dFirstPage=1 -dLastPage=1 "%%I"
I cant see why all the directories in D:\files\input\ are not being looped through.
You are executing the command with two parameters,"D:\files\input\" and *.pdf.
You need to look for each *.pdf file. The accepted way is
for /f "delims=" %%I in ('dir /b /s /a-d "D:\files\input\*.pdf" ') do ...
directory, basic format, with subdirectories, but no directorynames.
However, this would execute the command against every *.pdf including the *_p1.pdfs (ie those already processed) so
for /f "delims=" %%I in ('dir /b /s /a-d "D:\files\input\*.pdf"^|findstr /v /i /e /L "_p1.pdf" ') do ...
would exclude those files by find those names that do not (/v) end (/e) with the literal (/L) "_p1.pdf" without regard to case (/i). It would however regenerate the _p1s.
for /f "delims=" %%I in ('dir /b /s /a-d "D:\files\input\*.pdf" ') do if not exist "%%~dp*_p1.pdf" ...
would process those *.pdfs that have not already been processed.
(not tested)
Your usage of the for loop is the problem. See for /?
Magoo was faster, see this complete solution.
#Echo off
Set "GS=C:\Program Files\gs\gs9.20\bin\gswin64c.exe"
Set "Opt=-dSAFER -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dFirstPage=1 -dLastPage=1"
Pushd "D:\files\input\"
for /f "delims=" %%I in (
'Dir /B/S *.pdf ^|find /v "_p1.pdf" '
) do If not exist "%%~dpnI_p1.pdf" "%GS%" %Opt% -sOutputFile="%%~dpnI_p1.pdf" "%%~fI"
Popd

Resources