Batch - how to create multiple text files? [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 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

Related

Loop in .bat 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 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"

How to run net use with multiple lines of input( at once)? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
my batch currently asking for an input for one line of net use command, and then it pauses and asks for another line of input.
My goal would be, that if i copy+paste 10 or 20 lines of directory names separated with an Enter, then it does each line and then i would have 10 or 20 attached network drive.
I couldn't find anything which can help me or i am not sure what to look for exactly.
Is it even possible?
#echo off
cls
:start
set /p Input=Enter shared directory name:
net use * \\%Input%\directory /USER:someuser Somepassword
pause
goto start
Try removing the pause:
#echo off
cls
:start
set /p Input=Enter shared directory name:
net use * \\%Input%\directory /USER:someuser Somepassword
goto start

How to make a batch file read and respond to text input using set/p [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 am trying to create a batch file that asks 3 questions. There is one correct answer to the questions, and if you get it right, it goes to the next question. If you get all of them right, it says something and closes itself. If you get any one of them wrong, it opens another batch file. All the other answers to similar questions had multiple choice questions, whereas I need one with a single answer. One post on this website answered the question, but I cannot find it anymore.
#echo off
:firstquestion
cls
set /p question1="Type your question here. For fanciness, add this character.> "
if "%question1%"=="answer" (
goto secondquestion
) else (
echo WRONG!
pause
exit
)
:secondquestion
cls
set /p question2="Type your question here. For fanciness, add this character.> "
if "%question2%"=="answer" (
goto thirdquestion
) else (
echo WRONG!
pause
exit
)
:thirdquestion
cls
set /p question3="Type your question here. For fanciness, add this character.> "
if "%question3%"=="answer" (
goto win
) else (
echo WRONG!
pause
exit
)
:win
cls
echo Victory Message.
pause
Below is a comprehensive list of all the commands I used in the above example. This is your basic setup for this kind of quiz.
#echo off - This is removes unnecessary echoes.
: - Use this as a group of sorts. Use the goto command to jump to this group.
cls - Clears the command prompt.
set /p variable="message" - Creates a variable that is set to whatever the user responds with.
if/else - Use this to detect whether the variable matches what you want the correct answer to be.
echo Message - Use this to tell the user something. Use echo. to add a text break.
pause - This stops all code until a user presses any key.
exit - Exits the command prompt.

How to check if all the arguments have the .txt file extension in a batch file? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 3 years ago.
Improve this question
I want to check if an unlimited number of arguments in the cmd have the .txt extension.
For example: myscript.bat test.txt test2.exe test3.txt should return an error message because not all of the arguments have the .txt extension.
That's pretty straight forward: for each argument check the extension and stop if it's a wrong one:
#echo off
for %%A in (%*) do (
if /i "%%~xA" neq ".txt" (
echo Wrong extension found
pause
goto :eof
)
)
echo all of them are .txt
pause
Note: an unlimited number of arguments is relative - there is a limitation on how long a command line can be.

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"
)

Resources