I have a function already working to remove all lines which doesn't contain a string in several files, it's working great to use with common strings:
#echo off
set "string_to_find=level.waypoints["
for /f "tokens=*" %%a in ('dir /B *.gsc') do (
set "tempfile=%temp%\%%a"
if exist "%tempfile%" del "%tempfile%" >NUL
findstr /C:"%string_to_find%" "%~dp0\%%a" >> "%tempfile%"
if not errorlevel 1 (
del "%%a" >NUL
move /Y "%tempfile%" "%~dp0\%%a" >NUL
if exist "%tempfile%" del "%tempfile%" >NUL
echo File "%~dp0\%%a" processed successfully
) else (
echo Problem processing file "%~dp0\%%a"
)
)
If I search for just "level.waypoints" (without the "[") works fine but few lines that I don't want to keep are not deleted. I need to search for "level.waypoints[" to really delete all the lines that I need to, but because the char "[" make everything goes wrong, messing up with the temp files, giving in the end the error "file not found"...
I think should have some char that I need to put before "[" like "[" to make it work, but I can't find it... tryed already many without luck. :/
So how can I search for the string "level.waypoints[" and works?
thanks
On the link sent by #jmoon says:
Special Cases
A small number of commands follow slightly different rules, FINDSTR, REG and RUNAS all use \ as an escape character instead of ^
Tryed and works! so the end string is "level.waypoints\["
cheers!
Related
So far I have this:
#ECHO OFF
dir *.txt /c /b /on > content.txt
Which gives output:
file1.txt
file2.txt
file3.txt
But I need it like this, separated with semicolon on each line:
file1.txt;
file2.txt;
file3.txt;
I assume I probably need to write for loop and add string ";" somewhere, but I don't know where or how to do this. Or is there a way to just set a specific delimiter?
Edit:
My usecase changed, I thought it would be better if there are files in subfolders listed as well, but "/" should be replaced with space " ".
Example output:
file1.txt;
file2.txt;
subfolder1 file1.txt;
subfolder2 file1.txt;
Note that I do not want the full parent path, only subfolders.
Quick single line batch-file answer:
#(For /F Tokens^=*^ Delims^=^ EOL^= %%G In ('Dir "*.txt" /A:-D /B /O:N 2^>NUL') Do #Echo %%G;) 1>"content.log"
…and in cmd:
(For /F Tokens^=*^ Delims^=^ EOL^= %G In ('Dir "*.txt" /A:-D /B /O:N 2^>NUL') Do #Echo %G;) 1>"content.log"
I have decided to output to a .log file, so that the listing doesn't include itself.
Please use the built-in help to learn how each command works.
When you read the help information, please be aware that a 'simple' for loop will not pick up all files, it will ignore all hidden files for instance. Also despite any first impressions you may have from testing, the order of files returned, depends upon both the file system and type. The dir command is the most efficient way of ensuring that sort order.
[EDIT /]
Here is a batch-file solution, (as that's what you posted as an answer), for your New and completely different question.
#(For /F Tokens^=*^ Delims^=^ EOL^= %%G In ('Dir "*.txt" /A:-D /B /O:N /S 2^>NUL') Do #(Set "FileName=%%~dpG" & SetLocal EnableDelayedExpansion & Set "Filename=!FileName:~,-1!" & For %%H In ("!FileName:%__CD__%=!") Do #EndLocal & Echo %%~H %%~nxG;)) 1>"content.log"
In future, when you have existing answers to your asked question, do not change that question when not only the main command is different, but the intended result format too.
a Simple for loop will do:
#(for %%i in (*.txt) do #echo %%i;)>"content.txt"
That will however also echo the content of contents.txt to itself if it exists, so you can exclude it.
#(for %%i in (*.txt) do #if /i not "%%~i" == "content.txt" #echo %%i;)>"content.txt"
if the plan is to iterate through subdirs as well, run for /R
I found a partial solution to list subfolders, excluding parent path. It is not perfect (for loop is not accurate as mentioned in above comments, neither I am happy with pushd command), but works. I left "space" in place of \ because thats how I need it.
#echo off
setlocal enabledelayedexpansion
pushd "c:\users\documents\"
(
for /r %%a in (*.txt) do (
set x=%%a
set x=!x:%cd%=!;
echo !x:\= !
)
)>filelist.log
popd
Code explanation:
First I remove the parent path with set x=!x:%cd%=!; (exclamation marks replace the %, see enabledelayedexpansion help) and then remove the slashes when echoing.
I need to clean several txt files at once, cleaning all the lines which doesn't have a string mark. Need to be Windows batch file.
exemple:
set string="abcd"
for each *.txt file do (
if line doesn't have string delete line
)
I don't want to generate new files, just update the original ones deleting the lines which doesn't have the string.
Of course you can create a temp file and overwrite the original one too without change the name. Doesn't matter how you do it, just the result is the same files but clean.
thank you
Edited fixing the errors, now it's working great! cheers!
#echo off
set "string_to_find=some string"
for /f "tokens=*" %%a in ('dir /B *.txt') do (
set "tempfile=%temp%\%%a"
if exist "%tempfile%" del "%tempfile%" >NUL
findstr /C:"%string_to_find%" "%~dp0\%%a" >> "%tempfile%"
if not errorlevel 1 (
del "%%a" >NUL
move /Y "%tempfile%" "%~dp0\%%a" >NUL
if exist "%tempfile%" del "%tempfile%" >NUL
echo File "%~dp0\%%a" processed successfully
) else (
echo Problem processing file "%~dp0\%%a"
)
)
I have this large database of equipment:
Equipment500
Equipment501
..........
Equipment998
Equipment999
As well as an even larger database with details about equipment:
Equipment1:details....
Equipment2:details....
..................
Equipment9998:details....
Equipment9999:details....
What i need, is to select only the details for equipment i need:
for /f "tokens=* delims= " %%a in (%cd%\equipment.db) do (
findstr /i /c:"%%a" details.db > Output\%%a
)
The output will be, of course, a folder with files:
In Equipment500 it will be Equipment500:details....
In Equipment501 it will be Equipment501:details....
..................
In Equipment998 it will be Equipment998:details....
In Equipment999 it will be Equipment999:details....
The problem is that it takes a lot of time.
I need this multithreaded so that it runs more instances of findstr (preferably all 500) at the sametime to do processing instantly.
Any idea is appreciated. Thank you!
#echo off
echo building input files (this needs some time):
del *.db
for /l %%i in (500,1,999) do #echo Equipment%%i>>equipment.db
for /l %%i in (1,1,9999) do #echo Equipment%%i:Detailswhatever>>details.db
echo %time% start adapting
REM adapt equipment.db:
(for /f "delims=" %%i in (equipment.db) do echo %%i:)>equip.db
REM find all strings:
echo %time% start searching
findstr /g:equip.db details.db >output.txt
echo %time% done
NOTE: "Equipment.db" has to be adapted, because searching for "Equipment2" would also find "Equipment20", Equipment21"... "Equipment200" ...
Since you only provide vague information about your file structure, I'd suggest
#echo off
for /f "tokens=1*delims=:" %%a in (details.db) do >>%%a.dat echo %%b
which assumes each entry in details.db is of the form
equipment1234:details
I have a txt file that contains about 500 values, one per line. I need to check to see of any of those 500 values appear in any of 6 csv files each containing 100k lines. I can search for one value in those 6 csv files using
for /f "delims==" %%f in ('dir /s /b "P:\*.txt"') do FIND /N "[SEARCHSTRING]" "%~1%%f" >> "C:\found.txt"
but how do I do multiple searches automatically via command-line or batch file (CaSe SenSiTIve)?
#ECHO OFF
SETLOCAL
SET "sourcedir=c:\sourcedir"
SET "destdir=C:\destdir"
for /f "delims=" %%a in ('dir /s /b "%sourcedir%\*.csv"') do (
FINDSTR /N /g:"yourtextfilecontaining500linestomatch.txt" "%%~fa") > "%destdir%\%%~nafound.txt"
GOTO :EOF
What you are asking is rather unclear. I used c:\sourcedir as the location of the .csv files and c:\destdir as the location for the reports. Replacing
FINDSTR /N /g:"yourtextfilecontaining500linestomatch.txt" "%%~fa") > "%destdir%\%%~nafound.txt with your original (with the double > would accumulate the lines into a single file - if that's what you want. As it stands, a new file will be created with name the same as your .csv+found.txt
An easy way is to use a batch script. You can loop through each of the files one by one. If you want to do them all at once you need to thread your program.
for /L %%A in (1,1,6) do (
Your code goes here
)
That batch script will loop six times. I am not really sure how you specify the file but if you loop through each file it will work.
So put your current batch script where I said "Your code goes here"
for /f "delims==" %%f in ('dir /s /b "P:\*.txt"') do FIND /N "[SEARCHSTRING]" "%~1%%f" >> "C:\found.txt"
But you need to edit it to point to the file that you want to search. If your files are 1.txt, 2.txt 3.txt then all you need to do is set your file name to the current loop iteration number.
I've been using variants of this shell function for years:
ematch () {
for f in $(find . -type f | grep -v '~' | grep -v \.svn\/) ; do
egrep "$1" "$f" /dev/null 2> /dev/null
done
}
-> ematch "(string1|string2|string3)"
Feel free to adapt to your needs and post your mods here.
My camera only numbers photos using 4 digits, but I am now well into the 5 digit realm. So for thousands of photos, I would like to add a fifth digit. E.g. rename IMG_2450 to IMG_12450.
I did this before about a year ago using Command Prompt, but I'm having trouble replicating those results today.
I tried: ren IMG_*.jpg IMG_1*.jpg
But what ends up happening is that instead of adding the number 1, command prompt ends up replacing the first character of the existing string of numbers.
So, IMG_2450 becomes IMG_1450 rather than IMG_12450.
What am I doing wrong here?
This is untested - try it on some sample folders first.
It will process all the img_*.jpg files under the current directory tree and expects the filenames to be in the format you described.
#echo off
for /f "delims=" %%a in ('dir /ad /b /s') do (
pushd "%%a"
for /f "delims=" %%b in ('dir img_*.jpg /a-d /b') do (
for /f "tokens=1,* delims=_" %%c in ("%%b") do ren "%%b" "%%c_1%%d"
)
popd
)
If you want to process one folder only then try this (after testing):
#echo off
for /f "delims=" %%b in ('dir img_*.jpg /a-d /b') do (
for /f "tokens=1,* delims=_" %%c in ("%%b") do ren "%%b" "%%c_1%%d"
)