Batch incremental name change only updates a few files at a time - windows

I am attempting to create a batch file that will rename a series of file based on an XML file. It works until it encounters a file name with the same name at which point it skips the file. I was able to amend the script to create an incremental version of the file, the issue that I am having now is the script will cycle through just a few files and then it exits. Any ideas why it's doing that?
for %%z in ("C:\Recordings\AT1*.WAV") do (
for /f tokens^=4^,8^,10^,12^ delims^=^" %%a in ('type "C:\Recordings\index.xml"^|find /i "%%~nxz"') do (
for /f "tokens=1,2 delims=:" %%t in ("%%b") do (
ren "%%z" "%%c-%%t-%%u_%%d%%~xz" 2>nul
if errorlevel 1 set "Number=2" & call :NumberedRename "%%z" "%%c_%%t-%%u_%%d%%~xz"
goto :EOF
)
)
)
)
:NumberedRename
if exist "%~n2_%Number%%~x2" set /A "Number+=1" & goto NumberedRename
ren %1 "%~n2_%Number%%~x2"
goto :EOF
Here is an example of the output that I am able to see at the moment:
C:\Recordings>for %z in ("C:\Recordings\AT1*.WAV") do (for /F tokens=4,8,10,12 delims=" %a in ('type "C:\Recordings\index.xml"|find /i "%~nxz"') do (for /F "tokens=1,2 delims=:" %t in ("%b") do (
ren "%z" "%c-%t-%u_%d%~xz" 2>nul
if errorlevel 1 set "Number=2" & call :NumberedRename "%z" "%c_%t-%u_%d%~xz"
goto :EOF
) ) )
C:\Recordings>(for /F tokens=4,8,10,12 delims=" %a in ('type "C:\Recordings\index.xml"|find /i "AT1_ID1_TT3_ID6-1626034093.52156.WAV"') do (for /F "tokens=1,2 delims=:" %t in ("%b") do (
ren "C:\Recordings\AT1_ID1_TT3_ID6-1626034093.52156.WAV" "%c-%t-%u_%d.WAV" 2>nul
if errorlevel 1 set "Number=2" & call :NumberedRename "C:\Recordings\AT1_ID1_TT3_ID6-1626034093.52156.WAV" "%c_%t-%u_%d.WAV"
goto :EOF
C:\Recordings>if exist "John Doe 1_2021-07-11 15-08_9394056960_2.WAV" set /A "Number+=1" & goto NumberedRename
C:\Recordings>ren "C:\Recordings\AT1_ID1_TT3_ID6-1626034093.52156.WAV" "John Doe 1_2021-07-11 15-08_9394056960_2.WAV"
) ) )

Shortly after posting the question I tinkered with the script and realized that one of the goto :EOF was causing the script to end prematurely. I removed the goto :EOF and it seems to be working as intended.
Updated Script
for %%z in ("C:\Recordings\AT1*.WAV") do (
for /f tokens^=4^,8^,10^,12^ delims^=^" %%a in ('type "C:\Recordings\index.xml"^|find /i "%%~nxz"') do (
for /f "tokens=1,2 delims=:" %%t in ("%%b") do (
ren "%%z" "%%c-%%t-%%u_%%d%%~xz" 2>nul
if errorlevel 1 set "Number=2" & call :NumberedRename "%%z" "%%c_%%t-%%u_%%d%%~xz"
)
)
)
)
:NumberedRename
if exist "%~n2_%Number%%~x2" set /A "Number+=1" & goto NumberedRename
ren %1 "%~n2_%Number%%~x2"
goto :EOF

Related

Batch variable store result from command

It seems for me a very simple question, however I am struggling a lot with this and don't find an answer yet.
My aim is to count occurences of a specific string in a file.
I do or try to do this using following command:
for /f "tokens=2 delims=;" %%a in ('path to file with searchstring') do (
set count=('path to file I am searching in' find /c "%%a")
if !count! NEQ 0 echo !count!
)
Seems straightforward for me however the second line seems to be wrong as the script exits.
I want to save the result of findin a var because I only wants to outpout Nonzero resutls.
Here's an example batch-file using findstr, for better word matching, (It uses the filenames from your previous question):
#(For /F "UseBackQTokens=2Delims=;" %%G In ("classID.txt")Do #(Set "count=0"
For /F %%H In ('^""%__AppDir__%findstr.exe" /IN "\<%%G\>" "trc.txt"^"'
)Do #(Set /A count+=1)&SetLocal EnableDelayedExpansion
If !count! Gtr 0 (Echo %%G: !count!)&Endlocal))&Pause
Here it is fully parenthesized over multiple lines for you to better understand:
#Echo Off
For /F "UseBackQ Tokens=2 Delims=;" %%G In (
"classID.txt"
) Do (
Set "count=0"
For /F %%H In (
'^""%__AppDir__%findstr.exe" /I /N "\<%%G\>" "trc.txt"^"'
) Do (
Set /A count +=1
)
SetLocal EnableDelayedExpansion
If !count! Gtr 0 (
Echo %%G: !count!
)
Endlocal
)
Pause
And a fully parenthesized example of it outputting the results to a file named results.txt:
#Echo Off
(
For /F "UseBackQ Tokens=2 Delims=;" %%G In (
"classID.txt"
) Do (
Set "count=0"
For /F %%H In (
'^""%__AppDir__%findstr.exe" /IN "\<%%G\>" "trc.txt"^"'
) Do (
Set /A count +=1
)
SetLocal EnableDelayedExpansion
If !count! Gtr 0 (
Echo %%G: !count!
)
Endlocal
)
)>"results.txt"

How to remove names from a list in batch

I'm using windows bath, I have a list of names that I can add to but I don't know how to remove a name from the list.
So far my code is:
#echo off
setlocal enabledelayedexpansion
set allchoices=123456789
set "names=Bob,Steven,Harry"
set amount=6 ::max limit of list
set list=0
:start
::echoes a list of all names in the list
for /l %%i in (1; 1; %amount%) do (
call :sub %%i
)
goto check
:sub
for /f "tokens=%1 delims=," %%a in ("%names%") do (
echo %%i. %%a
set /a list=list+1
)
goto :eof
:check
::Remove a name from the list
choice /c !allchoices:~0,%list%! /m "What name do you want to remove?"
if errorlevel 3 (
for /f "tokens=3 delims=," %%a in ("%names%") do (
echo you have choosen to remove %%a
::remove third name in the list
goto start
)
)
if errorlevel 2 ::remove second name in the list
if errorlevel 1 ::remove first name in the list
I've tried using del but that turns out to delete a file in your folder.
I've tried renaming a specific name using set name[%%a]="" but that did nothing.
Any ideas?
Have a look at this example. There are many ways.
#echo off
setlocal enabledelayedexpansion
set names="Bob","Steven","Harry"
for %%i in (%names%) do (
set /a num+=1
set "!num!=%%~i"
)
for /l %%a in (1,1,%num%) do (
set choices=!choices!%%a
echo !num!.!%%a!
)
choice /c 123 /m "please select name to remove"
for /l %%a in (1,1,%num%) do if not "!%%a!"=="!%errorlevel%!" set new_names=!new_names! !%%~a!
echo %new_names:~1%
It can be done without the last for loop as well.. but I opted for it.
Here is some example code, for you to run, and then try to comprehend, I hope it helps rather than confuses:
#Echo Off
SetLocal EnableDelayedExpansion
For /F "Delims==" %%A In ('Set # 2^>NUL') Do Set "%%A="
Set "i=0"
For /F "Delims=:" %%A In ('FindStr "^::" "%~f0" 2^>NUL') Do (
Set /A i+=1
Set "#!i!=%%A"
Echo= !i!. %%A
)
:Ask
Set # 1>NUL 2>&1
If ErrorLevel 1 Echo= Empty names list&Timeout 3 1>NUL&Exit /B
Echo=&Set /P "Option= Choose a name to remove>"
Set #|Findstr "^#%Option%=" 1>NUL||GoTo :Ask
Set "Name=!#%Option%!"
Echo= You've chosen to remove "%Name%"
Timeout 2 1>NUL
Set "#%Option%="
ClS
For /F "Tokens=1*Delims=#=" %%A In ('Set # 2^>NUL') Do Echo= %%A. %%B
GoTo Ask
::Alan
::Beth
::Cleo
::Dale
::Eric
::Faye
::Greg
::Hugh
::Inga
Important note:Please ensure, before saving the above content as a Windows Command Script, that there is a line return, (blank line), at the end.

The process cannot access the file because it is being used by another process.(Batch file)

Device1.bat
#echo off
:: script for updating property files
SETLOCAL EnableExtensions
SETLOCAL EnableDelayedExpansion
if "%3"=="" (
ECHO Script will optionally accept 3 args: PropKey PropVal File
SET PROPKEY=Deviceid
SET PROPVAL=dfs2341f
SET FILE=config.properties
) ELSE (
SET PROPKEY=%1
SET PROPVAL=%2
SET FILE=%3
)
FINDSTR /B %PROPKEY% %FILE% >nul
IF %ERRORLEVEL% EQU 1 GOTO nowork
MOVE /Y "%FILE%" "%FILE%.bak"
FOR /F "USEBACKQ tokens=*" %%A IN (`TYPE "%FILE%.bak" ^|FIND /N /I "%PROPKEY%"`) DO (
SET LINE=%%A
)
FOR /F "tokens=1,2* delims=]" %%S in ("%LINE%") DO SET LINE=%%S
SET /A LINE=%LINE:~1,6%
SET /A COUNT=1
FOR /F "USEBACKQ tokens=*" %%A IN (`FIND /V "" ^<"%FILE%.bak"`) DO (
IF "!COUNT!" NEQ "%LINE%" (
ECHO %%A>>"%FILE%"
) ELSE (
ECHO %PROPKEY%=%PROPVAL%>>"%FILE%"
ECHO Updated %FILE% with value %PROPKEY%=%PROPVAL%
)
SET /A COUNT+=1
)
GOTO end
:nowork
echo Didn't find matching string %PROPKEY% in %FILE%. No work to do.
pause
:end
Device2.bat
#echo off
:: script for updating property files
SETLOCAL EnableExtensions
SETLOCAL EnableDelayedExpansion
if "%3"=="" (
ECHO Script will optionally accept 3 args: PropKey PropVal File
SET PROPKEY=Deviceid
SET PROPVAL=dfs2341f
SET FILE=config.properties
) ELSE (
SET PROPKEY=%1
SET PROPVAL=%2
SET FILE=%3
)
FINDSTR /B %PROPKEY% %FILE% >nul
IF %ERRORLEVEL% EQU 1 GOTO nowork
MOVE /Y "%FILE%" "%FILE%.bak"
FOR /F "USEBACKQ tokens=*" %%A IN (`TYPE "%FILE%.bak" ^|FIND /N /I "%PROPKEY%"`) DO (
SET LINE=%%A
)
FOR /F "tokens=1,2* delims=]" %%S in ("%LINE%") DO SET LINE=%%S
SET /A LINE=%LINE:~1,6%
SET /A COUNT=1
FOR /F "USEBACKQ tokens=*" %%A IN (`FIND /V "" ^<"%FILE%.bak"`) DO (
IF "!COUNT!" NEQ "%LINE%" (
ECHO %%A>>"%FILE%"
) ELSE (
ECHO %PROPKEY%=%PROPVAL%>>"%FILE%"
ECHO Updated %FILE% with value %PROPKEY%=%PROPVAL%
)
SET /A COUNT+=1
)
GOTO end
:nowork
echo Didn't find matching string %PROPKEY% in %FILE%. No work to do.
pause
:end
I am running device1.bat and device2.bat at a time. Device1.bat is running properly.But device2.bat throwing The process cannot access the file because it is being used by another process.please help me to solve this problem.
You could make a temporary copy of the config.properties for the device2 to use and delete it later.
DEVICE 1:
#echo off
:: script for updating property files
SETLOCAL EnableExtensions
SETLOCAL EnableDelayedExpansion
if "%3"=="" (
ECHO Script will optionally accept 3 args: PropKey PropVal File
SET PROPKEY=Deviceid
SET PROPVAL=dfs2341f
SET FILE=config.properties
::make a temporary copy of config.properties
copy config.properties config2.properties
) ELSE (
SET PROPKEY=%1
SET PROPVAL=%2
SET FILE=%3
)
FINDSTR /B %PROPKEY% %FILE% >nul
IF %ERRORLEVEL% EQU 1 GOTO nowork
MOVE /Y "%FILE%" "%FILE%.bak"
FOR /F "USEBACKQ tokens=*" %%A IN (`TYPE "%FILE%.bak" ^|FIND /N /I "%PROPKEY%"`) DO (
SET LINE=%%A
)
FOR /F "tokens=1,2* delims=]" %%S in ("%LINE%") DO SET LINE=%%S
SET /A LINE=%LINE:~1,6%
SET /A COUNT=1
FOR /F "USEBACKQ tokens=*" %%A IN (`FIND /V "" ^<"%FILE%.bak"`) DO (
IF "!COUNT!" NEQ "%LINE%" (
ECHO %%A>>"%FILE%"
) ELSE (
ECHO %PROPKEY%=%PROPVAL%>>"%FILE%"
ECHO Updated %FILE% with value %PROPKEY%=%PROPVAL%
)
SET /A COUNT+=1
)
GOTO end
:nowork
echo Didn't find matching string %PROPKEY% in %FILE%. No work to do.
pause
:end
DEVICE2.bat:
#echo off
:: script for updating property files
SETLOCAL EnableExtensions
SETLOCAL EnableDelayedExpansion
if "%3"=="" (
ECHO Script will optionally accept 3 args: PropKey PropVal File
SET PROPKEY=Deviceid
SET PROPVAL=dfs2341f
::changed config.properties to config2.properties
SET FILE=config2.properties
) ELSE (
SET PROPKEY=%1
SET PROPVAL=%2
SET FILE=%3
)
FINDSTR /B %PROPKEY% %FILE% >nul
IF %ERRORLEVEL% EQU 1 GOTO nowork
MOVE /Y "%FILE%" "%FILE%.bak"
FOR /F "USEBACKQ tokens=*" %%A IN (`TYPE "%FILE%.bak" ^|FIND /N /I "%PROPKEY%"`) DO (
SET LINE=%%A
)
FOR /F "tokens=1,2* delims=]" %%S in ("%LINE%") DO SET LINE=%%S
SET /A LINE=%LINE:~1,6%
SET /A COUNT=1
FOR /F "USEBACKQ tokens=*" %%A IN (`FIND /V "" ^<"%FILE%.bak"`) DO (
IF "!COUNT!" NEQ "%LINE%" (
ECHO %%A>>"%FILE%"
) ELSE (
ECHO %PROPKEY%=%PROPVAL%>>"%FILE%"
ECHO Updated %FILE% with value %PROPKEY%=%PROPVAL%
)
SET /A COUNT+=1
)
GOTO end
:nowork
echo Didn't find matching string %PROPKEY% in %FILE%. No work to do.
pause
::delete the temporary config2.properties
del /f /q config2.properties
:end
hope that helps!

read then write delimited file - Batch cmd

Hi I have a pipe | delimited file.
I need to reverse all the numbers in there
The file looks like this
Entity|Division|Channel|200|300|800
I need to read the file an create a new one with reversed numbers
Entity|Division|Channel|-200|-300|-800
trying to make this work but, not entirely sure how to amend the text I'm getting. I need help on the :processToken procedure. How to output the tokens into a new file and add "-" and add back the delimiter |
for /f "tokens=* delims= " %%f in (M:\GPAR\Dev\EQ\Upload_Files\eq_test.txt) do (
set line=%%f
call :processToken
)
goto :eof
:processToken
for /f "tokens=* delims=|" %%a in ("%line%") do (
)
if not "%line%" == "" goto :processToken
goto :eof
Thanks
#echo off
set "ent_file=c:\entity.txt"
break>"%tmp%\rev.txt"
for /f "usebackq tokens=1,2,3,4,5,6 delims=|" %%a in ("%ent_file%") do (
echo %%a^|%%b^|%%c^|-%%d^|-%%e^|-%%f
)>>"%tmp%\rev.txt"
move /y "%tmp%\rev.txt" "%ent_file%"
del /q /f "%tmp%\rev.txt"
Should work if your file contains only lines like the one you've posted.
EDIT output the part after the 6th token:
#echo off
set "ent_file=c:\entity.txt"
break>"%tmp%\rev.txt"
for /f "usebackq tokens=1,2,3,4,5,6,* delims=|" %%a in ("%ent_file%") do (
echo %%a^|%%b^|%%c^|-%%d^|-%%e^|-%%f|%%g
)>>"%tmp%\rev.txt"
move /y "%tmp%\rev.txt" "%ent_file%"
del /q /f "%tmp%\rev.txt"
#echo off
setlocal EnableDelayedExpansion
set digits=0123456789
(for /F "delims=" %%f in (eq_test.txt) do (
set "input=%%f"
set "output="
call :processToken
set /P "=!output:~1!" < NUL
echo/
)) > new_file.txt
goto :EOF
:processToken
for /F "tokens=1* delims=|" %%a in ("%input%") do (
set "token=%%a"
set "input=%%b"
)
if "!digits:%token:~0,1%=!" neq "%digits%" set "token=-%token%"
set "output=%output%|%token%"
if defined input goto processToken
exit /B

Read content of files and check if values are same

I have one file located at C:\Users\abc\Desktop named BUIBNESSDATE.
File contents are
Updated on :
Thu Jan 23 04:05:00 IST 2014
ProfileBusinessDate=23/1/2014
NucleusBusinessDate=23/01/2014
I want script which will check if both have same date(ProfileBusi nessDate & NucleusBusinessDate )date are same. If both date are same then script should give message as OK else NotOk.
how to do?
Test this:
#echo off
set "file=C:\Users\abc\Desktop\BUIBNESSDATE"
for /f "tokens=2 delims==" %%a in (' find /i "profile" ^< "%file%" ' ) do set "p=%%a"
for /f "tokens=2 delims==" %%a in (' find /i "nucleus" ^< "%file%" ' ) do set "n=%%a"
if "%p%"=="%n%" (echo OK) else (echo NotOK)
#ECHO OFF
SETLOCAL
SET "ppd="
SET "nbd="
FOR /f "delims=" %%a IN (q21312106.txt) DO (
SET "line=%%a"
CALL :process
)
IF NOT DEFINED ppd ECHO ProfileBusinessDate missing&GOTO :EOF
IF NOT DEFINED nbd ECHO NucleusBusinessDate missing&GOTO :EOF
FOR /f "tokens=1-3delims=/" %%a IN ("%ppd%") DO CALL :slz ppdd %%a&CALL :slz ppdm %%b&CALL :slz ppdy %%c
FOR /f "tokens=1-3delims=/" %%a IN ("%nbd%") DO CALL :slz nbdd %%a&CALL :slz nbdm %%b&CALL :slz nbdy %%c
IF "%ppdd%-%ppdm%-%ppdy%"=="%nbdd%-%nbdm%-%nbdy%" (ECHO OK) ELSE (ECHO NotOk)
GOTO :EOF
:process
SET "$1=%line:*ProfileBusinessDate=%"
IF NOT "%$1%"=="%line%" SET "ppd=%$1:~1%"&GOTO :EOF
SET "$1=%line:*NucleusBusinessDate=%"
IF NOT "%$1%"=="%line%" SET "nbd=%$1:~1%"&GOTO :EOF
GOTO :EOF
:: suppress a leading 0
:slz
SET "$1=%2"
IF "%$1:~0,1%"=="0" (SET "%1=%$1:~1%") ELSE (SET "%1=%2")
GOTO :eof
Should do the task. I used q21312106.txt with your data as a source file.
#echo off
setlocal enableextension disabledelayedexpansion
set "pdb="
set "nbd="
for /f "tokens=1,2 delims==" %%a in (
'findstr /b /l /c:"ProfileBusinessDate=" /c:"NucleusBusinessDate=" "c:\users\abc\desktop\buibnessdate"'
) do (
if "%%a"=="ProfileBusinessDate" (set "pbd=%%b" ) else (set "nbd=%%b")
)
if "%pdb%"=="%nbd%" (
echo OK
) else (
echo NOT OK
)
Search the file for the required lines, splitting them on equal sign and assigning the values to the adecuated variables. Then check if both variables hold the same content.

Resources