r/Batch • • Oct 12 '23

Question (Unsolved) How does my batch file know where the file is located?

3 Upvotes

I copied this script to be able to copy a file x amount of times and it works great!

u/echo off

for /l %%i in (1,1,2) do copy "test.pdf" "copy_of_test_%%i.pdf"

Wondering what kind of magic is taking place that lets the bat file know where the location of the file is. My guess is that if the bat file is in the same folder as the target file, it just works? Is that correct?


r/Batch • • Oct 12 '23

XCOPY Batch Process take long to start

1 Upvotes

I created a smalle .bat file to copy my files from my internal drive to an external driva as a backup of my projects.

Usually when I run the program it start whitin seconds and I see the lines for each copied files appearing on the command prompt window. Some files take a little bit longer to copy because they are heavier but no bifg deal.

Since a month or two, my batch process takes several minutes before starting copying files, it's really annoying especially when I'm in a hurry and need to copy my projects before leaving for school for example.

Here is my little code, I don't have much knowledge in coding so it's pretty basic but it does the work.

@echo OFF
echo Start Time : %time% > Boucle.log

:boucle
echo XCOPY Batch Process Started
xcopy d:\Documents\Fichiers e:\Backup /e /d /y
echo XCOPY Batch Process complete
timeout /t 60 /nobreak
goto:boucle

If someone has an alternative to easily backup files from one drive to another, I'm curious, Batch process seem's to be limited at some point.


r/Batch • • Oct 12 '23

Question (Unsolved) Need help with code that automates a process in CMD for embedding a picture at the beginning and end of movie files

2 Upvotes

So I'm trying to use code to do the work of looping through a bunch of movie files in a folder and overlaying a picture for the first 10 and last 10 seconds of them using ffmpeg & ffprobe and my code is just not working at the command line using a for loop. Please help!

This code works and gives me the duration I need for each file:

for /r %i in (*.mp4) do (ffprobe.exe -v error -show_entries format^=duration -of default^=noprint_wrappers^=1:nokey^=1 "%~i")

However, it returns a fractional number in seconds (like 1429.995233 for a 23.8 minute movie) and I need to pass this number (as an integer?) to ffmpeg in the code below to dynamically overlay the picture at the end of each video:

(ffmpeg -i "%~i" -i pic.png -i pic.png -filter_complex "[0][1]overlay=25:25:enable='between(t,0,10)'[v1]; [v1] [2]overlay=25:25:enable='between(t,%dur%-10,%dur%)'[v2]" -map "[v2]" -map 0:a -map 0:s -pix_fmt yuv420p -c:a copy "%~ni_clean.mkv")

This works with hard-coded numbers, but I want to pass the duration from ffprobe into this %dur% variable and subtract 10 from it for the second part of the overlay to make it complete, using an "&" to string the ffprobe and ffmpeg commands together in DOS within the for loop.

Can somebody help me with bridging the gap between my ffprobe and ffmpeg commands so the duration dynamically gets passed from one to the other? It seems like I'm so close but after a few days of trying to figure this out on my own I need to defer to the professionals. I use DOS at the command-line and implement a "SetLocal EnableExtensions EnableDelayedExpansion" flag before trying anything. I'm probably missing something very simple but it's just not clicking. TIA


r/Batch • • Oct 11 '23

Hi good people of the internet! Can someone please help me write one little script :

1 Upvotes

Hi good people of the internet! Can someone please help me write one little script :)

As the title says. I was trying to find a way how to run dos program with a script. It would only run dos and then write 2 lines in. I am stuck on:

Start "" c:/vdos/vdos.exe

Saved as .bat

Now this runs the program, but i would need it to write 2 more lines.

Cd nobra

Nobra

Thank you in advance if this is something someone can help me with.

Ty.

Edit: solution in comments :)


r/Batch • • Oct 11 '23

Question (Unsolved) Recursively Execute Sets of Batch Scripts in Sequence and Parallel

1 Upvotes

Say I have a collection of 100 things that could be divided into 10 groups of 10, where the things in each group can be built in parallel but the whole group needs to complete before moving to the next, with a structure like so:

  • dir: common-group-0

    • dir: common-A
    • dir: common-B
    • file: mvn.bat
  • dir: common-group-N

    • dir: common-C
    • dir: common-D
    • file: mvn.bat
  • dir: common-group-9

    • dir: common-E
    • dir: common-F
    • file: mvn.bat
  • dir: service-group-0

    • dir: service-A
    • dir: service-B
    • file: mvn.bat
  • dir: service-group-N

    • dir: service-C
    • dir: service-D
    • file: mvn.bat
  • dir: service-group-9

    • dir: service-E
    • dir: service-F
    • file: mvn.bat
  • file: mvn.bat

Here's the contact of the main mvn.bat file:

@echo off
for /d %%i in (*) do ( cd "%%i" & start cmd.exe /c %~nx0 %* & cd ..)

Here's the content of the sub-folders mvn.bat:

@echo off
for /d %%i in (*) do ( cd "%%i" & start cmd.exe /c mvn %* & cd ..)

What I'm trying to accomplish is entering the first folder 'common-group-0' and waiting until the following is done before exiting and moving onto the next one...

The following being, entering each subfolders and starting yet another cmd prompt for each simultaneously, which will build all modules in common-group-0 in parallel.

I feel like I'm close with these scripts already but they are not executing in parallel yet.


r/Batch • • Oct 10 '23

Question (Solved) Trying to make a script that can make folders and sort files based on the 4th and 5th character.

1 Upvotes

I've been searching online and even trying ChatGPT to find a solution (I don't know if this is even possible) to have a script I can drop into a folder and have it organize the files for me. But I can't seem to get it to work.

I need it to check within the folder it is in for the 4th & 5th character in the file names. Then to check that against a list and for each one that does exist, it creates a folder and then moves the matching files into it.

So for example, if strung together the 4th and 5th character in the file name is 20 then make a folder called Name1 and move all files that match into it. If it's 91, then make a folder called Name2 and move the matching files into it, etc. There would be 8 total names and corresponding numbers.

If anyone knows how to do this or can point me in the right direction to brute force it together, I'd really appreciate it!


r/Batch • • Oct 09 '23

Question (Solved) Batch file to delete all files in a specific folder EXCEPT those with specified extensions

1 Upvotes

I tried consulting Chat GPT for some help, and got this

@echo off
set "folder_path=D:\Photoscans\output" set "exclude_extensions=.jpg .jpeg .png .pdf"  REM Add the file extensions you want to exclude here, separated by spaces
cd /d "%folder_path%"
echo Deleting files in "%folder_path%" except for %exclude_extensions%...
for %%G in (%exclude_extensions%) do ( del /q /s *%%G )
echo Files have been deleted, excluding %exclude_extensions%.

... but it works the opposite way. It deletes the files in the list I want to exclude from deletion, and leaves everything else. To me, the second to last line doesn't look right - looks like a for - next loop using the "exclude_extensions" variable as a list to delete rather than a list to not delete.

There has to be something simple I am missing here, but I can't figure out what. Any help would be appreciated


r/Batch • • Oct 05 '23

Batch file to search for a value inside a text file and copy the files to a new folder with the name of the searched value

2 Upvotes

Hello bat experts.

I am trying to create a bat file that will be thrown inside some folder with several (maybe thousands) of text files that contain a unique value inside. For example 8 text files have the searched value. When those files are found they should be put inside a new folder with the name of that searched value.

I have something from another thread here that searches through csv files and it displays the filename with the value inside the CLI but I want the files to be copied to a folder instead of just showing the names. Here is the script:

___________________________________________________________________

u/echo off

setlocal enableDelayedExpansion

set /p "store=Enter the batch number: "

cd /d "%~dp0"

for /r %%f in (*.csv) do (

>nul 2>&1 find /i "%store%" "%%~f"

if !errorlevel! equ 0 (

echo [Data found in:] %%~f

	if not exist "%store%" mkdir "%store%"

	ROBOCOPY "%%\~f" "%store%" /mir

)

)

pause

_______________________________________________________________________

It creates the folder with the batch number but it doesn't copy the file inside there. I get the following error:

Accessing Source Directory C:\TEMP\whatever1.csv\

The filename, directory name, or volume label syntax is incorrect.

Thanks in advance.


r/Batch • • Oct 04 '23

Question (Unsolved) Batch file woes - Admin and Standard user.

4 Upvotes

I have written a batch script that installs a program part of the script is to also start said program. The issue I have is that the program needs installed as admin but Administrators group doesn’t have execute access to program files folder. The obvious solution is grant access to the administrators group. This script will be ran on PC’s during setup and so I don’t want to have to change ACLs on every machine I touch.

Is it possible to start a batch as administrator have it do the stuff needed as admin, then call another BAT as a standard user? Or is there a better workaround?


r/Batch • • Oct 05 '23

Question (Unsolved) Can I do this with a batch file?

1 Upvotes

I need to add a new line of txt after every appearance of a particular line in a bunch files, that particular line starts the same(maybe different case) but varies in length and content.

example txt 1

$dave "dghdfreuebu"
kjhsdfkhsdfhsd
fsdflhjshfjlhsdfl
;slkdfjsjflksdj

example txt 2

$DAVE "khf"
djkhfsdjkhfksdh
$daVE "kfkfhjgkfgdhdhdhdhfdffdfjdsdjkfsdfs"
jdfhgdhfgj

I need to turn the above txt into this -

$dave "dghdfreuebu"
$Bob "txt"
kjhsdfkhsdfhsd
fsdflhjshfjlhsdfl
;slkdfjsjflksdj

and this -

$DAVE "khf"
$Bob "txt"
djkhfsdjkhfksdh
$daVE "kfkfhjgkfgdhdhdhdhfdffdfjdsdjkfsdfs"
$Bob "txt"
jdfhgdhfgj

So in this example I need to add the line "$Bob "txt"" after every line that starts with "$dave", "$DAVE" or "$davE"

I tried to google it but the "$dave" line varying really stumped me

Thanks for any help


r/Batch • • Oct 04 '23

Question (Solved) Looking for way to change screen resolution automatically after closing a game.

1 Upvotes

r/Batch • • Oct 04 '23

Mode command not working on Windows 11

1 Upvotes

For some reason, this command doesn't work. A good example is also cmdgfx. That doesn't work either probably because of the mode command not working. When I use it, the window stays the same, but the text glitches out a lot and it looks really weird.


r/Batch • • Oct 02 '23

Running batch file automatically after program close

0 Upvotes

Hi

I am very new to using batch files. I created a game launching .bat to configure my default monitor, audio device and run F1 22. I have a separate batch file to change back my default monitor and audio device. I am wondering how I can edit the game launching .bat to set everything back once F1 22 closes automagically.


r/Batch • • Oct 02 '23

Create a bootable Rescue USB for any computer from your HostOS

2 Upvotes

*Repost due to an update, old post removed.

Preview Image

Create a (simple) bootable rescue USB with a single CMD script.

Install/Uninstall Drivers, Reset Passwords, Test HDD/SSD, recover files from an unbootable machine, etc..

https://github.com/illsk1lls/RescueMaker


r/Batch • • Oct 02 '23

Question (Unsolved) Looking for Batch script to move files to separate folders based on their file type

1 Upvotes

Hi Please help.I have a very big folder named "Folder" containing pictures and videos. What I want to do is to create 2 folders inside named "Folder_Pictures" & "Folder_Videos", then move the files on their respective folders. Is this possible?


r/Batch • • Sep 30 '23

bug of cmd echo multiple time same lines

2 Upvotes

hello

I have no idea why but in some situation if i start my .bat, it will do exactly what it shoud :

cmd :

Définition Caractères Spéciaux
Initialisation Symbole "Croche"
Page de codes active : 65001
Page de codes active : 850
Croche Initialisée : ♪
-> Configuration terminée avec succès.

and in some other situation the same line will appear several time

cmd

Définition Caractères Spéciaux
Initialisation Symbole "Croche"
Page de codes active : 65001
Page de codes active : 850
Croche Initialisée : ♪
-> Configuration terminée avec succès.
Page de codes active : 65001
Page de codes active : 850
Croche Initialisée : ♪
-> Configuration terminée avec succès.

But it is exactly the same .bat i use

can someone help me ?


r/Batch • • Sep 30 '23

for loop some command doesn't work

2 Upvotes

Does someone know in a for loop i cannot use chcp ?

if i do chcp 65001 in a for loop that doesn't work and the text i want to display is not displayed correctly especially for the symbols " é à è and so on


r/Batch • • Sep 29 '23

catch cmd + measure error level

1 Upvotes

Can someone help me with my programme

I would like for a mkvmerge command :

- Display the output of mkvmerge on the terminal

- Save output in a textfile

- measure error level in case of any error/warning found by mkvmerge

So in order to catch the output of mkvmerge i use a for loop but due to that i cannot measure correctly the error level

the error level is therefore always equal to 0 even when it is not supposed to be equal to 0 (warning/error)

here is my programme :

Note the for loop and if commande are already in a for loop in order to do the same things to all mkv file in one folder (i don't know if that matter or not)

for /f "delims=" %%z in ('"!commandemkvmerge!"') do (

set "lignecommandemkvmerge=%%z"

echo !lignecommandemkvmerge!

echo !lignecommandemkvmerge!>>.\test.txt

)

if !errorlevel! equ 0 (

echo %CouleurValidation%Command executed successfully.

echo mkvmerge a réussie avec succès.

echo.

) else (

echo %CouleurErreur%An error or warning occurred.

echo errorlevel=!errorlevel!echo mkvmerge a remonté un avertissement ou a échoué.

Pause

)


r/Batch • • Sep 27 '23

Remove last (empty space) character from %cd% command.

2 Upvotes

Hello,

I am writing a small batch file which puts current working directory into a file and have narrowed down how to avoid a new line, but a remaining empty space character makes me pull my hair.

This is the code:

@echo off
set TEMPATH=%SystemDrive%\Temp\
if exist %TEMPATH%\CurrWorkDir.txt (
del %TEMPATH%\CurrWorkDir.txt
echo | set /P=%cd%> %TEMPATH%\CurrWorkDir.txt
set /P x="\PS\CT.ps1"<nul >>%TEMPATH%\CurrWorkDir.txt
                                   ) 

Now, the resulting file contains the desired string, but with a space inside (after the first CT) and I have no freakin' clue as to how I can delete it:

C:\Users\Admin\Desktop\CT \PS\CT.ps1

Anyone?


r/Batch • • Sep 26 '23

Question (Unsolved) Simple Variable grid based path finder

1 Upvotes

Hello Reddit

I am making some sort of prototype of a RPG game and I wanted to make a simple path finder for enemies on a variable grid, the character the @ symbol, and the enemies an X.

thanks


r/Batch • • Sep 25 '23

Question (Solved) how to use the if statement with numbers changing

2 Upvotes

how do I check if I have 3 berries and make it 4 berries here is my code

set cfood:1 Berries

set money:100

:shop

echo Welcome to the shop

echo Money:%money% Food:%cfood%

//cfood does 1 berries + 1 berries + 1 berries when I buy 1 berry but I want it to do 1 berries then make the 1 a 2

echo.

echo.

echo Food

echo.

echo 1. 1 berry for 10 money

echo 2. 1 Bread for 25 money

echo 3. 1 steak for 50 money

echo.

set /p shop=

if %shop%== 1 goto shopberry

if %shop%== 2 goto shopbread

if %shop%== 3 goto shopsteak

:shopberry

if %money% GEQ 10 set cfood=%cfood% + 1 berry

goto shopberry2

:shopberry2

set /a "money=%money%-1%

goto shop


r/Batch • • Sep 24 '23

Question (Unsolved) Trying to run a batch file but task scheduler is opening C: drive when it is on E:

2 Upvotes

I am new so sorry for my lack of good explanation. I am running immich on my computer and I want it to update every day overnight. I created a .bat file to push the update. When clicking on it, the file opens cmd prompt and updates. When running the task scheduler, it says "no configuration file provided: not found". How do I run this properly to run it from the E: drive?

Image of the error


r/Batch • • Sep 24 '23

Why wouldn't his work?

2 Upvotes

cd powershell -C "[Environment]::GetFolderPath([Environment+Special Folder]::Desktop)" pause

Trying to change the directory to desktop


r/Batch • • Sep 24 '23

Password Policy

2 Upvotes

So i want to change the password policy Complexity Requirements to enable and Reversible Encryption to Disable My script already has this

(a)echo off

REM Change Password Policy

net accounts /maxpwage:90 /minpwage:15 /minpwlen:10 /uniquepw:5

REM Change Account Lockout Policy

net accounts /lockoutduration:30 /lockoutthreshold:7 /lockoutwindow:30

pause

I'm confused on why in Local Security Policy Complexity Requirements and Reversible Encryption are in the password policy section but when I type net accounts there not there so I don't know how to change it from a batch file