Maybe I did not give it a good title, anyway
Hi,
I writing some script in batch, and need help.
with FOR i checking all possible drives letters A-Z and when on some of that will be found X:\Users\Public\Desktop , then save this letter to numbered variable 1_windrive, 2_windrive, 3_windrive etc........
but my code does not work, and i do not know where is problem.
Here is the code:
#echo off
setlocal EnableDelayedExpansion
set number=1
if not defined !number!_windrive (
for %%p in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do if exist %%p:\Users\Public\Desktop (
set !number!_windrive=%%p
echo !number!%_windrive%
set /a "number=%number%+1"
)
pause
solution code:
#echo off
setlocal EnableDelayedExpansion
set number=1
if not defined windrive_!number! (
for %%p in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do if exist %%p:\Users\Public\Desktop (
set windrive_!number!=%%p
CALL echo %%windrive_!number!%%
set /a number=number+1
)
)
echo %windir%
echo %windrive_1%
echo %windrive_2%
echo %windrive_3%
echo %windrive_4%
echo %windrive_5%
pause
set windrive_!number!=%%p
CALL echo %%windrive_!number!%%
set /a number=number+1
The easy way to list the windrive variables set is
set windrive
As squishy says, starting a variable with a numeric is likely to cause cmd syntactic apoplexy.
%var% means "the value of var when the code-block was started" so it will remain unchanged as the loop progresses. !var! means the value as it changes within the loop.
quotes are not required for a set/a, neither is % nor ! - the variable-name itself means "the run-time value of the variable" (ie as it changes within the loop) and this is regardless of whether or not enabledelayedexpansion has been invoked.
[edit - fixed echo within for loop]
Related
From C:\x\adrenaline_-_shut_the_fug_up_and_dance-2000
to C:\x\Adrenaline_-_Shut_The_Fug_Up_And_Dance-2000
I had this code but it capitalize every single letter instead
#echo off
setlocal disableDelayedExpansion
echo Renaming folders
for /d %%F in (C:\x\*) do (
for /f "eol= " %%A in ("%%~nxF") do (
set "name=%%F"
set "newName=%%A"
setlocal enableDelayedExpansion
for %%C in (
A B C D E F G H I J K L M
N O P Q R S T U V W X Y Z
) do set "newName=!newName:%%C=%%C!"
ren "!name!" "!newName!"
endlocal
)
)
thank you!
How about this modification? I think that this might be able to write more simpler. So please think of this as one of several answers.
Modification points :
Retrieve the initial letter from filename, and convert it to uppercase.
For each initial letter in the filename, retrieve the letter using _. And the letter of behind _ is converted to uppercase.
Add the converted letter to the filename which was removed the initial letter.
If there is the same filename, it is not renamed.
The modified script which reflected above points is as follows.
Modified script :
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
ECHO Renaming folders
SET "DIR=C:\x\"
FOR /D %%F IN (%DIR%*) DO (
SET "BASENAME=%%~NXF"
SET "NAME=%%~NXF"
SET "F=TRUE"
SET "NEWNAME="
CALL :CONVERT
)
EXIT /B
:CONVERT
SET "L=!NAME:~0,1!"
IF %F% == TRUE (
SET "INITIAL=!L!"
FOR %%I IN (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) DO SET INITIAL=!INITIAL:%%I=%%I!
SET NEWNAME=!NEWNAME!!INITIAL!
) ELSE (
SET NEWNAME=!NEWNAME!!L!
)
IF !L! == _ (
SET "F=TRUE"
) ELSE (
SET "F=FALSE"
)
SET "NAME=!NAME:~1!"
IF DEFINED NAME GOTO CONVERT
IF NOT %DIR%!BASENAME! == %DIR%!NEWNAME! REN "%DIR%!BASENAME!" "!NEWNAME!"
EXIT /B
Note :
When you use this, please modify SET "DIR=C:\x\" for your environment.
Please be careful _ of the filename, because the letters for converting to the uppercase are retrieved using _.
If I misunderstand your question, I'm sorry.
I wrote the following batch file to search all drives to find my files but I get "%d:\ was unexpected at this time." error, My code is:
#echo off & setLocal EnableDELAYedeXpansion
for %%d in (c d e f g h i j k l m n o p q r s t u v w x y z) do (
if exist %%d: (
For /R %%d:\ %%G IN (*.zip) do Echo %%G >> zipres.txt
))
What is the problem of my code? Thanks in advance
I don't think the for will be able to parse it like that.May be you can try with:
#echo off
setlocal enableDelayedExpansion
set loc=%cd%
for %%d in (c d e f g h i j k l m n o p q r s t u v w x y z) do (
if exist %%d: (
pushd %%d:
For /R %%G IN (*.zip) do Echo %%G >>"%loc%\zipres.txt"
popd
)
)
or with subroutine.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
This is a batch script to run httpserver in every drive path if drive exists to list its files. in this script set /a %port% is not working.
#echo off
set /a port=8080
for %%i in (c d e f g h i j k l m n o p q r s t u v w x y z) do if exist %%i: (
cd /d %%i:\
start SimpleHTTPServer.exe %port%
set /a port+=1
)
This has to do with how environment variables are expanded inside of blocks in a batch file. To override this behavior, use setlocal enabledelayedexpansion and use ! instead of % to reference the environment variable.
#echo off
setlocal enabledelayedexpansion
set /a port=8080
for %%i in (c d e f g h i j k l m n o p q r s t u v w x y z) do if exist %%i: (
cd /d %%i:\
start SimpleHTTPServer.exe !port!
set /a port+=1
)
Batch File:
#echo off
echo.
echo Verifying existence of File
for %%d in (c d e f g h i j k l m n o p q r s t u v w x y z) do (
if exist %%d:\dir1\dir2\dir3\file1 (
set BDCPATH=%%d:\dir1\dir2\dir3\file1
) else if exist %%d:\dir1_2\dir2\dir3\file1 (
set BDCPATH=%%d:\dir1_2\dir2\dir3\file1
)
)
echo %BDCPATH%
echo %BDCPATH%
IF NOT EXIST %BCDPATH% echo %BCDPATH%
goto :eof
When I echo the '%BDCPATH% variable, it takes out the drive letter. Can you explain why this happens and a fix for this?
cmd output:
c:\Tools\KDNET_Helper>C:\Users\c_jamesp\Desktop\test1.bat
Verifying existence of BCD File
i:\dir1\dir2\dir3\file1
i:\dir1\dir2\dir3\file1
dir1\dir2\dir3\file1
Try this: the quotes fix an issue with certain path names, and the parentheses are changed.
Note that if neither of those path\file exists then the variable will not be set.
#echo off
echo.
echo Verifying existence of File
set "bcdpath="
for %%d in (c d e f g h i j k l m n o p q r s t u v w x y z) do (
if exist "%%d:\dir1\dir2\dir3\file1" (
set "BDCPATH=%%d:\dir1\dir2\dir3\file1"
) else (
if exist "%%d:\dir1_2\dir2\dir3\file1" set "BDCPATH=%%d:\dir1_2\dir2\dir3\file1"
)
)
echo "%BDCPATH%"
echo "%BDCPATH%"
IF NOT EXIST "%BCDPATH%" echo "%BCDPATH%"
if not defined bcdpath echo no files found
pause
goto :eof
good guess, but by spending more time playing around I found out two things:
#echo off
echo.
echo Verifying existence of File
for %%d in (c d e f g h i j k l m n o p q r s t u v w x y z) do (
if exist %%d:\dir1\dir2\dir3\file1 (
set BDCPATH=%%d:\dir1\dir2\dir3\file1
) else if exist %%d:\dir1_2\dir2\dir3\file1 (
set BDCPATH=%%d:\dir1_2\dir2\dir3\file1
)
)
echo %BDCPATH%
echo %BDCPATH%
IF NOT EXIST %BDCPATH% echo %BDCPATH%
:eof
I had %BCDPATH% instead of %BDCPATH%
It should just be either goto somename and not goto :somename.
I've been working on a batch file to search for a folder and if it exhist do a goto command with that variable. It works, but every time you get spammed with:
"There is no disk in the drive. Please insert a disk into drive to \device\hardisk1\dr21 and so on. Is there a way I can prevent this message from popping up?
Batch File:
#echo off
setLocal Enabledelayedexpansion
for %%d in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do (
if exist %%d:\custom\ (
ECHO Device Found : %%d
)
)
if you're calling this from the command line then going:
batch.bat arguments 2> NUL
will redirect all error messages to the NUL device, which will prevent them from popping up.
or create a wrapper subroutine within the batch file itself, for example:
#echo off
setLocal Enabledelayedexpansion
CALL :SUB_A 2> NUL
GOTO :EOF
:SUB_A
for %%d in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do (
if exist %%d:\custom\ (
ECHO Device Found : %%d
)
)
GOTO:EOF
will not return an error because the subroutines error messages are going to NUL