Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I need to do a bat script to process file one by one, according to a number in the filename. Here is an example of what I can get :
CDEP_0003.pdf
CIM_0001.pdf
ESCALE_0002.pdf
It's possible to loop and process each of those files in an ordered way using regex or something like this to exclude the number and use it as a loop index ? (0001, 0002, 0003 etc..)
Simply loop through .*0000\.pdf$ to .*9999\.pdf$ using another answer on how to loop through files matching wildcard in batch file
You could
build an array of used numbers by first iterating through the files, or
use a counting loop (for /l) from 10000 to 19999 to have the
leading zeros and take the last 4 digits..
Provided there is only one underscore preceding the number:
#Echo off
Pushd "X:\folder\to\start"
::clear array
for /f "delims==" %%A in ('Set No[ 2^>Nul') do Set "%%A="
:: fill array
for /f "tokens=2delims=_." %%A in (
'Dir /B "*_????.pdf" ^| findstr "^.*_[0-9][0-9][0-9][0-9]\.pdf$"'
) Do Set /A "No[%%A]=%%A"
:: process existing numbers
for /f "tokens=2 delims=[]" %%A in ('Set No[ 2^>Nul') do (
Rem dir "*_%%A.pdf" or whatever you want to do
)
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 2 years ago.
Improve this question
I have a variable %data% in a Windows batch file which has variable information in the following format:
URL_A
TEXT
URL_B
So essentially, three strings separated by two newline characters. The length of each string varies on each run. I need to split these three lines into three separate variables. The FOR command seemed promising but there doesn't seem to be a way to specify a newline delimiter for the delim= option.
EDIT (as requested by #Compo):
Here is the batch file's syntax:
play_vid.bat -stream <VID_HEIGHT> <START-TIME> <URL>
Here is the command that generates the %data% variable in my batch file (requires youtube-dl to be installed within the %PATH% environment variable to work):
FOR /F "skip=1 delims=" %%a IN ('youtube-dl --get-title -g -f "bestvideo[height<=%2][fps<=30],worstaudio" --no-playlist "%4"') DO #set data=%%a
Here is a sample command with all the batch variables resolved so that it can be run directly from cmd to test and view the contents of the %data% variable:
FOR /F "skip=1 delims=" %a IN ('youtube-dl --get-title -g -f "bestvideo[height<=1080][fps<=30],worstaudio" --no-playlist "https://www.youtube.com/watch?v=ltynWs_LtIw"') DO #echo %a
For this particular example, %data% will contain:
https://manifest.googlevideo.com/api/manifest/dash/expire/1592941694/ei/HgjyXvilD4ndwQG3nrzoBg/ip/[redacted]
How To Stabilize GoPro Video for Free with FFMPEG
https://manifest.googlevideo.com/api/manifest/dash/expire/1592941694/ei/HgjyXvilD4ndwQG3nrzoBg/ip/[redacted]
that is,
line 1: The URL of the video (DASH) stream.
line 2: The title of the video.
line 3: The URL of the audio (DASH) stream.
The for /F command is intended to loop through lines, so line-breaks as delims option would not make much sense. Anyway, you could combine for /F with if defined:
#echo off
rem // Initially clear variables:
set "ONE=" & set "TWO=" & set "THREE="
rem // Read file line by line (note that `eol=;` by default):
for /F "usebackq delims=" %%L in ("data.txt") do (
if not defined ONE (
rem // This section is entered in the 1st iteration:
set "ONE=%%L"
) else if not defined TWO (
rem // This section is entered in the 2nd iteration:
set "TWO=%%L"
) else (
rem // This section is entered in the 3rd iteration (and further):
set "THREE=%%L"
)
)
echo Line 1: %ONE%
echo Line 2: %TWO%
echo Line 3: %THREE%
As an alternative, you could assign a pseudo-array, like this:
#echo off
rem // Initially clear pseudo-array variables:
for /F "delims== eol==" %%V in ('set $LINE[ 2^> nul') do set "%%V="
rem // Read file line by line, precede each one with line number + `:`:
for /F "tokens=1* delims=:" %%K in ('findstr /N "^" "data.txt"') do (
rem // Assign pseudo-array element with line number as index:
set "$LINE[%%K]=%%L"
rem // Conditionally leave loop (optionally):
if %%K geq 3 goto :NEXT
)
:NEXT
rem // Return result:
set $LINE[
For more information on that topic, refer to this comprehensive answer: Arrays, linked lists and other data structures in cmd.exe (batch) script
I don't know if you mean something like this ?
#echo off
<test.txt (
set /p Var1=
set /p Var2=
set /p Var3=
)
echo Var1=%Var1%
echo Var2=%Var2%
echo Var3=%Var3%
pause
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I need to substitute tab with 2 spaces in all files on a specified path using windows batch script. I see that it is not easy to enter tab character correctly into a batch file. Can anyone help. Thanks
EDIT Corrected output of leading spaces.
The tricky part is to grab the tab char. May be...
#echo off
setlocal EnableDelayedExpansion
rem Grab TAB character
rem http://www.dostips.com/forum/viewtopic.php?f=3&t=7898
set "TAB="
rem First, try the method for Windows XP
for /F "skip=4 delims=pR tokens=2" %%a in ('reg query hkcu\environment /v temp' ) do set "TAB=%%a"
rem Then, the method for newer versions
rem http://www.dostips.com/forum/viewtopic.php?f=3&t=1733&p=6840#p6853
for /F "tokens=2 delims=0" %%a in ('shutdown /? ^| findstr /BC:E') do if not defined TAB set "TAB=%%a"
rem set desired number of spaces
set "SPACES= "
pushd "your_desired_path"
for /f "delims=" %%f in ('dir /B *.txt') do (
> "output_%%~nxf" (
for /f "tokens=* delims=" %%i in (%%~nxf) do (
set "myData=%%i"
set "myData=!myData:%TAB%=%SPACES%!"
echo !myData!
)
)
echo copy /y "output_%%~nxf" "%%~nxf"
echo del "output_%%~nxf" >NUL 2>NUL
)
popd
endlocal
exit/B
Remove echo from copy and del lines above, if it seems to do the work.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I need to make windows batch script to rename files that have random names. I have a folder with thousand .txt files, their names are completely random,
I want to rename first 5 files in that folder to file1.txt, file2.txt,file3.txt, file4.txt,file5.txt.
Help appreciated.
Is this okay?
#ECHO OFF
(SET f=C:\Test)
IF /I "%CD%" NEQ "%f%" PUSHD "%f%" 2>NUL||EXIT/B
SET "i=5"
FOR %%A IN (*.txt) DO CALL :SUB "%%A"
EXIT/B
:SUB
IF %i% GTR 0 REN %1 File%i%.txt
SET/A i-=1
Just change line two if your stated directory path\name has changed.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have few mp3 files that have external site names say www.mymusic.com and that name will be either at the start/end of the mp3 file
Examples:
01 - {www.mymusic.com} Hello Hello.mp3,
02 - {www.mymusic.com} Hai Hai.mp3,
03 - Hello Boys [www.musicworld.in].mp3,
04 - Hello girls [www.musicworld.in].mp3
So I want a (windows) batch script which asks me to enter a name with special characters say "[www.musicworld.in]" and removes it from multiple mp3 files present in a directory.
Also give me an example how to use/run the code.
Please help :)
Thanks in advance
The code works immediately!
#echo off &SETLOCAL enabledelayedexpansion
SET /p "word=enter word to remove: "
IF "%word%"=="" GOTO :EOF
FOR /f "delims=" %%a IN ('dir /a-d /b "*%word%*.mp3"') DO (
SET "fname=%%~na"
SET "fname=!fname:%word%=!"
IF NOT "!fname!"=="" REN "%%~a" "!fname!%%~xa"
)
This is a batch file that can help you - it uses a helper batch file called repl.bat from http://www.dostips.com/forum/viewtopic.php?f=3&t=3855
At the moment it only echos the REN command so remove echo if you are happy with the result. One benefit with this is that ! characters are not an issue as they are with delayed expansion.
#echo off
set /p "var=Enter the text to remove: "
for /f "delims=" %%a in ('dir *.mp3 /b /a-d ^|repl "^(.*)%var%(.*)" "ren \x22$&\x22 \x22$1$2\x22" x ') do echo %%a
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I have the files named as such that is divided into three parts that are separated by hyphens.
Motorcycle-001-01.jpg
Motorcycle-001-02.jpg
Motorcycle-001-03.jpg
Motorcycle-002-01.jpg
Motorcycle-002-02.jpg
Motorcycle-002-03.jpg
The first part of the filename denotes the contect, which are pictures of motorcycles. The second part is the series while the third part is the individual picture within the series.
I have an entire directory called Motorcycles with 6,000 or so pictures named this way going all the way to Motorcycle-238-150.jpg
My question is if there is a batch file or script that I can use to sort these files using the series number, or the second part of the file. I would like all the files in my motorcycle directory to be moved into subdirectories within the motorcycle directory. For example, I would like Motorcycle-001-01.jpg through Motorcycle-001-150.jpg to be moved into directory 001. Then Motorcycle-002-01.jpg to Motorcycle-002-135.jpg to moved into directory 002 and so forth.
#Echo OFF
:: By Elektro H#cker
REM Series:
REM If you type "1" it's equals to serie "001"
REM If you type "10" it's equals to serie "010"
REM If you type "100" it's equals to serie "100"
REM You can change that at the pattern multi-comparisions
:: Introduces a number of serie...
:SELECT
Set /P "Serie=Select the serie to move >>"
:: Basic error handling
Echo "%SERIE%" | FINDSTR /I "[a-z]" 1>NUL && (
Echo [-] Type only nums...
Set "Serie="
GOTO:SELECT
)
:: Set the correct pattern for selected serie
IF %SERIE% LEQ 999 IF %SERIE% GTR 99 (Set "PATTERN=%SERIE%")
IF %SERIE% LEQ 99 IF %SERIE% GTR 9 (Set "PATTERN=0%SERIE%")
IF %SERIE% LSS 999 IF %SERIE% LSS 99 IF %SERIE% LEQ 9 (Set "PATTERN=00%SERIE%")
:: Makes the serie directory
MKDIR ".\%PATTERN%\" 2>NUL
:: Move all the files of the serie to the folder
For /F "Tokens=*" %%# in ('DIR /B "Motorcycle-%PATTERN%*"') do (
Move "%%#" ".\%PATTERN%\" >NUL
Echo [+] Moved: %%#
)
Pause&Exit