Delete all files except filenames with specific string - batch-file

I have 1000's of files like TOP_QUERIES-abc.com.au.csv,TOP_QUERIES-www.abc.com.au.csv, TOP_QUERIES-m.lmn.com.au.csv, TOP_QUERIES-blog.com.au.csvand get-files.php
Is it possible to delete all the .csv files from a folder except for files starting with blog. ,m. , www, and .php from the folder?
I know its possible in php but how can I achieve in batch file?

#ECHO OFF
SETLOCAL
SET "targetdir=U:\destdir"
SET "exclude=blog. m. www."
FOR /f "delims=" %%a IN (
'dir /b /a-d "%targetdir%\*.csv" '
) DO (
ECHO %%a | findstr /B /L "%exclude%" >NUL
IF ERRORLEVEL 1 ECHO(DEL "%targetdir%\%%a"
)
GOTO :EOF
This should get the task done for you. You would need to change the setting of targetdir to suit your circumstances.
The required DEL commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(DEL to DEL to actually delete the files.
The .php files are excluded because they don't match the *.csv filename - or did you want to not-delete files starting .php? If so, simply add .php into the exclude variable...
Revision for efficiency:
#ECHO OFF
SETLOCAL
SET "targetdir=U:\destdir"
SET "exclude=blog. m. www."
FOR /f "delims=" %%a IN (
'dir /b /a-d "%targetdir%\*.csv" ^| findstr /B /L "%exclude%"'
) DO (ECHO(DEL "%targetdir%\%%a"
)
GOTO :EOF

Related

How can I move files based on their name?

I'm trying to move files into existing sub-folders based on the file names.
For example, I want to move a file named AP16742 found in the directory X:\Files into a folder named AP in the directory X:\Files\AP. Other files named MO14823 I want to move into a folder named MO in the directory X:\Files\MO.
I'm inexperienced in coding, so I need explanations to go with a provided example.
This is what I tried:
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
FOR /f "delims=" %%a IN ('dir /b /a-d "%sourcedir%*.xml" ') DO (
FOR /f "tokens=1delims=_-" %%b IN ("%%a") DO (
FOR /f "delims=" %%d IN ( 'dir /b /ad "%destdir%*%%b*" ' ) DO (
ECHO(MOVE "%%a" "%destdir%\%%d\"
)
)
)
GOTO :EOF
A simple explanation would have been:
you want to distribute files in X:\Files to subfolders with the
first 2 letters of the file name.
To get a substring you need to set the content from for variable into
a normal variable.
Setting and using a variable inside a (code block) requires delayed
expansion.
#Echo off&SetLocal EnableExtensions EnableDelayedExpansion
Pushd "X:\Files"
For %%A in (*) do (
Set "File=%%~nA"
if not exist "!File:~0,2!" md "!File:~0,2!" 2>&1>Nul
Move "%%A" "!File:~0,2!"
)
Popd
In case you want to move only distinct 2 letter pairs and not all files that's also possible without great effort.
A modification of LotPings answer, this uses RoboCopy with its /MOV option, which will create directories as needed and move the files to them:
#Echo Off & SetLocal EnableDelayedExpansion
CD /D "X:\Files" 2>Nul || Exit /B
For %%A In (*) Do (Set "File=%%~nA"
RoboCopy . "!File:~,2!" "%%A" /MOV >Nul)

Batch - Returning full path from a dir /b

I am traversing folders on a drive, collecting file names with specific extensions, and building a string which is later used in a command line switch. When I find a qualifying file I need to know its full path as this is what is required by the command line. I currently use "%~dp0%%a\%%b" to build the full path, but I can see that may have limitations later on when the batch becomes more complex (e.g. it digs deeper into sub folders). I am hoping there is a way to replace "%~dp0%%a\%%b" with the path to the located file. Thank you:
#ECHO OFF
for /f "usebackq tokens=*" %%a in (`dir /b /a:d`) do (
pushd %%a
setlocal ENABLEDELAYEDEXPANSION
for /f "delims=" %%b in ('dir /b "*.E01" "*.L01" "*.AD1" 2^>nul') do (
SET EVIDENCE_STR=!EVIDENCE_STR! /e:"%~dp0%%a\%%b"
)
IF DEFINED EVIDENCE_STR (
ECHO !EVIDENCE_STR!
) ELSE (
ECHO No evidence files located in: %%a
)
endlocal
SET EVIDENCE_STR=
popd
)
PAUSE
Why do you need to create 2 loops, each running a dir command to find files? Why not just do for /R loop? Here is an example:
#echo off
set "files=*.E01 *.L01 *.AD"
for /R %%a in (%files%) do echo %%a
Simply use "Sub"-Option: /S of the DIR-Command:
Dir /B /S C:\*.jpg
cause it is that equivalent to:
#echo off
set "DirPath=C:\"
set "files=*.jpg"
for /R %DirPath% %%a in (%files%) do echo %%a
and so you should got the same result in each script.
The Problem: If you don't want any deep of SubDirectorys, you've to filter out these and so you may lose time - in both solutions.

Finding a latest folder that contains a specific file

I need to search a directory with multiple folders and check for the latest file(.exe) and copy that to another location.
SET "src_root"
SET "tgt_path"
DIR "%src_root%" /B /AD /O-D /TC > "%TEMP%\dirlist.tmp"
< "%TEMP%\dirlist.tmp" SET /P last_dir=
XCOPY "%src_root%\%last_dir%\*.exe" "%tgt_path%"**
This code helps me copy the EXE file in the latest folder, but in case, there is no EXE in the latest folder, I need to copy it from the folder which contains the latest EXE, can anyone help me out?
#ECHO OFF
SETLOCAL
SET "sourcedir=c:\sourcedir"
SET "filename="
FOR /f "delims=" %%a IN (
'dir /s /b /a-d "%sourcedir%\neo.7z" '
) DO SET "filename=%%a"&set "dirname=%%~dpa"&goto found1
ECHO NOT found!
GOTO :eof
:found1
FOR /f "delims=" %%a IN (
'dir /s /b /a-d "%sourcedir%\neo.7z" '
) DO IF /i "%dirname%" neq "%%~dpa" FOR /f %%s IN ('XCOPY /y /L /D "%filename%" "%%~dpa"') DO IF "%%s"=="0" SET "filename=%%a"&set "dirname=%%~dpa"&goto found1
ECHO latest file is "%filename%"
GOTO :EOF
You would need to change the setting of sourcedir to suit your circumstances.
I used files named neo.7z for my testing - I believe that you should have a fixed filename in the situation you describe - the directories are no doubt not crammed with *.exe files.
It's very pedestrian, but will do the job.
Essentially, find any file with the required name in the required directory-tree and record the name in filename, directory in dirname.
Using that filename as a base, try xcopying it over every other matching filename. Use the /L flag to list-only, and the /y flag to remove user-intervention. If the return is 1 file(s) copied ,f which only the first token is selected into %%s, then the chosen file is later. 0 file(s) copied sets %%s to 0 and means that the chosen file is earlier, so select the newer file and directorynames and restart.
I'll leave it a a user-exercise to speed it up. (suggestions if interested : delayedexpansion, subroutine and retain-new-startpoint)
Revision [about 5 times faster]
#ECHO OFF
SETLOCAL
SET "sourcedir=c:\sourcedir"
SET "filename="
FOR /f "delims=" %%a IN (
'dir /s /b /a-d "%sourcedir%\neo.7z" '
) DO SET "candidate=%%a"&CALL :latest
ECHO latest file is "%filename%"
GOTO :eof
:latest
IF NOT DEFINED filename GOTO selectnew
FOR /f %%s IN ('XCOPY /y /L /D "%filename%" "%candidate%"') DO IF "%%s"=="1" goto :eof
:selectnew
SET "filename=%candidate%"
GOTO :EOF
This revision avoids the repetitive directory-scan. For each matching filename, compare the newly-found file against the previously-found file using the xcopy /L method, and select the new candidate if the new one is later. continue untill all matching names have been tested.

Partial path known..need to search for file type inside

There is a particular folder that begins with a name such as SS followed by random characters. The names would be different every time and the only thing we are sure of is the folder begins with SS. How do we look if this folder has a .txt file inside in batch programming.
An idea :
#echo off
for /f "delims=" %%a in ('dir /b/ad ^|find /i "SS"') do set $Dir=%%a
dir /b/a-d *.txt %$dir%>nul
if %errorlevel% equ 0 echo File(s) found in "%$DIR%"
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
FOR /f "delims=" %%a IN (
'dir /b /ad "%sourcedir%\ss*" 2^>nul'
) DO (
FOR /f "delims=" %%h IN (
'dir /b /a-d "%sourcedir%\%%a\*.txt" 2^>nul'
) DO (
ECHO "%sourcedir%\%%a\%%h"
)
)
GOTO :EOF
should solve your problem - you need to change sourcedir to suit your system, obviously.
The code below check if the folder contain any .txt file:
#echo off
set "filePath="
for /D %%a in (SS*) do if exist "%%a\*.txt" do set "filePath=%%a"
if defined filePath echo File exists in folder %filePath%
If you want to check for a particular .txt file, just change *.txt by the appropriate name.

create folders based on string in file name

I need a batch file to create a process for a list of files in a directory.
The filename structure is, for example: 00000_AAA_132144_2012021.txt
I need the batch to:
1 - Create a folder name based on the numbers after the second underscore, as this is the only constant in the naming.
2 - Move the file into the new folder.
In the example of the above the batch would create a folder called 132144 and then move the file 00000_AAA_132144_2012021.txt into the folder
For a similar requirement I used the script Endoro created for me (below). Is it possible to modify this to meet my requirement?
#echo off &setlocal
for /f "delims=" %%i in ('dir /b /a-d *.PDF') do (
set "filename1=%%~i"
setlocal enabledelayedexpansion
set "folder1=!filename1:~11,6!"
mkdir "!folder1!" 2>nul
move "!filename1!" "!folder1!"
endlocal
)
If you know that the filenames will be the same length, you can do the following to get the numbers after the second underscore -
set filename=00000_AAA_132144_2012021.txt
set dirname=%filename:~10,6%
If the spacing may vary - you can do the following -
for /f "delims=_ tokens=3" %%a in ('echo %filename%') do set dirname=%%a
And yes, the script written for you seems to do essentially the same thing as what you're asking - I've edited it to do what you've asked -
#echo off
setlocal enabledelayedexpansion
for /f %%i in ('dir /b /a-d *.txt') do (
set "filename=%%~i"
for /f "delims=_ tokens=3" %%a in ('echo !filename!') do set folder=%%a
mkdir "!folder!" 2>nul
move "!filename!" "!folder!"
)
This will move all *.txt documents to a folder created based on the third section of the text files name. Note that this will cause problems if you have .txt documents in the directory that do not follow the same naming standard.
#ECHO OFF
SETLOCAL
SET "sourcedir=c:\sourcedir"
SET "destdir=c:\destdir"
FOR /f "delims=" %%a IN ('dir /b /a-d "%sourcedir%\*_*_*_*.txt" ') DO (
FOR /f "tokens=3delims=_" %%m IN ("%%a") DO (
ECHO MD "%destdir%\%%m"
ECHO MOVE "%sourcedir%\%%a" "%destdir%\%%m\"
)
)
GOTO :EOF
Endoro's routine selects .pdf files, you've specified .txt
Find filenames matching the mask, find the third _-separated token in the name, make that directory and then move the file.
The required commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO MD to MD to actually create the directories and change ECHO MOVE to MOVE to actually move the files.
Append 2>nul to suppress error messages (eg. when the directory already exists)
Append >nul to suppress report messages (eg. 1 file moved)

Resources