Loop in .bat file [closed] - batch-file

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 20 days ago.
Improve this question
I must rename more hundred files, they are numbered
on the left, from 01 to 100, on the right side from G3002185 to G3002285
i use this script
rename 01.pdf G3002185.pdf
rename 02.pdf G3002186.pdf
rename 03.pdf G3002187.pdf
rename 04.pdf G3002188.pdf
rename 05.pdf G3002189.pdf
rename 06.pdf G3002130.pdf
(...)
but I would like to shorten it, is there an option to loop it somehow?

#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /L %%c IN (1001,1,1100) DO (
SET "oldname=%%c"
SET /a newname=3002185 - 1001 + %%c
IF %%c==1100 (SET "oldname=!oldname:~-3!") ELSE (SET "oldname=!oldname:~-2!")
ECHO REN "!oldname!.pdf" "G!newname!.pdf"
)
GOTO :EOF
Always verify against a test directory before applying to real data.
The required REN commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO REN to REN to actually rename the files.
Your example appears to be faulty. "06" should be renamed to "G3002190".
And your count is incorrect. The last new-filename should be "G3002284.pdf"

Related

Take a part of the filename and copy it into the file [closed]

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'm looking for a (batch) solution to copy a part of the filename into the textfile. All of the files (thousands) are in one directory and the modified files should go to another directory (let's say c:\tmp ).
The files are called:
0001_Songtitle1.txt
0002_Songtitle2.txt
0003_Songtitle3.txt
The leading number is always 4 digits, ending with a _, whereas the Songtitle can have a different length. I want the number (the first four digits) to be copied into the first row of each file in the following format:
(for the first file) SongID:0001
(for the second file) SongID:0002
(for the third file) SongID:0003
Anybody an idea?
I tried this code:
#echo off
for %%I in (*.txt) do (
echo %%~nI>"%TEMP%\FileName.tmp"
echo/>>"%TEMP%\FileName.tmp"
copy /B "%TEMP%\FileName.tmp" + "%%~I" "%%~I.tmp">nul
del "%%~I"
ren "%%~I.tmp" "%%~nxI"
)
del "%TEMP%\FileName.tmp" 2>nul
It sends the complete filename (without .txt) into the first line. But I struggle to extract just the first four digits.
Thanks,
Ralf
Here is where I meant it to be inserted:
#echo off
for %%I in (????_*.txt) do (
for /f "tokens=1 delims=_" %%J in ("%%~nxI") do (
>"temp.tmp" echo SongID:%%J
>>"temp.tmp" echo/
copy /B "temp.tmp" + "%%~I" "%%~I.tmp">nul
move /y "%%~I.tmp" "%%~nxI" >nul
)
)
del "temp.tmp" 2>nul
I took the freedom to change your code slightly for readability and minor improvements.

Batch - how to create multiple text files? [closed]

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 the following script:
#echo off
:metka
SET /p LPN=[Please scan/add LPN-nr.]
SET /p NumberOfBoxes=[Please add number of boxes/quantity]
echo %LPN%
echo %NumberOfBoxes%
if %LPN%==0 (goto:metka)
pause
Question:
how can I extend the script with the following use cases?
Use case 1:
If %NumberOfBoxes% is less than 2, then create the following text file (where %LPN% is 123):
LPN123.TXT (contents LPN123)
Use case 2:
If %NumberOfBoxes% is greater than or equal to 2, then create as many text files as specified in %NumberOfBoxes% like so (where %LPN% is 123):
LPN123.TXT (contents LPN123)
LPN123-2.TXT (contents LPN123-2)
LPN123-3.TXT (contents LPN123-3)
LPN123-4.TXT (contents LPN123-4)
Many thanks in advance.
I believe what you're looking for is a for loop:
#echo off
:metka
SET /p LPN=[Please scan/add LPN-nr.]
SET /p NumberOfBoxes=[Please add number of boxes/quantity]
echo %LPN%
echo %NumberOfBoxes%
if %LPN%==0 goto metka
echo LPN%LPN% > LPN%LPN%.TXT
for /l %%i in (2, 1, %NumberOfBoxes%) do (
echo LPN%LPN%-%%i > LPN%LPN%-%%i.TXT
)
pause

Looking for a batch script that will name files in a folder starting with A and going to Z [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have found a bunch of that rename numerically but I need one that will do alphabet. I need the first one to be renamed to just a single A and the last to a Z. I won't have more than 26 files in a folder so that shouldn't be a problem.
This loops through all the files in the directory and renames them to a single alphanumeric character. It may break over 26 files, but I haven't tested that. You also might want to add in your own file extension, but since that wasn't mentioned in the original post, I'll leave that up to you.
setlocal ENABLEDELAYEDEXPANSION
set "alpha=abcdefghijklmnopqrstuvwxyz"
set /a index=0
for /r %%i in (*) do (ren %%i !alpha:~%index%,1! & set /a index=(%index%+1)%26)
Presuming your input files are named like this
apple.txt
banana.txt
cherry.txt
Then this untested batch code (please save it in a batch file) should give you
a.txt
b.txt
c.txt
Here is the code
setlocal EnableDelayedExpansion
for %%a in (*.txt) do (
set currentFileName=%%a
REM Remove echo below to rename files
echo ren %%a !currentFileName:~0,1!%%~xa
)

Continue name and numbering [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am trying to add photos to existing photo folder. If my last photo in the folder is photo 56.jpg and I want to add 10 more new photos and want them to continue name and numbering like photo 57.jpg ; photo 58.jpg so on....
Can some one help with a batch file for this.
#echo off &SETLOCAL ENABLEDELAYEDEXPANSION
SET "photofolder=photos"
SET "newfolder=newphotos"
FOR %%a IN ("%photofolder%\Photo *.jpg") DO (
FOR /f "tokens=2" %%b IN ("%%~na") DO IF %%b gtr !hi! SET /a hi=%%b
)
FOR %%a IN ("%newfolder%\*.jpg") DO (
SET /a hi+=1
COPY "%%~fa" "%photofolder%\Photo !hi!%%~xa"
)

how to write a basic batch process to copy files from one location to another location on windows 7 [closed]

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
How do I write a basic batch process to copy files from one location to another location and give a limit or rule where I can enter the number of files that can be copied and from which file to which file?
Example: If I have 300 files I want to just copy from 50 to 250 files.
#ECHO OFF
SETLOCAL
SET "destdir=c:\destdir"
SET "source=%cd%\*.bat"
for /f "tokens=1*delims=:" %%i in (
'xcopy /L /y "%source%" "%destdir%\" ^|findstr /n ":" '
) DO (
IF %%i leq 12 ECHO XCOPY "%%j" "%destdir%\"
)
should do the job, echoing the first 12 copies to be performed, copying the batch files from the current directory to the destination.
The process relies on : in the source filename, so %cd% is used in preference to .

Resources