I would like to delete a file name from a txt list ussing a drag and drop function however i cant get my code to work
:delete
setlocal enableDelayedExpansion
set /p dnr=%1
find /v "!dnr!" document.txt > deleted.txt
pause
This will remove the file you drag and drop onto it, from the text file (presuming name and extension format). If it's full path format then change the %%~nxa to %%a.
:delete
setlocal enabledelayedexpansion
for /f "tokens=*" %%a in ("%1") do (
set dnr=%%~nxa
find /v "!dnr!" filenames.txt >deleted.txt
)
for /f "skip=2 tokens=* delims=" %%x in (deleted.txt) do echo %%x >>new.txt
del deleted.txt /f /q
ren new.txt deleted.txt
Related
I'm trying to search and find Emails containing for example #gmail.com and stock them to a new file "OUTPUT_FILE", and also delete the email from the "INPUT_FILE", I was able to do the first part (finding the email and stoking it to a new file).
But I couldn't delete it from the old one 'INPUT_FILE'
#echo off
setlocal enableextensions disabledelayedexpansion EnableDelayedExpansion
set INPUT_FILE=results.txt
set OUTPUT_FILE=emails.txt
set OUTPUT_FILE2=emails2.txt
set result=result.txt
set "REGEXP=[\.A-Z\-_][\.A-Z\-_]*#gmail.com"
>nul copy nul %OUTPUT_FILE%
for /f "tokens=*" %%a in (%INPUT_FILE%) do for %%b in (%%a) do (
for /f %%z in ('echo %%b ^| findstr /R /I "%REGEXP%"') do (
echo %%z >> %OUTPUT_FILE%
)
)
for example INPUT_FILE
nour.elhouda.elarabi#gmail.com
houda#hotmail.com
nour#gmail.com
smile#outlook.com
sau#gmail.com
after runung the script i should have
INPUT_FILE
houda#hotmail.com
smile#outlook.com
OUTPUT_FILE
nalte.el#gmail.com
nour#gmail.com
sau#gmail.com
#echo off
echo File Extionsion To Create List?
SET /p Ext=Ext:
echo %Ext%
for /f "tokens=1 delims=." %%g in ('dir /b *.%Ext%') do echo %%g >> Names.txt
How do you remove the last dot and the file extension without accidentally removing other dots. For example 10.01320.pdf will become 10.01320.
Since your goal is to keep all the .'s in the file name just not the extension, you can simply use some Parameter Extensions. In the example of your goal, %%~ng will Expand %%g to a file Name without file extension or path.
#echo off
echo File Extionsion To Create List?
SET /p Ext=Ext:
echo %Ext%
for /f "tokens=1 delims=*" %%g in ('dir /b *.%Ext%') do echo %%~ng >> Names.txt
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.
I downloaded about 34000 books in .txt format from Project Gutenberg. Now I want to rename all of them by its content. For example every text file includes its "Title" and "Author's Name" so I want to rename all the text files on its "Title" and "Author's Name" by some commands.
I created a batch file. It runs but is not renaming the files. This is my code:
#echo off&setlocal
cd E:\Test
for /f "delims=" %%i in ('dir /a-d/b *.txt') do (
set "nname="
set "fname=%%~i"
for /f "usebackqskip=7delims=" %%f in ("%%~i") do if not defined nname
set "nname=%%f"
setlocal enabledelayedexpansion
set "nname=!nname:~0,40!"
echo rename "!fname!" "!nname!"
endlocal
)
You can use this as a base
#echo off
setlocal enableextensions disabledelayedexpansion
rem Change to source folder
pushd "e:\test" && (
rem Where the renamed files will be placed to avoid re-rename
if not exist renamed\ md renamed
rem For each input file
for %%f in (*.txt) do (
rem Retrieve the data from inside the file
set "author=" & set "title="
for /f "tokens=1,* delims=: " %%a in ('
findstr /b "Author: Title:" "%%~ff"
') do if not defined %%a set "%%a=%%b"
rem If the fields have been retrieved then do the rename
if defined author if defined title (
setlocal enabledelayedexpansion
for /f "delims=" %%a in ("!author! - !title!") do (
endlocal
echo move "%%~ff" "renamed\%%a%%~xf"
rem NOTE: operation is only echoed to console
rem if console output seems correct, then
rem remove the echo command
)
)
)
rem Done. Return to previous active directory
popd
)
Of course, filesystem has rules about what is allowed in a file name and, not knowing what kind of characters can be found, this code could and probably will fail to rename some files.
Your current script will just print the rename commands, not execute them. You should remove echo (after checking what it produces) in this line:
echo rename "!fname!" "!nname!"
Your script also has a few formatting issues. There should be spaces like this:
for /f "usebackq skip=7 delims=" %%f in ("%%~i") do
And there should be no newline just after:
if not defined nname
I want to write a batch script to rename folders in a directory.
The way that would work is, I would have a file that contains names that I would like each folder to be renamed with. So basically the batch script would just pick names from the file (that contains names) and use it to rename each folder.
So if I have 20 folders, 20 names would exist in file to rename each folder.
What I have so far:
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET old=*.txt
SET new="c:\Users\user\Desktop\testing.txt"
< %new% (for /f "tokens=*" %%f in ('dir /b %old%') do (
ren Read the next name from the redirected input file
SET /P newname=
ren "%%f" "!newname!"
))
The above script didn't give me the desired result.
Not tested:
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET old=*.txt
SET new="c:\Users\user\Desktop\testing.txt"
set counter=0
(for /f "tokens=*" %%f in ('dir /b %old%') do (
ren Read the next name from the redirected input file
set /a counter=counter+1
for /f "tokens=1* delims=:" %%a in ('findstr /R /N "^" "%new%"^|find "!counter!"') do set "newname=%%b"
ren "%%f" "!newname!"
)
The problem is that the dir /b %old% command generate a list of files with .txt extension. If you want to rename folders, then include /AD switch and eliminate the wild-card:
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET new="c:\Users\user\Desktop\testing.txt"
< %new% (for /f "tokens=*" %%f in ('dir /b /AD') do (
ren Read the next name from the redirected input file
SET /P newname=
ren "%%f" "!newname!"
))