Rename file as per barcode using windows cmd or batch script - batch-file

I have some scan images which having barcode i had found cmd barcode scanner tool
bardecode -f C:\input\file1.png -t code128 >> C:\output\output.txt
this will read the image file and save barcode number in output.txt file.
I want to make a process so this command run on each file in a folder and rename that file with its barcode first 3 or 4 digits.
i try for loop, it runs the command on all png files in folder and print output in file.
for %%i in (C:\input\*.png) do bardecode -f %%i -t code128 >> C:\output\output.txt
but i want to use output to rename that file.

So, if I understood well, with no layout to test, I image the loop to do this.
If possible, please, provide barcode.exe output layout, without this, there no much references to perform test.
Please note that I need to know the layout of output.txt to improve my code:
#echo off && cd /d "%~dp0" & setlocal enableextensions enabledelayedexpansion
for /f "tokens=* delims= " %%i in ('dir /b /a-d C:\input\*.png') do (
for /f "tokens=*" %%I in ('"bardecode.exe" -f "!_name_!" -t code128') do (
set _full_name=%%~fi
set _name_only=%%~ni
set _bar_code_to_rename=%%I
set _bar_code_to_rename=!_bar_code_to_rename:~0,4!-!_name_only!
ren "!_full_name!" "!_bar_code_to_rename!.png"
)
)

Related

downsizing multiple mpg files using ffmpeg.exe

I'm using ffmpeg.exe to process screen captures for a demo using MS Game Bar. Game Bar captures at a high frame rate at high resolution and the files are very large.
Using ffmpeg.exe I can process the files using:
ffmpeg.exe -i file.mpg file_l.mpg
And all is good but I'd like to create a bat file so I can do half a dozen or any in a folder all at once.
My Windows bat skills are poor and I've been trying to get it to work using something like this without success:
for %%f in (*.mp4) do (
ren %%~nf%%~xf !fileNum!%%~xf
set/a fileNum += 1
Can anyone help please
here's one I've used that can perhaps be adapted to meet your needs.
SETLOCAL EnableDelayedExpansion
:: start loop
for %%G in (*.mp3) do (
:: get filename without extension
set OUTPUT=%%~nG
:: pass filename to FFmpeg output
ffmpeg -i %%G !OUTPUT!.wav
:: delete source file
del %%G
)
if your input format and output format are the same, you can do something like this:
SETLOCAL EnableDelayedExpansion
:: start loop
for %%G in (*.mp3) do (
:: get filename with extension
set MYNAME=%%~nxG
:: create temp name
ren !MYNAME! delete1.mp3
:: pass original filename to FFmpeg output
ffmpeg -i delete1.mp3 !MYNAME!
:: delete temp file
del delete1.mp3
)

How to use gdal_calc in a batch file

I'm trying to create a mask file from several input images using gdal with a batch windows file. However, the system is sending me a error when I use the "!" on the comparison calc, and after the first round, all the variables had read as a string.
My code is the following:
#ECHO OFF
SETLOCAL EnableDelayedExpansion
SET mypath=F:\my_in_path\
SET path_salida=F:\my_out_path\
FOR /F %%i IN ('DIR /B %mypath%*.tif') DO (
SET infile=%%i
SET outfile=!infile!
echo %mypath%!infile!
echo %path_salida%!outfile!
gdal_calc -A %mypath%!infile! --outfile %path_salida%!outfile! --calc="2*(A==0)+1*(A==0)" --NoDataValue=0 --quiet
)
As you've provided no feedback over half a day since my comment, here is an example to test and provide feedback on:
#Echo Off
Set "mypath=F:\my_in_path"
Set "path_salida=F:\my_out_path"
Set "calc_params=-A "%%A" --outfile "%path_salida%\%%~nxA" --calc="2*(A==0)+1*(A==0)" --NoDataValue=0 --quiet"
For %%A In ("%mypath%\*.tif") Do gdal_calc %calc_params%

How to pull specific data from comma separated .txt file and apply it to a variable to use in command line switch

I have hundrends of subfolders with identical file structures. One of the files in each folder is a metadata.txt file that contains comma separated information. I need to pull the 8th value and asign it to a variable %x and then use that variable as a switch in a follow-up command.
The metadata.txt file looks like this:
91,Chocolate,10,Large,Easy Bake Oven,350,0,39.9475,Cake,1.0.0.1,C:\CakeRecords,2044449,2(10-50),1,
Step 1: Assign varariable x (%x) the 8th value in the data. In this case it's 39.9475
Step 2: Run EasyBake command using the variable as a switch
Example: EasyBake.exe 39.9475
I'll be running this command one time in the root folder and having it go through every subfolder to run the command in each of them. I've already run a command to create a new folder 'NewRecipe' in each of the subfolders, so I've got a decent idea on how to bounce between folders to run the scripts, but any advice would be appreciated there as well.
FOR /d %A IN ("N:\old recipes\*") DO mkdir "%A\NewRecipe"
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "filename1=%sourcedir%\metadata.txt"
FOR /f "delims=" %%a IN (
'dir /b /s /a-d "%filename1%" '
) DO (
FOR /f "usebackqtokens=8delims=," %%t IN ("%%a") DO (
ECHO take your choice
ECHO PUSHD "%%~dpa"
ECHO MD newrecipe
ECHO easybake.exe %%t
ECHO POPD
ECHO ================
)
)
GOTO :EOF
Simply shows the required instructions.
Find each of the target files, get the appropriate field to %%t, then do what you will with the appropriate elements of %%a

delete a specific word in text file via batch

Best Users,
I want to made a batch script that deletes a specific word in a txt file.
I used the following but it didn't work
(Script that I used) :
SET /p word=device
FOR /f "delims=" %%a IN ('dir /a-d /b "Input_%date%.txt"') DO (
SET "fname=%%~na"
SET "fname=!fname:%word%=!"'
IF NOT "!fname!"=="" REN "%%~a" "!fname!%%~xa"
(Input_%date%.txt):
1015faf2da091b02 device
1115fbd4e0dd3b03 device
I want to delete the word "device" in every line.
Can anyone help me out!
Kind Regards,
A.V.R
I guess you want sth like this:
SETLOCAL EnableDelayedExpansion
set x=device
FOR /f "delims=" %%a IN (Input_%date%.txt) DO (
set b=%%a
echo !b:%x%=!
)
But once you install sed (from for example unxutils) you can do this with simple one liner
type input_%date%.txt | sed "s/device//"
I assumed you can forward output to another file afterwards.

Passing folder path as variable cmd

I want to scan a folder whose path is defined by user input & finally apply ffmpeg to store all media files information into a text file. The following code does not work
#echo off
setLocal EnableDelayedExpansion
set /P "path=Enter folder path: "
dir %path% /B /O:N | findstr ".wmv$ .mpg$ .mkv$ .mpeg$ .mp4$ .avi$" >filename.txt
echo. >info.txt
for /f "tokens=* delims= " %%a in ('type filename.txt') do (
set in=%in%%%a
ffprobe "%path%!in!" 2>>info.txt
)
pause
However if I strip user input as follows the code does work?
#echo off
setLocal EnableDelayedExpansion
::set /P "path=Enter folder path: "
dir d:\Trainers\out\outt\ /B /O:N | findstr ".wmv$ .mpg$ .mkv$ .mpeg$ .mp4$ .avi$" >filename.txt
echo. >info.txt
for /f "tokens=* delims= " %%a in ('type filename.txt') do (
set in=%in%%%a
ffprobe "d:\Trainers\out\outt\!in!" 2>>info.txt
)
pause
The above script is placed in the folder containing ffprobe.exe & it successfully creates two txt files in the same directory
Note that d:\Trainers\out\outt\ is the directory to scan for media files & not the directory from where this bat file is executed
The basic syntax for ffprobe is
ffprobe "videofile"
Use a different variable name than path.
PATH already does something really important. So important, in fact, that your changing it is precisely why the script is breaking.
More specifically, when you try to execute a program using just a filename (no path at all), if the program cannot be found in the working directory, the contents of the PATH environment variable are searched. I haven't seen the error message you're getting, but it's probably failing to execute findstr, which is an executable typically found in a folder specified in PATH.
The user input command looks like the wrong context. Drop the quotes and it should work properly. The quotes are not needed to separate your prompt from your user input.

Resources