Finding a string within 2 files and then moving the files. Batch - windows

I recently asked a question about finding files with the same ID and then moving them.
Although the files I am moving do not have the same ID. Instead they have the same string within the files.
Here is the code I have for finding the same ID of files
ECHO OFF
setlocal enableDelayedExpansion
set "source=C:SORCEFOLER"
set "destBIAK=C:BIAKFOLER"
set "destBIPO=C:BIPOFOLDER"
set "POfile=BIPO"
SET "AKfile=BIAK"
pushd "%source%"
for %%F in ("%POfile%*") do (
set "setID=%%F"
set "setID=!setID:%POfile%=!"
if exist "%AKfile%!setID!" robocopy /mov "%source%" "%destbiak%" "%%F"
if exist "%AKfile%!setID!" robocopy /mov "%source%" "%destbipo%" "%AKfile%!setID!"
)
popd
Like I said, I need to actually find a string that matches in the two different and then move them based on the the string.
Here is a example of the beginning of the first file
H|0003341369|20131123
I can not post the entire PO file because it contains customer names and addresses that are proprietary information of the person I am making the file for.
here is the entire AK file
0003341369|SO-02052|20131124|A|
That just a bunch of numbers. Notice that digits 3 through 12 of the PO file match digits 1 though 10 of the AK file.
There are multiple AK files and PO files in one folder.
I was thinking that the script would find the ID of the AK file, search all the PO files until it finds the one that matches and then moves them.
I have looked at several scripts that search for a string, they then output the file name with the string to a certain file. I cant figure out how to output this as a variable.
Thanks

This should take the first term in every AK.* file (the characters before the first | character such as 0003341369 ) and search for it in every PO.* file.
If it finds the term inside two || characters (such as |0003341369| ) then it will move those files to a folder - and after comparing all PO.* files it will move the AK.xxx file to the same folder.
#echo off
for %%a in (AK.*) do (
MD "folder %%~na"
for /f "delims=|" %%b in ('type "%%a" ') do (
for %%c in (PO.*) do findstr "|%%b|" "%%c" >nul && move "%%c" "folder %%~na" >nul
)
move "%%a" "folder %%~na"
)

Related

Batch File Loop

I need to migrate a bunch of files across several subfolders in a single directory. Each subfolder containing a maximum of 100 data files (0000-0099,0100-0199, etc.) and is named dir_001, dir_002 etc. respectfully.
For example I can successfully transfer all the files that begin with a prefix of "F_0" using the following:
for /r X:\<PATH1>\ %%F in (F_0*.txt) do copy %%F E:\<PATH2>\
This will grab all the files from all the subfolders covering all images from F_00001.txt to F_09999.txt (or whatever the upper limit is). But most of the time I only need a smaller subset such as from F_04395.txt through F04542.txt.
Here is what I have been trying that does not work
for /r X:\PATH1\ %%F in (F_0*.txt) do (
For %%A in (%%F) do (
Set Folder=%%~dpA
For /L %%i in (4395 1 4542)do if /I exist Folder\F_0%%i.txt copy %%F E:\PATH2\
)
)
This seems to break when it comes to converting the file path to a variable.
It also would need to work for a sequence from F_000001.txt through F_010001.txt or in other words from file number 1 through 1001 (with a different number of leading zeros for 0-9, 10-99,100-999,1000-9999, so it is always a 5 digit number plus the prefix of "F_0")
Any advice would be most appreciated!
Update:
Thanks for #Gerhard I've changed the code to what is below but still not working and there is an issue combining/concatenating the %%dpA variable with the rest of the filename. The result of the below code is a double slash between the filename and the path, or if I take out the hard coded clash then the code also fails.
for /r X:\PATH1\ %%F in (F_0*.txt) do (
For %%A in (%%F) do (
For /L %%i in (4395 1 4542)do if /I exist %%dpA\F_0%%i.txt copy %%F E:\PATH2\
)
)
Thanks again to #Gerhard you beat me to the optomization but still helped greatly. Here is my final code that also address the multiple subfolders, and consolidated into a single line!
setlocal EnableDelayedExpansion
for /L %%i in (43,1,45,) do ((SET "formattedValue=000000%%i" & SET PathSource2=X:\Path1\Dir_!formattedValue:~-3!) & (For /L %%i in (4395 ,1,4542) do (if exist "!PathSource2!\F_0%%i.jpg" copy "!PathSource2!\F_0%%i.txt" "E:\Path2\")))

Batch Create Several Folders Based on Multiple Filenames, and Move Multiple Related Files to The Created Folders

Every week, one of my co-workers has had to go through a folder with hundreds of demuxed video and audio files, rename each one individually for a specific city TV station and then sort them into folders based on the name of the city. I've created a .bat file to rename them all for him, and now I'd like to create a .bat file that creates new directories based on the filenames, and places the corresponding files into the new folders. I copied a few of the files to test with.
So the end result will be a "Houston" folder with all it's corresponding files, a "Compton" folder with it's files, a "Moline" folder, etc, etc... for every city, up to around 200 cities, and we're only getting more.
He's currently searching "Houston", cutting all the files that come up, creating a new folder manually, naming it "Houston" and pasting all the files into his new folder. FOR EVERY CITY. 200 TIMES. And it takes hours.
The files are ALWAYS named with this system: X### Random City, ST
With my little wee programming knowledge, I'm supposing that the script could detect all the characters after the first space, and before the comma, copy those characters (Random City), create a new folder, name it the copied characters (Random City) then move any files containing "Random City" in their filename into the newly created folder. The end result would be as such, just with a lot more folders.
Is there anyone more advanced than me who could explain the best way to to this?
I apologize in advance if I'm in the wrong place or not savvy enough. Cheers!
UPDATE: I messed around, learned about tokens and delimiters, variables etc. Here is what I have which works amazingly, except I'm not sure how to remove the comma at the end of the city name. I'm using space as the delimiter, which makes the text chunks the tokens if I understand correctly, including my comma, using tokens=2. Another problem that arises; Say there's a city with two text chunks (tokens) eg. San Fransisco, Baton Rouge. How could I grab both of them, using the comma as my stopping point? My code is below.
#echo off
setlocal enabledelayedexpansion
for %%A in (*.m2v *.mpa) do (
echo file found %%A
for /f "delims=" %%B in ("%%A") do set fname=%%~nB
for /f "delims=" %%C in ("%%A") do set fextn=%%~xC
for /f "tokens=2* delims= " %%D in ("!fname!") do set folname=%%D
echo folder name !folname!
if not exist "!folname!" (
echo Folder !folname! doesn't exist, creating
md "!folname!"
) else (
echo Folder !folname! exists
)
echo Moving file %%A to folder !folname!
move "%%A" "!folname!"
)
echo Finished
pause
UPDATE 2: I found a meh workaround to get rid of the comma, by adding it as a delimiter, but I'm still trying to wrap my head around the 2 word cities. My Baton Rouge and San Fransisco folders are being named respectively, "Baton" and "San". Here is my code so far, I'll update if I find a better way.
setlocal enabledelayedexpansion
for %%A in (*.m2v *.mpa) do (
echo file found %%A
for /f "delims=" %%B in ("%%A") do set fname=%%~nB
for /f "delims=" %%C in ("%%A") do set fextn=%%~xC
for /f "delims=," %%B in ("%%A") do set fname=%%~nB
for /f "tokens=2* delims= " %%D in ("!fname!") do set folname=%%D
echo folder name !folname!
if not exist "!folname!" (
echo Folder !folname! doesn't exist, creating
md "!folname!"
) else (
echo Folder !folname! exists
)
echo Moving file %%A to folder !folname!
move "%%A" "!folname!"
)
echo Finished
pause
UPDATE 3
Here is my code which worked. However, if the number of characters in your filename prefixes/suffixes changes, it will screw things up and you'll have to edit code.
#ECHO OFF
SETLOCAL enabledelayedexpansion
FOR %%A in (*.m2v *.mpa) do (
ECHO file found %%A
FOR /F "delims=" %%B in ("%%A") do set fname=%%~nB
SET folname=!fname:~5,-4!
ECHO folder name !folname!
if not exist "!folname!" (
ECHO Folder !folname! doesn't exist, creating
MD "!folname!"
) else (
ECHO Folder !folname! exists
)
ECHO Moving file %%A to folder !folname!
MOVE "%%A" "!folname!"
)
ECHO Finished
PAUSE
Using the SET folname=!fname:~5,-4!allows me to trim the M373 prefix, 5 characters in, and the , TX suffix, 4 characters in, removing the comma and salvaging the city name, regardless of how long it is, or how many words it is (eg. West Palm Beach, FL) . Antares mentioned this solution in his answer which worked like a charm.
BUT IT ALSO MADE ME THINK
If the number of characters in the prefix changes, which is likely, I'll either have to edit the batch file every time, or create a specific batch file for each circumstance. Not terrible, but not great either. So I went with Michael Heath's answer which works flawlessly. I'm not smart enough yet to know exactly why, but I'm gonna dissect it and find out. I have a lot of learning to do. Thanks, everyone!
#echo off
setlocal
rem A=Fullpath, B=Name before comma, C=B prefix, D=B without prefix.
for /f "delims=" %%A in ('dir /b *.m2v *.mpa') do (
for /f "delims=," %%B in ("%%~nA") do (
for /f "tokens=1,*" %%C in ("%%~B") do (
if not exist "%%~D\" (
echo Folder "%%~D" doesn't exist, creating
md "%%~D"
)
if exist "%%~D\" (
echo Moving file "%%~A" to folder "%%~D\"
move /y "%%~A" "%%~D\"
) else echo Folder "%%~D\" doesn't exist
)
)
)
echo Finished
pause
3 for loops to get the tokens needed.
1st will get the fullpath.
2nd to get the name before the comma.
3rd to get the name without the prefix.
In the nested for loops check if folder exists, create it if not. Then if folder exists, move file inside.
Here is my code which worked. However, if the number of characters in your filename prefixes/suffixes changes, it will screw things up and you'll have to edit code.
#ECHO OFF
SETLOCAL enabledelayedexpansion
FOR %%A in (*.m2v *.mpa) do (
ECHO file found %%A
FOR /F "delims=" %%B in ("%%A") do set fname=%%~nB
SET folname=!fname:~5,-4!
ECHO folder name !folname!
if not exist "!folname!" (
ECHO Folder !folname! doesn't exist, creating
MD "!folname!"
) else (
ECHO Folder !folname! exists
)
ECHO Moving file %%A to folder !folname!
MOVE "%%A" "!folname!"
)
ECHO Finished
PAUSE
Using the SET folname=!fname:~5,-4!allows me to trim the M373 prefix, 5 characters in, and the , TX suffix, 4 characters in, removing the comma and salvaging the city name, regardless of how long it is, or how many words it is (eg. West Palm Beach, FL) . Antares mentioned this solution in his answer which worked like a charm.
BUT IT ALSO MADE ME THINK
If the number of characters in the prefix changes, which is likely, I'll either have to edit the batch file every time, or create a specific batch file for each circumstance. Not terrible, but not great either. So I went with Michael Heath's answer which works flawlessly. I'm not smart enough yet to know exactly why, but I'm gonna dissect it and find out. I have a lot of learning to do. Thanks, everyone!
Before Image
After Image
I can give you some hints. If you have a more specific problem case, feel free to update your question again.
You can cut the last X characters of a String like this: %variablename:~0,-X%
If you know the variables with the city parts, e.g. %%D and %%E or something, you can concatenate them again like this md "%%D %%E". However, this works just for a fixed number of tokens, like the two here.
You can store this concatenations in an own variable, if you need the result outside of your for-loop. Use set myVariable=%%D %%E for example, and show it with %myVariable% or !myVariable! (when delayed expansion is needed), for example md "%myVariable%".
A nifty workaround: if there are only a small number of "special cities" to take into consideration, then you could just add some rename commands at the end of your script, like rename San "San Francisco", rename Baton "Baton Rouge", etc. Will not work well, if there are more "San" cities (e.g. "San Bernadino"), because this cannot be distinguished anymore. But in this case, the copying into separate folders would already fail as well.
In your script you make a check for an existing folder. I think you can omit that. md or mkdir either create that directory or do nothing if it exists. Well, they do print a message to the console, which can be ignored. If you do not want to see them, redirect the error message stream to nul like this md myFolder 2>nul. This will swallow any error messages, but it is unlikely that you get any other error message than that in your scenario.
You could simplify your approach like this: I reckon your file renaming works well. Your "copy" script could be just a list of commands which are stated explicitly (and also could be edited fairly quickly if new cities are to be considered).
Set the batch file up like this with entries for each city:
#echo off
mkdir "Moline"
copy "*Moline*.*" "Moline"
mkdir "San Francisco"
copy "*San Francisco*.*" "San Francisco"
...
echo done.
Side effect is, that the folders for each city will be created, not just those where files are copied into. May be it suits your needs anyhow.
Also, I would like to give you pointers to sources of help/documentation:
On the command line you can get extensive help by executing the commands used in your batch file and appending /?. For example set /? gives you a lot of useful things you can do with variables/String manipulations.
Try: for /?, if /? (regarding errorlevel for example), maybe call /?, goto /?, and others.
A very good source for all the command line commands is https://www.ss64.com. This site provides extensive help even for PowerShell and Linux Bash and others. Relevant to you in this case would be CMD, direct link: https://ss64.com/nt/
Edit/Update:
Check out symbol replacement on the set command to build something like
if "%myVariable:~0,1%" == "M" (set myvariable=%myvariable:~1%)
The first part "cuts" the first character and checks, if it is an M and if so, it keeps everything except the first character. With this you could make your filenames even out to process them inside your batch file.
You can also "remove" a letter or substring with %myVariable:, TX=% which would replace any ", TX" occurance with "nothing" for example.
Oh, this could also help to remove any spaces in the filename. Then you could extract the "SanFrancisco" without a spaces problem ;) The folder name would be without space though. This could be resolved with further rename commands at the end.
Here's an alternative method, just for the sake of variety:
#Echo Off
SetLocal DisableDelayedExpansion
Set "SourceDir=.\Batch Rename\BACKUP"
If Exist "%SourceDir%\" For /F "EOL=|Delims=" %%G In (
'%__AppDir__%where.exe "%SourceDir%":"*, ??.m??" 2^>NUL'
)Do (Set "FileBaseName=%%~nG"&SetLocal EnableDelayedExpansion
For /F "EOL=|Delims=," %%H In ("!FileBaseName:* =!")Do (EndLocal
%__AppDir__%Robocopy.exe "%%~dpG." "%%~dpG%%H" "%%~nxG" /Mov>NUL))
This method filters your files with the where command. It selects for moving, only file names which end with a comma, followed by a space, followed by two letters, followed by a three letter extension beginning with the character m. It moves the files, automatically creating the destination directories if they do not exist, using the robocopy command. It uses only two for loops, the second of which, isolates the string between the first space and the next comma.
I have made it so that the script can be located anywhere, not necessarily in the directory with the files. This location is set on line 3 of the script, If you wish to modify it, please ensure that your location remains between the = and the closing double-quote, ", and does not end with a trailing back-slash, \. It is currently set to a relative directory, (based upon that visible in your screen-shot), but you could obviously use an absolute path too, e.g. Set "SourceDir=C:\Users\UserName\Videos". If you wish to keep the script in the same directory as the files to be moved, change it to read Set "SourceDir=." and just double-click it to run.
When I had to develop a script for the task at hand I would probably implement a few safety features in order to not move wrong files. Your sample data show pairs of .m2v and .mpa files, but I would likely not consider that as granted. Also would I not rely on a fixed-length prefix. Finally, I would perhaps also account for lit's comment.
So here is my attempt (see all the explanatory rem-remarks in the code):
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_ROOT=%~dp0." & rem // (root directory containing the files to be processed)
set "_MASK=M??? *, ??.m2v" & rem // (mask to find the files to be processed)
set _EXTS=".m2v" ".mpa" & rem /* (list of extensions that must all be present;
rem extensions are not checked if this is empty;
rem `_MASK` should then be changed to end with `.m*`) */
set "_FILT=^M[0-9][0-9][0-9] [^,][^,]*, [A-Z][A-Z]\.[^\.][^\.]*$"
rem // (additional filter to find files; deactivate by `.*`)
set "_SEPS=," & rem /* (defines (a) separator character(s) to derive the
rem sub-directory; leave it blank to use full name) */
rem // Change into the root directory:
pushd "%_ROOT%" && (
rem /* Loop through all files matching the mask as well as the additional filter;
rem if the post-filtering is not needed, remove `^|` and everything behind: */
for /F "delims= eol=|" %%F in ('
dir /B /A:-D-H-S "%_MASK%" ^| findstr /I /R /C:"%_FILT%"
') do (
rem // Get the portion in front of the `,` of the file name:
for /F "delims=%_SEPS% eol=|" %%G in ("%%~nF") do (
rem // Split that portion at the first space to get the city name:
for /F "tokens=1* eol=|" %%H in ("%%G") do (
rem // initialise flag that indicates whether to move the current file:
set "FLAG=#"
rem // Skip the following checks if there are no extensions defined:
if defined _EXTS (
rem // Loop through the extensions in the list:
for %%E in (%_EXTS%) do (
rem /* Reset flag if file with current name and iterated extension
rem cannot be found; this ensures that files with all listed
rem extensions do exist, otherwise no files are moved: */
if not exist "%%~nF%%~E" set "FLAG="
rem /* Reset flag if file with current name and iterated extension
rem is actually a directory (though this is very unlikely): */
rem if exist "%%~nF%%~E\*" set "FLAG="
rem /* Reset flag if file with current name and iterated extension
rem is already located in the target sub-directory: */
if exist "%%I\%%~nF%%~E" set "FLAG="
)
)
rem // Do the following steps only if the flag has not been reset:
if defined FLAG (
rem // Create target sub-directory (suppress potential error message):
2> nul md "%%I"
rem // Check if there are dedicated extensions defined:
if defined _EXTS (
rem // Loop through the extensions in the list again:
for %%E in (%_EXTS%) do (
rem /* Move file with current name and iterated extension;
rem nothing is overwritten due to the preceding checks: */
> nul move "%%~nF%%~E" "%%I\%%~nF%%~E"
)
) else (
rem /* Empty list of extensions, hence just move the current file;
rem if you do want to overwrite, remove the `if exist´ part: */
if not exist "%%I\%%F" > nul move /Y "%%F" "%%I\%%F"
)
)
)
)
)
rem // Return from root directory:
popd
)
endlocal
exit /B
The values in the Define constants here: section at the top of the script are defined to suit your sample data, but they can easily be adapted there to configure the script at one place:
_ROOT: points to the directory where your input files are; %~dp0. points to the parent directory of the script, but you may of course specify any other absolute directory path here;
_MASK: is a file pattern that matches one file per pair (only .m2v files, others are covered by _EXTS); M??? matches the four-character prefix, but you can change it to M?*, for instance, to also match prefixes like M1 or M9999; if you do so, however, also edit _FILT accordingly;
_EXTS: defines a list of extensions that all must be present; that means for a certain base file name (like M372 Houston, TX, there must exist a file per each given extension, hence M372 Houston, TX.m2v and M372 Houston, TX.mpa in our situation, otherwise these files are not going to be moved; if you do not care if such a pair is complete or not, simply state set "_EXTS=" (so clear it) and change the extension of _MASK from .m2v to .m*, so all files with an extension beginning with .m are moved;
_FILT: constitutes an additional filter for file names in order to exclude wrong files; this currently also reflects a four-character prefix, but if this is not always the case, just change M[0-9][0-9][0-9] to M[0-9]*; if you do not want to filter, set this to .*, so it matches everything;
_SEPS: defines the character(s) to split the base file name in order to derive the respective sub-directory, so everything ending before that character and beginning after the first SPACE is the resulting sub-directory name; if you do not define a character here, the whole remaining base file name (so everything after the first SPACE until but not including the (last) .) is taken;

Windows batch file to find duplicates size files in harddisk (no matter the name and the extension)

I'd like a batch file ( Windows CMD is the interpreter, a .bat ) to do this type of task:
1) Search through a folder and its subfolders (entire harddisk)
2) Find files with the same size (no matter filename and extension)
3) Display these files (or not)
Thank you for any kind of help! :)
EDIT:
With the following commands, I can know all sizes that the files have...
#echo off
set "filename=*.*"
for %%A in (%filename%) do echo.Size of "%%A" is %%~zA bytes
but now the big problem is that you need to compare the first, with the remainder and so on!
The Batch file below should solve your problem; however, be aware that in despite of its apparent simplicity, it is based on advanced concepts, like array management in Batch files.
#echo off
setlocal EnableDelayedExpansion
rem Group all file names by size
for /R %%a in (*.*) do (
set "size[%%~Za]=!size[%%~Za]!,%%~Fa"
)
rem Show groups that have more than one element
for /F "tokens=2,3* delims=[]=," %%a in ('set size[') do (
if "%%c" neq "" echo [%%a]: %%b,%%c
)
This program may take too much time if the number of files is large or if the starting folder have a long path.

Write script to search the solution

I'm working on removing a large number of old and unused images from our website. We run ASP.NET with C# code behind, and do our work out of Visual Studio (2013). Right now I'm just going through our images directory and searching the solution for the image file name. While we have some filenames that follow a pattern and can be done in a group using regex, this is still rather tedious. Is there a way that I can write a batch script (or anything) to search the solution for every file in this directory? I can imagine pseudocode like
for file in images_directory
if file not in solution
delete file
but is this possible?
Technically we're just moving the files into another folder to be safe, so I guess the actual pseudocode would be more like
for file in images_directory
if file not in solution
move file to backup_directory
Within your solution file, find all references to .csproj files. Within each .csproj file, find all include lines. Within each included file, find all lines containing references to images. Copy each relevant line to a temporary list. This will make searching faster than searching every .cs file multiple times for every image.
For each graphic file, use findstr to perform a regexp search for /\bfilename\b/i within the temporary list. If not found, use conditional execution to initiate a move of the orphaned image to backup.
Save this with a .bat extension, modify the first three set lines to appropriate values, and give it a shot. By default, it only pretends to move. If you're satisfied that the simulations will produce correct results, remove echo from the move line near the bottom to let the script off its leash.
#echo off
setlocal
set "image_dir=c:\path\to\images"
set "sln_file=c:\path\to\solution\Project1.sln"
set "backup_dir=c:\path\to\backup"
set "remember=%temp%\proj_images.txt"
for %%I in ("%sln_file%") do pushd "%%~dpI"
rem // .sln -> .csproj -> .cs -> images. Find image references and remember.
del "%remember%" >NUL 2>NUL
for /f "delims=" %%I in ('findstr /i ".csproj\>" "%sln_file%"') do (
rem // %%I contains lines matching /.csproj\b/ig
for %%p in (%%I) do if exist "%%~p" (
rem // %%p contains a .csproj filename
for /f "delims=" %%J in ('findstr /i "\<include\>" "%%~p"') do (
rem // %%J contains lines matching /\binclude\b/ig
for %%c in (%%J) do if exist "%%~c" (
rem // %%c contains the filename of an include
findstr /i ".png\> .jpg\> .gif\> .bmp\> .tif\>" "%%~c" >>"%remember%" && (
echo Images referenced within %%~nxc. I'll remember this.
)
)
)
)
)
rem // for each image file in image_dir (recursive)
for /r "%image_dir%" %%I in (*.png *.jpg *.gif *.bmp *.tif) do (
rem // regexp test for /\bfilename.ext\b/i
findstr /i "\<%%~nxI\>" "%remember%" >NUL || (
rem // non-zero exit status of findstr means not found
echo %%~nxI is not referenced by any files included in the solution's projects.
rem // *********************************************************
rem // REMOVE "ECHO" FROM THE FOLLOWING LINE TO ENABLE THE MOVES
rem // *********************************************************
echo move "%%~fI" "%backup_dir%"
)
)
del "%remember%" >NUL 2>NUL
echo Press any key to exit.
pause >NUL
Is this what you had in mind?
All together If I understand correctly You first want to obtain all images files from directory. Using PowerShell:
$imageFiles = Get-ChildItem 'path/to/image/directory' -Recurse | Where-Object { !($_.PSIsContainer) }
This grabs all files excluding Directories. Then:
$solutionText = Get-Content 'path/to/solution/file.csproj' | Out-String
ForEach ($file in $imageFiles ) {
if ($solutionText -match $file.Name) {
# Move to another folder
}
}
The only issue is that you'd need to make sure that the filenames don't have a chance of matching elsewhere on the file giving false positives.

Batch file to perform a looped search based on the line items of a text file

I have been reading great posts in this forum and got close to what I want to do but couldn't figure out the exact code.
I want to create a windows batch file to do following:
Perform a looped search for each line item of a text file (this is a list of keyword) to locate files in a a specific directory
For this search partial match is okay.
Each time a file is found, move it to a predefined directory (e.g. C:\temp\search_results)
Thanks.
I'm not running Windows at the moment, so I can only post some ideas, not the solution.
1) Use for /f to iterate over file contents.
2) Use find "%Keyword%" %SourceDir% to get the list of matching files. You will have to parse out file names from the output of find.
2a) As an alternative, you can iterate over files in the source dir (with nested for) and call find for each file, discarding its output and using its exit code (%ERRORLEVEL%) to decide whether the file matches (it will return 0 if there is a match and nonzero if there is no match). Something like this:
for %%F in (%SourceDir%\*) do (
find "%Keyword%" %%F > nul
if not errorlevel 1 (echo File %%F matches) else (echo File %%F does not match)
)
3) Move matching files with move.
There are multiple problems.
FIND /i "%A%" ... can't work, the name of the FOR-Varibale is %%A
And the second proble: With FIND you check the content of the file not the name.
And you should use indention to avoid too much parenthesis.
You better try
FOR /F "tokens=*" %%A IN (%listfile%) DO (
FOR %%f in (%searchdir%\*) do (
set "filename=%%~f"
set replaced=!filename:%%A=!
if !replaced! NEQ !filename! (
echo !filename! contains '%%A'
)
)
)
It tries to replace %%A inside of the filename with .
If the replaced is not equal the filename, the filename must contain %%A
I wrote the following code but not sure if I am in the right track. Here is my setup:
list.txt file contents are (my keywords for the filename search) --
one
two
five
ten
six
f1 folder contains --
four.txt
one.txt
three.txt
I want to move the matching ones to F2 folder, but the code simplicity I am using echo instead.
My code is:
#ECHO OFF
SETLOCAL EnableDelayedExpansion
SET listfile=D:\batchtest\list.txt
SET searchdir=D:\batchtest\f1
FOR /F "tokens=*" %%A IN (%listfile%) DO (
FOR %%f in (%searchdir%\*) do (FIND /i "%A%" %%f
if errorlevel 1 (
echo Search failed) else (
echo Search successful
)
)
)
)
It is running but not finding matching filenames.
Thanks.

Resources