Renaming Multiple Files - Command Prompt acting odd if want to add numbers - rename

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"
)

Related

Batch file extract string

I know this has been asked numerous times, but I've been researching for like 2 hours and still can't do that.
I need a batch script to extract a string from a file.
The content of the file is this:
C:\Windows\system32\tasks{7D7A0547-0D79-0805-0A11-0B780D08110D}
I want to extract this part:
{7D7A0547-0D79-0805-0A11-0B780D08110D}
I tried it with for /f command and all kinds of options and searches, but I just can't do it.
TIA
for /f "tokens=2 delims={}" %%# in ("C:\Windows\system32\tasks{7D7A0547-0D79-0805-0A11-0B780D08110D}") do echo {%%#}
?
#echo off
for /f "tokens=2 delims={}" %%A in (
'findstr "{[0-9A-F-]*}" "X:\path\yourfile.ext" '
) Do Echo %%A

Batch File : Clean every line from several files which doesn't have a String?

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"
)
)

Batch search multiple strings simultaneously

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

Check every string in textfile for substring

I've searched a long time and didn't find something useful for my problem. It may sound simple, I would be very happy if somebody could help me:
I want to write a batch script, which proves every string in a textfile whether it contains a specific substring. If this is the case, the whole string, which contains this substring, should be printed out.
The strings, I'm looking for, are surrounded by double quotes.
My code just works for all lines of my textfile, but I need it for all strings.
Thx in advance!
#echo off
setlocal enableextensions enabledelayedexpansion
for /f "delims=" %%A in ('findstr "somesubstring" "textfile.txt"') do (
echo %%A
)
Perhaps is this what you want?
#echo off
setlocal enabledelayedexpansion
set "substring=somesubstring"
for /f "delims=" %%A in ('findstr "%substring%" "textfile.txt"') do (
for %%B in (%%A) do (
set "string=%%~B"
if "!string:%substring%=!" neq "!string!" echo %%B
)
)
This Bath file may fail if the characters outside the "strings" (not enclosed in quotes) are special Batch characters.
I would first use a tool to isolate each string on a single line. Then you can use FINDSTR to return only quoted lines that contain the substring
The trickiest part is isolating the quoted strings. My REPL.BAT regex search and replace utility is a good option. It is a hybrid JScript/batch script that will run natively on any modern Windows machine from XP onward.
type "textfile.txt" | repl (\q.*?\q) \r\n$1\r\n x | findstr /x ^"\".*substring.*\"^"
If you want to see your strings without enclosing quotes, then:
for /f delims^=^ eol^= %%A in (
'type "textfile.txt" ^| repl (\q.*?\q) \r\n$1\r\n x ^| findstr /x ^"\".*substring.*\"^"'
) do echo %%~A

Parse PATH using FOR /F in BAT script

I need to parse the %PATH% list in a .BAT script, but I'm not having much luck with paths that contain spaces.
for %%a in (%PATH%) do #echo %%a
The above parses on spaces (default), but I need to parse on semi-colons. I'm trying to use this but it's throwing me an error:
for /f "tokens=* delims=;" %%a in (%PATH%) do #echo %%a
The result is one line: "The system cannot find the file C:\Windows\system32."
I'm sure I'm missing something very basic but any help would be greatly appreciated. TY!
SET TempPath="%Path:;=";"%"
FOR %%a IN (%TempPath%) DO echo.%%~a
To do this correctly, you need something more complicated than a simple FOR. Try the following in a batch file:
#ECHO OFF
SET TEMPPATH=%PATH%
:PARSE_START
IF "%TEMPPATH%"=="" GOTO EXIT
FOR /F "tokens=1* delims=;" %%a in ("%TEMPPATH%") Do ECHO %%a
FOR /F "tokens=1* delims=;" %%a in ("%TEMPPATH%") Do SET TEMPPATH=%%b
GOTO PARSE_START
:EXIT

Resources