Variable value not showing in Win Batch [closed] - windows

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
I am writing a batch to automate the installation of a a number of settings, INI files and APKs on a multitude of devices. I am getting errors in the batch due to the variables in showing NULL.
I have checked that the config.ini and ADBCommands.ini and adb+.bat are in the correct location.
Can you help?
::Global
#echo off
cd C:\
::INI Locations
set mypath=%~dp0
set config=%mypath%config.ini
set Commands=%mypath%ADBCommands.ini
set multi=%mypath%adb+.bat
#pause
::Set Varialbes
#for /f "tokens=1,2 delims==" %%a in (%config%) do (
if %%a==Build set Build=%%b
if %%a==Version set Version=%%b
if %%a==Creator set Creator=%%b
if %%a==DateModified set DateModified=%%b
if %%a==ScreenTimeout set ScreenTimeout=%%b
if %%a==UnknownSources set UnknownSources=%%b
if %%a==ScreenBrightness set ScreenBrightness=%%b
)
#for /f "tokens=1,2 delims==" %%d in (%Commands%) do (
if %%d==ScreenTimeoutCommand set ScreenTimeoutCommand=%%e
if %%d==UnknownSourcesCommand set UnknownSourcesCommand=%%e
if %%d==ScreenBrightnessCommand set ScreenBrightnessCommand=%%e
if %%d==OpenSOTICommand set OpenSOTICommand=%%e
)
::Build information
cls
#echo.
#echo.
#echo BUILD: %Build%
#echo VERSION: %Version%
#echo BUILD CREATOR: %Creator%
#echo LAST UPDATED: %DateModified%
#echo.
#echo.
#pause
Contents the ini files
config.ini
Build=XCover 3
Version=1.0.0
Creator=James B
DateModified=20/02/2017
ScreenTimeout=300000
UnknownSources=1
ScreenBrightness=225
Contents the ini files
ADBCommands.ini
ScreenTimeoutCommand=shell settings put system screen_off_timeout
UnknownSourcesCommand=shell settings put system install_non_market_apps
OpenSOTICommand=shell am start -n net.soti.mobicontrol.elm.samsung/net.soti.mobicontrol.startup.SplashActivity

Because your path has spaces you need to enclose the path with quotes. But when you do this, you need to tell the FOR /F command that you are still parsing a file and not a variable. So you need to use the USEBACKQ option.
#for /f "usebackq tokens=1,2 delims==" %%a in ("%config%") do (
AND
#for /f "usebackq tokens=1,2 delims==" %%d in ("%Commands%") do (

Related

Batch file to find files in a directory

I am creating a batch file to find if any files are present in the path d:\Users\gladiator\Desktop\my docs.
If any files are present, I need to trigger an email stating which are the files present in that path.
This is the snippet of my batch script.
#echo off
for /f "delims=" %%F in ('dir /b /s "d:\Users\gladiator\Desktop\my docs"') do set MyVariable=%%F
echo %MyVariable%files are present
But this script is not displaying all the files present in that location.
Could someone help me to modify the script so as to display all the files present in the location?
Try to use setlocal enabledelayedexpansion to start a local environment before your loop, as described here:
https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/setlocal
Then, after you're done, use endlocal
When you expand the variable MyVariable use !MyVariable! which is how "delayed expansion" within a local environment works (rather than %MyVariable%).
Microsoft does not allow modification of an environment variable with set variable=... within a loop, without using "delayed expansion."
Here is an example with your code:
#echo off
setlocal enabledelayedexpansion
for /f "delims=" %%F in ('dir /b /s "d:\Users\gladiator\Desktop\my docs"') do (
set MyVariable=%%F
echo !MyVariable! file is present
)
endlocal
This will at least allow you to see the files present. Beyond that I'm not sure what you wish to do once filenames are detected.
If you want to fill MyVariable with all files found, then you can do this
#echo off
setlocal enabledelayedexpansion
set MyVariable=
for /f "delims=" %%F in ('dir /b /s "d:\Users\gladiator\Desktop\my docs"') do (
set MyVariable=!MyVariable! %%F
)
echo !MyVariable! files found
endlocal

read multiple paths from a .txt file in a batch file

I have a batch script which requires to read multiple directory paths mentioned in config.txt. I am able to achieve this for one directory path but modified version of it is not working for multiple paths.
Below is the sample which is working fine.
#echo off
for /F "usebackq delims=" %%L in (config.txt) do set "DataPath=%%L"
set "DataPath=%DataPath:/=\%"
echo Application path is: %DataPath%
How to modify this to handle for multiple directory paths.
EDIT:
Below is my attempt to get two paths and '%DataPath%' is printing the value.
#echo off
for /F "usebackq tokens=1,2 delims=" %%L in (config.txt) do set
"DataPath=%%L"&set "filepath=%%M"
set "DataPath=%DataPath:/=\%"
set "filepath=%filepath:/=\%"
echo Application path is: %DataPath%
echo Application path is: %filepath%
Based upon the advice I gave in the comments, which you didn't implement in your edit, the following methodology is what I was expecting you to come up with:
#Echo Off
SetLocal DisableDelayedExpansion
For /F "UseBackQ Delims=" %%L In ("config.txt") Do (
Set "DataPath=%%L"
SetLocal EnableDelayedExpansion
Set "DataPath=!DataPath:/=\!"
Echo Application path is: !DataPath!
EndLocal
)
Pause
This assumes that config.txt contains a list, one per line, of just file paths.
Array version of Path recovery from file.
#ECHO OFF
SETLOCAL EnableDelayedExpansion
Set "_P=0"
FOR /F "tokens=* USEBACKQ" %%a IN (%userprofile%\desktop\logfile.txt) DO (
Set /a _P+=1
Set "Pathway[!_P!]=%%a"
)
::: use the below to take actions with elements in the array.
::: For example, search the array for specific files to delete/copy/move/open.
FOR /L %%e IN (1,1,!_P!) DO (
ECHO Path !%%e! = !Pathway[%%e]!
)
pause

Windows batch script to substitute tabs with 2 spaces [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I need to substitute tab with 2 spaces in all files on a specified path using windows batch script. I see that it is not easy to enter tab character correctly into a batch file. Can anyone help. Thanks
EDIT Corrected output of leading spaces.
The tricky part is to grab the tab char. May be...
#echo off
setlocal EnableDelayedExpansion
rem Grab TAB character
rem http://www.dostips.com/forum/viewtopic.php?f=3&t=7898
set "TAB="
rem First, try the method for Windows XP
for /F "skip=4 delims=pR tokens=2" %%a in ('reg query hkcu\environment /v temp' ) do set "TAB=%%a"
rem Then, the method for newer versions
rem http://www.dostips.com/forum/viewtopic.php?f=3&t=1733&p=6840#p6853
for /F "tokens=2 delims=0" %%a in ('shutdown /? ^| findstr /BC:E') do if not defined TAB set "TAB=%%a"
rem set desired number of spaces
set "SPACES= "
pushd "your_desired_path"
for /f "delims=" %%f in ('dir /B *.txt') do (
> "output_%%~nxf" (
for /f "tokens=* delims=" %%i in (%%~nxf) do (
set "myData=%%i"
set "myData=!myData:%TAB%=%SPACES%!"
echo !myData!
)
)
echo copy /y "output_%%~nxf" "%%~nxf"
echo del "output_%%~nxf" >NUL 2>NUL
)
popd
endlocal
exit/B
Remove echo from copy and del lines above, if it seems to do the work.

.bat - Create a menu from folder file list

I don't usually create .bat file, but I made this little script useful for develop.
I'm using this for reading and creating a list of files contained into a folder:
for /f "delims=|" %%f in ('dir /b C:\src\release\android\') do echo %%f
and I found this about how to create a menu starting from a list of file -> Multiple choices menu on batch file?
Now my question is:
I'd like to create a menu with a list of files contained into that folder which I can select (not multiple selection) by pressing it's relative number on the list, but i don't really know how to merge the two bit of code above.
The final result should work something like:
[1] ..
[2] ..
[3] ..
[4] ..
select file:
and it will install the selected file from the folder.
Any suggestion would be really appreciated.
Thanks in advance
This should work unless you're using a version of Windows that doesn't have choice, like if you're still on XP for some reason.
#echo off
setlocal enabledelayedexpansion
set count=0
set "choice_options="
for /F "delims=" %%A in ('dir /a:-d /b C:\src\release\android\') do (
REM Increment %count% here so that it doesn't get incremented later
set /a count+=1
REM Add the file name to the options array
set "options[!count!]=%%A"
REM Add the new option to the list of existing options
set choice_options=!choice_options!!count!
)
for /L %%A in (1,1,!count!) do echo [%%A]. !options[%%A]!
choice /c:!choice_options! /n /m "Enter a file to load: "
:: CHOICE selections get set to the system variable %errorlevel%
:: The whole thing is wrapped in quotes to handle file names with spaces in them
:: I'm using type because I'm not familiar with adb, but you be able to get the idea
type "C:\src\release\android\!options[%errorlevel%]!"
Improving upon SomethingDark's script to run Python scripts in a user's Document folder (I know, not best practice here for brevity's sake), as it currently wouldn't work when there are more than 10 choices:
#echo off
setlocal enabledelayedexpansion
set count=0
set "choice_options="
for /F "delims=" %%A in ('dir /a:-d /b C:\Users\JohnSmith\Documents\*.py') do (
REM Increment %count% here so that it doesn't get incremented later
set /a count+=1
REM Add the file name to the options array
set "options[!count!]=%%A"
)
for /L %%A in (1,1,!count!) do echo [%%A]. !options[%%A]!
::prompts user input
set /p filechoice="Enter a file to load: "
:: Location of python.exe and location of python script explicitly stated
echo Running !options[%filechoice%]!...
"C:\Users\JohnSmith\AppData\Local\Microsoft\WindowsApps\python.exe" "C:\Users\JohnSmith\Documents\!options[%filechoice%]!"

The system cannot find the path specified - Batch file

I was using the below batch script to search the list of IDs(in a text file) in multiple text files. It was working fine. But today I am getting the Error The system cannot find the path specified.
when I run the same batch script. I cross verified the paths and paths are corerect. Can anyone help me to fix this.
Batch Script I used
#echo off
setlocal enableextensions disabledelayedexpansion
set "manifest_folder=\\vfiler-padhu\padhu\*.txt"
set "file_list=\\vfiler-padhu\padh\File_list.txt"
set "tmpFile=\\vfiler-padhu\padh\tmpFile.txt"
(for /f "usebackq delims=" %%a in ("%file_list%") do (
set "found="
for /f "delims=" %%b in ('findstr /l /m /c:"%%a" "%manifest_folder%"') do (
echo %%a is found in %%~nxb
set "found=1"
)
if not defined found (
echo %%a is not found
)
)) > "%outputFile%"
Thanks in advance
outputFile is not defined; tmpFile is unused. Otherwise, seems fine.

Resources