Windows Batch Script, For Loop stops after three iterations [closed] - windows

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 3 days ago.
Improve this question
Windows Scripting (batch), for loop exiting before all iteration are finished, without an error.
When I run this script
#ECHO OFF && SETLOCAL enabledelayedexpansion
SET PROJECTS[0]=project1
SET PROJECTS[1]=project2
SET PROJECTS[2]=project3
SET PROJECTS[3]=project4
SET PROJECTS[4]=project5
FOR /l %%N in (
0, 1, 2, 3, 4
) DO (
ECHO !PROJECTS[%%N]!
ECHO ===========================================================
)
echo Success.
exit
I get
cmd.exe /c test.bat
project1
===========================================================
project2
===========================================================
project3
===========================================================
Success.
Process finished with exit code 0
But expecting all 5 projects to print.
I cannot find the error. What is happening??

Related

How can I use a string variable in .bat for-loop? [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
In a .bat file, it's ok to use a char-type variable in for-loop. The code below, for example,
#echo off
for /l %%p in (15,2,20) do (
echo %%p
)
gives "15 17 19", as expected. But it doesn't work anymore if I use a string-type variable,
#echo off
for /l %%parameter in (15,2,20) do (
echo %%parameter
)
I wonder if it's possible to make the 2nd piece of code work. Can somebody help me out? Many thanks.
you can use an ordinary environment variable defined with a for metavariable in place of the for metavariable:
#Echo off
Set "Loop[1]=%%#"
Set "Loop[2]=%%#"
For /l %Loop[1]% in (1 1 5) Do For /l %Loop[2]% in (5 1 9) Do (
<nul set /p=%Loop[1]% + %Loop[2]%=
Cmd /c Set /A %Loop[1]% + %Loop[2]%
Echo(
)
The use of %value% is used to define variables that contain more than one character. The %% is used to define variables consisting of a single character.
Since only %% is allowed in the for loop, %value% cannot be used to define variables with more than one character. In this case, the only option is to define a single character variable using %%; such as %%p.
The syntax for the for loop is available below:
for %%a in (list) do command [ parameters ]

Problem with Space in Variable on Batch-Script [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
I have a problem with my batch script.
I want to delete a file witch I first copy in an variable.
If I output the variable all spaces are there.
but if I try to delete it, the filename stops at the first space.
For /F "delims=" %%i in ('dir /B *.mkv') do set orginaldatei=%%~ni.mkv
ECHO %orginaldatei%
del %orginaldatei%
I also tried it without the "delims=".
The solution is to quote the filename.
del "%orginaldatei%"
That's because del accepts a list of files to delete, delimited by spaces.
Quotes help to make clear, if a space is a delimiter or part of a filename.
del my file --- Tries to delete "my" and "file"
del "my file" --- Tries to delete "my file"

Remove files in given folder but leave files in subfolders [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 5 years ago.
Improve this question
I think this should be rather simple but I want to delete all the files that are stated in for example: C:\TEST but I want to leave the files that are located in subdirectories of this folder. For example files in the folder: C:\TEST\Backup should not be deleted.
When using the following batch command all the files get deleted including those located in sub directories but it leaves the folder:
DEL /S C:\TEST\ /q
Does anyone know the command that I need?
It is as simple as this:
from cmdline:
for %a IN (C:\TEST\*.*) do echo "%a"
OR In a batch script, just add an additional %
for %%a IN (C:\TEST\*.*) do echo "%%a"
Just replace echo with del once you are confident with your final script.
OR simply doing:
del C:\TEST /q
All these things can be found by simply opening cmd.exe and running ANY of the commands with the /? switch. For instance.
for /?
del /?
etc.

Quotes not working in script [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 7 years ago.
Improve this question
I am trying to run the below command in a script I am making but due to the spaces the script just will not run as it does not like the syntax..can someone help me on the syntax? I have tried various combinations of double quotes, single quotes and more but it just does not like it:
If Not oLib.RunCommand( "REG ADD ""`HKLM\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\3`"" /V ""`1806`"" /T ""`REG_DWORD`"" /D ""`00000000`"" /F", True, Array(0, 3010)) Then
It just does not amend the registry key though..
Try
If Not oLib.RunCommand("REG ADD ""HKLM\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\3"" /V ""1806"" /T ""REG_DWORD"" /D ""00000000"" /F", True, Array(0, 3010)) Then
OR
If Not oLib.RunCommand("REG ADD 'HKLM\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\3' /V '1806' /T 'REG_DWORD' /D '00000000' /F", True, Array(0, 3010)) Then
If this doesn't work let us know exactly what oLib is and what you are trying to do,

How do you find the last modified folder in a directory with command prompt? [closed]

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 9 years ago.
Improve this question
In the end, I want to copy the last modified folder in a directory. In order to do this, I need to pass in the name of the last modified folder to xcopy.
How do you find the last modified folder, not file, in a directory with command prompt? I have found many scripts that will find the last modified file, but I cannot seem to find a command that will find the last modified folder.
Any help would be appreciated.
#echo off
for /f "delims=" %%a in (' dir /ad /od /b ') do set "folder=%%a"

Resources