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)%
Related
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]
I am new to this, please bear with me. I am try to list all folders in all drives to a list in a txt file. I get errors when the drive search reaches card reader drives and finds them empty. Tried 2>nul with no avail. Am I way off base with my syntax? Thanks
rem ----------list all folders in all drives and copy to a file
for %%a in (c d e f g h i j k l m n o p q u r s t u v w x y z) do (if exist "%%a:\" 2>nul |dir "%%a:\" /ad /b /s >"c:\alldirs.txt")
Next code should work:
echo off
rem ----------list all folders in all drives and copy to a file
(for %%a in (c d e f g h i j k l m n o p q u r s t u v w x y z) do if exist %%a:\ dir %%a:\ /ad /b /s)>"c:\alldirs.txt" 2>nul
or breakdown that ugly line into multiple lines:
(for %%a in (c d e f g h i j k l m n o p q u r s t u v w x y z) do (
if exist %%a:\ (
dir %%a:\ /ad /b /s
)))>"c:\alldirs.txt" 2>nul
I ended up using vol. This worked
for %%a 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 vol %%a: 2>nul |find "Volume" >nul && dir "%%a:\" /ad /b /s >>c:\alldirs.txt
I have some files in a folder and would like to uppercase the first letter of all the filenames with a certain extension using a batch script in windows.
example cap only *.m
before:
foo.m
bar.m
picture.jpg
after:
Foo.m
Bar.m
picture.jpg
for %%a 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 (
ren %%a*.m %%a* >nul 2>&1
)
Check also this -> https://superuser.com/questions/475874/how-does-the-windows-rename-command-interpret-wildcards
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