I would like a script to search within text files a certain pattern of strings and move those files to another directory - text

I need to know how to perform an advanced search on a text file using tools like notepad++, freecommander and windows if possible. A bat script file will be great.
The problem is that i need to search through about 1000 txt files in a directory. I need to know form those 1000 txt files which have a string in the form of for example "SYR_SHA/245/4". I just want it search for the pattern for example *****_******/*****/****** where * are characters that can change in number.
There must be an _ between the first and second set of characters as seen above in the example.
the script should go through the entire txt file and search for the above pattern. The script should then Move all the results in to a seperate directory
Many Thanks

Create this batch file and copy it into your folder where you have all the 1000 .txt files
Change the yourdestinationdirectory directory and run the batch file
#Echo Off
FindStr /M /R "[a-zA-Z]*_[a-zA-Z]*\/[0-9]*\/[0-9]*" *.Txt > findstr.out
For /F "tokens=*" %%a In (FindStr.out) Do call :move_Rtns %%a
del FindStr.out
Exit /B
:move_rtns
copy %1 yourdestinationdirectory\*
del /Q %1
Exit /B
Please change the Regex according to your requirement, for eg. If you are expecting number and letter together you can replace [a-zA-z] with [a-zA-Z0-9]
Good luck

Related

How to open a *.xlsx file containing a space in file name found by command DIR in Excel?

I have a batch file that opens an Excel file with a space as well as date in its file name.
For example: Book 1-27Aug2016
Currently, I am having trouble disabling the delimiters so that command start doesn't try to open two files: Book.xlsx and 1-27Aug2016.xlsx
Here is my code:
for /f "delims=*" %%# in ('dir /tw /o-d /b "Book 1-*"') do (start excel %%#& exit)
I referenced the web page in SS64 Windows CMD Shell forum below for disabling/modifying delimiters, but I still have yet to experience success.
For /f documentation
Lastly, once I remove the space from the file name, the batch runs without any issues.
What do I need to modify in the single line batch code to open also an Excel file with a space in file name in Excel?
Open a command prompt window, run in this window for /? and read very carefully all output help pages.
Command FOR with option /F splits up a string by default on spaces/tabs. It can be either used "tokens=*" or "delims=" to avoid this string splitting. The usage of "delims=*" works also for file names because the name of a file without or with path can't contain an asterisk. But "delims=*" is usually not used to prevent splitting up a string into tokens because a string read from a text file, output of an application or an environment variable could contain 1 or more asterisks as well.
Next run in a command prompt window cmd /? and read at least last output help page on which is explained on which characters in name of a file/folder without or with path the file/folder name string must be enclosed in double quotes. In general it is advisable to enclose file/folder names without/with path always in double quotes.
The command DIR returns with the used options just the names of the files without path and always without surrounding double quotes as it can be seen on running in a command prompt window in directory with Book 1-* files
dir "Book 1-*" /A-D /B /O-D /TW
after first running dir /? to get displayed the help for command DIR.
So used should be:
for /F "delims=" %%# in ('dir "Book 1-*" /A-D /B /O-D /TW 2^>nul') do ( start "" excel.exe "%%#" & exit /B )
2^>nul is 2>nul whereby the redirection operator > is escaped with ^ to apply this redirection on running command DIR instead of being interpreted as redirection of command FOR at an invalid position in command line. The command DIR outputs the error message File not found to handle STDERR if it can't find any file in current directory matching the pattern Book 1-*. This error message is suppressed by redirecting it to device NUL.
It is advisable to specify the application to start with file extension if well known even if the path is not known. Read answer on Where is “START” searching for executables? for an explanation.
In the batch command line above there are after command START also two double quotes before the name of the executable to start. Usage of "" is highly recommended as command START interprets often first double quoted string in arguments list as optional title string. By specifying explicitly an in this case empty title string helps to avoid unexpected application execution. For details on command START run in command prompt window start /?.
And last it is better to use command EXIT with parameter /B to exit just processing of current batch file and not exit entire command process. If this batch file is called with command CALL from another batch file or started from within a command prompt window and command EXIT is used without parameter /B, the processing would not continue on parent batch file respectively command prompt window would be closed by EXIT, too. There is no difference between usage of exit and exit /B if this batch file is executed by a double click on the batch file. For details on command EXIT run in a command prompt window exit /?.
By the way: The command START uses file association registration data if it can find an executable or script with given name to open the specified file with the registered application for the file extension.
So it would be also possible to use:
for /F "delims=" %%# in ('dir "Book 1-*" /A-D /B /O-D /TW 2^>nul') do ( start "" "%%#" & exit /B )
Now command START opens the *.xslx file with whatever application is associated with this file extension as default application for opening it.
Add double quotes around the last variable.
for /f "delims=*" %%# in ('dir /tw /o-d /b "Book 1-*"') do (start excel "%%#"& exit)

Search for a file and open the path in CMD

I would like to achieve the following in CMD:
Will search for a specific filename - This I know how to do, with dir /s filename
Once found it will bring me to that path.
Example: I am now on C:\, if that file was found in C:\test then it will open C:\test in command prompt.
Alternatively, I would like just to copy the found file to a path I will specify. I just don't know how to do it, since I don't know the path where the file will be stored (it's different every time).
Thanks in advance!
I'm a liitle confused with your question but I will try to walk you through a batch code I wrote to help you with this.
dir /b /s "test.txt" > "%userprofile%/Desktop/result.txt"
::find the file path to the file you want to find and insert it in a .txt file
::you made called result.txt (also %userprofile% is a variable that brings you to
::your user directory ex C:/users/admin)
for /F "tokens=*" %%A in (%userprofile%/desktop/result.txt) do (
set var1=%%A
)
::set the variable var1 equal to the first (well... only) line in the file.
::I could explain what that for loop means in detail but just remember that %%A
::is a variable set from what was found when looping through the result.txt.
xcopy /s "%var1%" "C:/wherever/you/want/it/to/go"
did this help??

Batch - Find string in text file and delete full line

I use the script from this Thread accepted answer from Mofi. The script copy folders and store them in text file to exclude once copied folders on next run.
Sometimes I have folders called [incomplete]-different.names and I do not want to copy this folders. I want that all folders with the string [incomplete]- and the name behind are skipped or are not even written in the text file %CurrentList% for further processing.
These are my previous attempts but so far I could not get up and running with the script from the top.
Help would be nice, and thanks in advance.
Try 1:
for /f "delims=" [incomplete]-%%D in ("%CurrentList%") do (
set str=%%D
set str=!str: =!
set str=!str: %%D =!
echo !str!
Try 2:
findstr /v /b /c:"[incomplete]-"%%D" "%CurrentList%" del "%%D"
You were really close with your second attempt.
If all you want is to delete lines in %CurrentList% that contain the string [incomplete]-, you can just direct the output of a findstr /v to a temp file and then overwrite CurrentList with that file.
findstr /v /c:"[incomplete]-" "%CurrentList%" >tmpList.txt
move /y tmpList.txt "%CurrentList%" >nul

Move 300 images from a folder (contains 800 images) to another based on file name list

I need to move 300 images from a folder (contains 800 images) to another folder. The file name list of these 300 images are available in the excel format. Is it possible to move them via programming instead of search the file and move it one by one? Our IT told me he can't separate these files. Do you have any solution? Many thanks in advance!!!
Here's one way of doing this - I am assuming you are on Windows. First, save text file called ListOfImages.txt that contains the names of the images you wish to move - put one image on each line and include the extension. Then, save the following into a file called movefiles.cmd:
#echo off
set Source=C:\Users\YourName\Desktop\moving\MovingFrom
set Target=C:\Users\YourName\Desktop\moving\MovingTo
set FileList=C:\Users\YourName\Desktop\moving\ListOfImages.txt
echo.
if not exist "%Source%" echo Source folder "%Source%" not found & goto Exit
if not exist "%FileList%" echo File list "%FileList%" not found & goto Exit
if not exist "%Target%" md "%Target%"
for /F "delims=" %%a in ('type "%FileList%"') do move "%Source%\%%a" "%Target%"
:Exit
echo.
echo press the Space Bar to close this window.
pause > nul
You will want to change the variables for Source, Target, and FileList to match where you have those folders and the ListOfImages.txt on your machine. After you have saved this file (make sure it has the .cmd extension, you should be able to double-click it and it will run the commands in your Command Prompt.
For example, say my MovingFrom folder contains the following:
And I only want to move Image1.png and Image2.png -- then my ListOfImages.txt file would like this:
After running moveFiles.cmd (provided I have changed the necessary variables to point to the right folders/places on my machine), my MovingTo folder should contain the following:
Notice that Image2.png was not moved because it was not listed in the ListOfImages.txt text file.

Finding text with a batch file using Notepad++

I need to find text in a file when I open it with Notepad++.
For instance I need to find the text "ABC". However the true text I need are 2 numbers that follow "ABC". So I have a batch file that uses Notepad++ to open a particular file when I double click it. It needs to find ABC and then look at the 2 digits directly following it and populate a variable with them.
Some files might contain ABC12 while others might contain ABC21, etc.
All I have so far is the following:
%echo off
"C:\Program Files (x86)\Notepad++\notepad++.exe" %1
FIND [/I] "ABC"
It opens the file fine, but does not search. Please help!
Thanks!
this is a really nice question! Try this:
#ECHO OFF &SETLOCAL
set "LineNr="
for /f "tokens=1*delims=[]" %%a in ('^<"%~1" find /i /n "%~2"') do if not defined LineNr (
set "LineNr=%%a"
SET "Line=%%b"
)
if not defined LineNr (
set "LineNr=1"
SET "Row=1"
GOTO :launch
)
CALL SET "Right=%~2%%Line:*%~2=%%"
CALL SET "Line=%%Line:%Right%=%%"
FOR /f "delims=:" %%a in ('"(echo("%Line%"&#echo()|findstr /o $"') do SET /a Row=%%a-4
:launch
START /b CMD /c ""%ProgramFiles(x86)%\Notepad++\notepad++.exe" -n%LineNr% -c%Row% "%~1""
calling the batch:
script.bat "D:\PATH\FILE.TXT" "STRING TO SEARCH"
notepad++ command line switches

Resources