Need Assistance With Batch File: Detecting Folders on Exhisting Drives - windows

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

Related

.bat file renaming folders, changing first letter of every word to uppercase

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.

numbering variables, when valid path is found

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]

unexpected at this time for searching all drives

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.

.bat script to Search all drives for existing subfolder

Currently I have this:
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\Program Files (x86)\Folder": (
cd "%%d\Program Files (x86)\Folder\subfolder\file"
)
)
But it doesn't seem to be working. I want the batch script to search the drives for the existence of a certain program's subfolder, then cd to the location of a .ini file if it does. The file may also be in a location other than /program files/, i.e. it might be on driveletter:\Folder.
Any help is much appreciated!
You have a few problems with your code:
The colon belongs after the drive letter.
You must use the /D option if your CD command is changing the active drive.
Your logic will fail if "folder" turns out to be a file name instead of a folder name
You never bother to verify that subfolder exists
You cannot CD to a file as your pseudo code implies
The following changes should work:
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:\Program Files (x86)\Folder\subfolder\" (
cd /d "%%d:\Program Files (x86)\Folder\subfolder"
)
)
But there is a better way - you can simply attempt to CD without verifying the existence of the folder, and redirect any error message to null.
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 (
cd /d "%%d:\Program Files (x86)\Folder\subfolder" 2>nul
)
If you want to detect whether the CD was ultimately successful, then you can use && to conditionally break out of the loop, so that the ERRORLEVEL will be 0 upon success, or 1 upon failure.
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 (
cd /d "%%d:\Program Files (x86)\Folder\subfolder" 2>nul && goto :break
)
:break
if %errorlevel% equ 0 (
echo SUCCESS
) else (
echo FAILURE
)
Besides detecting succuess/error, there is another difference with the last option if the subfolder exists on two different drive. The first two options ultimately CD to the last found subfolder. The last option does a CD to the first found subfolder.
if exist "%programfiles(x86)%\Folder\subfolder\file" (
echo it's alive!
)
is this working? Why do you traverse all these letters?
In Windows you should use environment variables to access the program files folder, so you don't have to check for the existing drives.
Take a look at http://ss64.com/nt/syntax-variables.html or enter set in the command prompt.
In your case you should use %ProgramFiles(x86)%

batch if not exist takes out drive letter from variable

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.

Resources